In the last post, we had setup a unittest enviroment for node.js with mocha and chai. Today we want to use it on our testproject
Prerequisits
You have to be installed:Node.js
Chai.js
Mocha.js
Our project from the last post:
$ git clone https://github.com/pboethig/starwarsNames.git
$ npm install
Let's start
This was our code to test. File "src/index.js"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var uniqueRandomArray = require('unique-random-array'); | |
var starWarsNames = require('./starwars-names.json'); | |
module.exports = | |
{ | |
all: starWarsNames, | |
random:uniqueRandomArray(starWarsNames) | |
}; |
For that we implement a function "isTypeOfStringArray()" and the describe function to test that in our testfile under "src/index_test.js"
Here is my "src/index_test.js" You deeply understand how the testcode works, if you have a look on the chai documentation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var expect = require('chai').expect; | |
var starwarsNamesSrcFile = require('./index'); | |
/** | |
* @param array array | |
* @return boolean | |
*/ | |
function isTypeOfStringArray(array) | |
{ | |
return array.every(function(item) | |
{ | |
return typeof item == 'string'; | |
}); | |
} | |
/** | |
* Tests if property all is an array of strings | |
*/ | |
describe('starwars-names',function() | |
{ | |
describe('all', function() | |
{ | |
it('should be an array of strings', function() | |
{ | |
expect(starwarsNamesSrcFile.all).to.satisfy(isTypeOfStringArray); | |
}); | |
}); | |
}); |
Run the test
Now switch to your terminal and type:$ npm t
This will launch the configured testfile "src/index_test.js".We have configured it in "package.json"
Now you should see something like that
Let's create another test
Now we want to test, if "Luke Skywalker" is in the list with starwarsnamesI have added the new test to "src/index_test.js"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var expect = require('chai').expect; | |
var starwarsNamesSrcFile = require('./index'); | |
/** | |
* @param array array | |
* @return boolean | |
*/ | |
function isTypeOfStringArray(array) | |
{ | |
return array.every(function(item) | |
{ | |
return typeof item == 'string'; | |
}); | |
} | |
/** | |
* Tests if property all is an array of strings | |
*/ | |
describe('starwars-names',function() | |
{ | |
describe('all', function() | |
{ | |
it('should be an array of strings', function() | |
{ | |
expect(starwarsNamesSrcFile.all).to.satisfy(isTypeOfStringArray); | |
}); | |
}); | |
describe('all', function() | |
{ | |
it('should contain "Luke Skywalker"', function() | |
{ | |
expect(starwarsNamesSrcFile.all).to.include('Luke Skywalker'); | |
}); | |
}); | |
}); | |
Kent You should now see a terminal like that:
Awesome. That it!
And now the final test
The last test, checks if the "random" function of our library gives us a random starwars-name from the listI have added the test "should contain a random name from the list" to the tests.
Here is my final version of "src/index_test.js"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var expect = require('chai').expect; | |
var starwarsNamesSrcFile = require('./index'); | |
/** | |
* @param array array | |
* @return boolean | |
*/ | |
function isTypeOfStringArray(array) | |
{ | |
return array.every(function(item) | |
{ | |
return typeof item == 'string'; | |
}); | |
} | |
/** | |
* Tests if property all is an array of strings | |
*/ | |
describe('starwars-names',function() | |
{ | |
describe('all', function() | |
{ | |
it('should be an array of strings', function() | |
{ | |
expect(starwarsNamesSrcFile.all).to.satisfy(isTypeOfStringArray); | |
}); | |
}); | |
describe('all', function() | |
{ | |
it('should contain "Luke Skywalker"', function() | |
{ | |
expect(starwarsNamesSrcFile.all).to.include('Luke Skywalker'); | |
}); | |
}); | |
describe('random', function() | |
{ | |
it('should contain a random name from the list', function() | |
{ | |
var randomItem = starwarsNamesSrcFile.random(); | |
expect(randomItem).to.include(randomItem); | |
}); | |
}); | |
}); | |
https://egghead.io/lessons/javascript-how-to-write-a-javascript-library-unit-testing-with-mocha-and-chai
Keine Kommentare:
Kommentar veröffentlichen