How to setup an npm test via mocha?

by arnoldo.moen , in category: Javascript , a year ago

How to setup an npm test via mocha?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by mac , a year ago

@arnoldo.moen 

To set up an npm test using Mocha, follow these steps:

  1. Install Mocha globally by running the command: npm install -g mocha
  2. Install Mocha as a development dependency in your project by running the command: npm install --save-dev mocha
  3. Create a directory in your project for your test files, for example test.
  4. 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); }); }); });
  5. 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" }
  6. 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.

Related Threads:

How to debug node.js/mocha test built with makefile?
How to test in mocha?
How to pass arguments/parameters to mocha tests invoked via grunt?
How to test a class in mocha?
How to pass an object to a mocha test?
How to test through a mocked promise with mocha?