Mocha Chai
In this tutorial we will setup Mocha to test JavaScript code.
In the previous tutorial we created a new project mocha-chai-project and installed Mocha and Chai via npm.
test
in the package.json fileOpen package.json file and set the test
property.
By default it is set to the following.
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
}
We want to run Mocha via npm so, we will set the test
property to mocha || true
.
So, change it to the following:
"scripts": {
"test": "mocha || true"
}
If we now run the command npm run test
npm will run Mocha which will run the tests for us.
$ npm run test
> mocha-chai-project@1.0.0 test /Users/yusufshakeel/Documents/GitHub/mocha-chai-project
> mocha || true
Warning: Could not find any test files matching pattern: test
No test files found
Note! Since we have not yet written any test code so, we are getting No test files found
message.
test
directoryIf we check the above output we will notice that there is a warning line.
Warning: Could not find any test files matching pattern: test
This is because when we run the npm run test
command Mocha looks for the test code inside that test
directory. Since we have not yet created the test directory so, we are getting the above warning line.
To fix the warning issue we will create the test directory inside our project directory.
$ mkdir test
If we run the command again we will not get the warning.
$ npm run test
> mocha-chai-project@1.0.0 test /Users/yusufshakeel/Documents/GitHub/mocha-chai-project
> mocha || true
No test files found
Inside this directory we will save our test files. So lets go ahead and write some JavaScript code and test them in the next tutorial.
Thanks for reading. See you in the next tutorial.
ADVERTISEMENT