Skip to content
the poor coder

mocha test cases in node js example

Poor Man
Published
Reading time
2 min read
On this page

How to write Mocha test cases in Node.js with example?#

As a software developer, testing is an essential part of the development process. In Node.js, Mocha is a popular JavaScript testing framework that allows developers to write test cases for their code. Mocha is flexible, easy to use, and supports both async and sync testing. Here's an example of how to write Mocha test cases in Node.js:

Step 1: Install Mocha#

Firstly, install Mocha via npm:

Text
npm install --global mocha

Step 2: Create a Test File#

Next, create a test file in your project directory. Let's call it "test.js".

Text
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);
        });
      });
    });

Step 3: Run the Test File#

To run the test file, open your terminal and navigate to your project directory. Then run:

Text
mocha

Step 4: View the Results#

You should see the results of your test cases:

Text
Array
      #indexOf()
        ✓ should return -1 when the value is not present
    
    1 passing (5ms)

Alternative Way 1: Chai Assertion Library#

Chai is another popular assertion library that can be used with Mocha. Here's an example:

Text
const expect = require('chai').expect;
    
    describe('Array', function() {
      describe('#indexOf()', function() {
        it('should return -1 when the value is not present', function() {
          expect([1,2,3].indexOf(4)).to.equal(-1);
        });
      });
    });

Alternative Way 2: Async Testing#

Mocha also supports async testing. Here's an example:

Text
describe('File', function() {
      describe('#readFile()', function() {
        it('should read file asynchronously', function(done) {
          fs.readFile('./test.txt', function(err, data) {
            if (err) throw err;
            expect(data.toString()).to.equal('Hello World\n');
            done();
          });
        });
      });
    });

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC