@arnoldo.moen
To set up an npm test using Mocha, follow these steps:
- Install Mocha globally by running the command:
npm install -g mocha
- Install Mocha as a development dependency in your project by running the command:
npm install --save-dev mocha
- Create a directory in your project for your test files, for example test.
- Create a test file within the test directory, for example test.js, and write your test cases using Mocha's syntax. Here is an example of a simple test case:
const assert = require('assert');
describe('Array', function() {
describe('#indexOf()', function() {
it('should return -1 when the value is not present', function() {
assert.equal([1,2,3].indexOf(4), -1);
});
});
});
- Add a test script to your package.json file that runs Mocha with your test files. Modify the scripts section in your package.json file like this:
"scripts": {
"test": "mocha test/*.js"
}
- Run the npm test command to execute your Mocha tests:
npm test
Your Mocha tests will now run and display the results in your console.