How to install and run mocha?

Member

by lily , in category: Javascript , 3 months ago

How to install and run mocha?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by ryleigh , 3 months ago

@lily 

To install and run Mocha, follow these steps:

  1. Install Mocha using npm by running the following command in your terminal:
1
npm install --global mocha


  1. Create a test file with your Mocha test cases. For example, create a file named test.js and add the following Mocha test case:
1
2
3
4
5
6
7
8
9
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);
    });
  });
});


  1. Run your Mocha test by executing the following command in your terminal:
1
mocha test.js


You should see the output of your Mocha test cases in the terminal. Congratulations, you have successfully installed and run Mocha!