In this project, we'll learn how to unit test JavaScript files by using Jest. Jest is a unit testing framework that was developed by Facebook. One of the cool features about this testing framework is that it doesn't require any configuration to get up and going and it will also automagically find test files that are named .test.js or located in a __tests__ folder.
In this step, we'll initialize a package.json and import Jest into our project.
- Run
npm init -y.- This creates a
package.jsonwith all the default values.
- This creates a
- Run
npm install --save-dev jestto install Jest and save it to the development dependencies. - Open
package.jsonand modify thetestscript:- In the string, replace everything with just
"jest".
- In the string, replace everything with just
package.json
{
"name": "unit-testing-mini",
"version": "1.0.0",
"description": "<img src=\"https://s3.amazonaws.com/devmountain/readme-logo.png\" width=\"250\" align=\"right\">",
"main": "functions.js",
"scripts": {
"test": "jest"
},
"repository": {
"type": "git",
"url": "git+https://github.com/DevMountain/unit-testing-mini.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/DevMountain/unit-testing-mini/issues"
},
"homepage": "https://github.com/DevMountain/unit-testing-mini#readme",
"devDependencies": {
"jest": "^21.1.0"
}
}In this step, create a JavaScript file that has a couple functions.
- Create a
functions.jsfile. - Use
module.exportsto export an object. - Add a new property to the object called
returnTwo:returnTwoshould be a function that returns the integer2.
- Add a new property to the object called
greeting:greetingshould be a function with anameparameter.greetingshould return"Hello, name."wherenameis the value of thenameparameter.
- Add a new property to the object called
add:addshould be a function with anum1andnum2parameter.addshould return the sum ofnum1andnum2.
functions.js
module.exports = {
returnTwo: function() {
return 2;
},
greeting: function( name ) {
return `Hello, ${ name }.`;
},
add: function( num1, num2 ) {
return num1 + num2;
}
};In this step, we'll create a test file to test the functions inside of functions.js.
- Create a new test file called
functions.test.js. - Open
functions.test.js. - Require
functions.jsat the top. - Create a test for
returnTwo:- This test should
expectreturnTwo()to equal2.
- This test should
- Create a test for
greeting:- This test should
expectgreeting('James')to equal"Hello, James.". - This test should
expectgreeting('Jill')to equal"Hello, Jill.".
- This test should
- Create a test for
add:- This test should
expectadd(1, 2)to equal3. - This test should
expectadd(5, 9)to equal14.
- This test should
functions.test.js
const functions = require('./functions');
test("returnTwo() should return 2.", () => {
expect( functions.returnTwo() ).toEqual( 2 );
});
test("greeting() should return a dynamic greeting based on name.", () => {
expect( functions.greeting('James') ).toEqual('Hello, James.');
expect( functions.greeting('Jill') ).toEqual('Hello, Jill.');
});
test("add() should return a dynamic sum based on two number parameters.", () => {
expect( functions.add( 1, 2 ) ).toEqual( 3 );
expect( functions.add( 5, 9 ) ).toEqual( 14 );
});In this step, we'll run our test script and watch Jest in action.
- Run
npm test.
In this step, we'll go over how to group unit tests. This helps keep tests organized.
- Open
functions.js. - Add a new function for
multiply,divide, andsubtract:- All these functions should follow the same structure as the
addfunction.
- All these functions should follow the same structure as the
- Open
functions.test.js. - Add a new test case for
multiply,divide, andsubtract:- All these tests should follow the same structure as the
addtest case. - Use whatever numbers you like, but include two
expects.
- All these tests should follow the same structure as the
- Group the
add,multiply,divide, andsubtracttest cases usingdescribe.- Call this group
Math functions.
- Call this group
- Run
npm test.
functions.js
module.exports = {
returnTwo: function() {
return 2;
},
greeting: function( name ) {
return `Hello, ${ name }.`;
},
add: function( num1, num2 ) {
return num1 + num2;
},
multiply: function( num1, num2 ) {
return num1 * num2;
},
divide: function( num1, num2 ) {
return num1 / num2;
},
subtract: function( num1, num2 ) {
return num1 - num2;
}
}; functions.test.js
const functions = require('./functions');
test("returnTwo() should return 2.", () => {
expect( functions.returnTwo() ).toEqual( 2 );
});
test("greeting() should return a dynamic greeting based on name.", () => {
expect( functions.greeting('James') ).toEqual('Hello, James.');
expect( functions.greeting('Jill') ).toEqual('Hello, Jill.');
});
describe("Math functions:", () => {
test("add() should return a dynamic sum based on two number parameters.", () => {
expect( functions.add( 1, 2 ) ).toEqual( 3 );
expect( functions.add( 5, 9 ) ).toEqual( 14 );
});
test("multiply() should return a dynamic product based on two number parameters.", () => {
expect( functions.multiply( 1, 2 ) ).toEqual( 2 );
expect( functions.multiply( 5, 9 ) ).toEqual( 45 );
});
test("divide() should return a dynamic quotient based on two number parameters.", () => {
expect( functions.divide( 2, 1 ) ).toEqual( 2 );
expect( functions.divide( 9, 3 ) ).toEqual( 3 );
});
test("subtract() should return a dynamic difference based on two number parameters.", () => {
expect( functions.subtract( 2, 1 ) ).toEqual( 1 );
expect( functions.subtract( 9, 3 ) ).toEqual( 6 );
});
});If you see a problem or a typo, please fork, make the necessary changes, and create a pull request so we can review your changes and merge them into the master repo and branch.
© DevMountain LLC, 2017. Unauthorized use and/or duplication of this material without express and written permission from DevMountain, LLC is strictly prohibited. Excerpts and links may be used, provided that full and clear credit is given to DevMountain with appropriate and specific direction to the original content.


