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.json
with all the default values.
- This creates a
- Run
npm install --save-dev jest
to install Jest and save it to the development dependencies. - Open
package.json
and modify thetest
script:- 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://devmounta.in/img/logowhiteblue.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.js
file. - Use
module.exports
to export an object. - Add a new property to the object called
returnTwo
:returnTwo
should be a function that returns the integer2
.
- Add a new property to the object called
greeting
:greeting
should be a function with aname
parameter.greeting
should return"Hello, name."
wherename
is the value of thename
parameter.
- Add a new property to the object called
add
:add
should be a function with anum1
andnum2
parameter.add
should return the sum ofnum1
andnum2
.
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.js
at the top. - Create a test for
returnTwo
:- This test should
expect
returnTwo()
to equal2
.
- This test should
- Create a test for
greeting
:- This test should
expect
greeting('James')
to equal"Hello, James."
. - This test should
expect
greeting('Jill')
to equal"Hello, Jill."
.
- This test should
- Create a test for
add
:- This test should
expect
add(1, 2)
to equal3
. - This test should
expect
add(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
add
function.
- 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
add
test case. - Use whatever numbers you like, but include two
expect
s.
- All these tests should follow the same structure as the
- Group the
add
,multiply
,divide
, andsubtract
test 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 );
});
});