Search code examples
javascriptnode.jsmocha.jsassert

mocha test no error but did not show green arrow


I have index.js and have a method like this

const Calculate = {
  exponential(base, power) {
    let accumulator = base

    for(let iterator = 0; iterator < accumulator; iterator++) {
      accumulator *= base;
    }
    return accumulator
  }
}

module.exports = Calculate;

My test suite is here

const assert = require('assert');
const Calculate =  require('../index.js')

describe('Calculate', () => {
  describe('.exponential', () => {
    it('returns the result of a base raised to a power', () => {
      const base = 3
      const power = 2
      const expected = 9

      const result = Calculate.exponential(base, power)
      assert.equal(result, expected)
    })
  })
});

When I run npm test I don't see any error, no green and red indicator on my terminal : https://i.sstatic.net/Bs2Hp.png

What's going on?


Solution

  • Your logic inside for loop goes infinite. To run the the test use npm run test and modify the package.json accordingly.

    Package.json

    {
      "name": "mocha-test",
      "version": "1.0.0",
      "description": "",
      "main": "index.js",
      "scripts": {
        "test": "mocha"
      },
      "author": "",
      "license": "ISC",
      "devDependencies": {
        "mocha": "^5.0.1"
      }
    }
    

    index.js

    const Calculate = {
        exponential(base, power) {
            let result=1;
            for(let iterator = 0; iterator < power; iterator++) {
                result *= base;
            }
            return result;
        }
    }
    
    module.exports = Calculate;
    

    test.js

    const assert = require('assert');
    const Calculate =  require('./index.js')
    
    describe('Calculate', () => {
        describe('.exponential', () => {
            it('returns the result of a base raised to a power', () => {
                const base = 3
                const power = 2
                const expected = 9
    
                const result = Calculate.exponential(base, power)
                assert.equal(result, expected)
            });
        });
    });
    

    Output
    enter image description here