javascriptnode.jsmocha.js

Mocha works when using classes but not on functions


Working through a Mocha section of modules on Codecademy, I thought I was getting it but when i tried to practice on my computer for a basic function, it's not working.

Clearly I am missing something and I am struggling to find the answer.

When I copy and paste the class from codecademy that all works fine on my computer, so I know its not a node/mocha/package.json issue.

The function below is basic just for the purposes of testing mocha.

Any help would be much appreciated.

*Note - The code below includes the code from CodeCademy that works when uncommented. The only difference I can see between the two is that CodeCademy is calling a method from a class. *

This is the function within index.js

const returnThree = () => {
    return 3;
}
module.exports = returnThree;

/* const Calculate = {
    factorial(inputInt) {
      if (inputInt === 0){
        return 1;
      }
      for(let x = inputInt - 1; x > 0; x --){
        inputInt *= x;
      }
      return inputInt;
    }
    
  }
  
  module.exports = Calculate; */

This is the code within index.js

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

describe('returnThree', () => {
    it('returns the number three', () =>{
        const expectedAnswer = 3;
        const result = Calculate.returnThree();

        assert.strictEqual(result, expectedAnswer);
    })
});


/*
describe('Calculate', () => {
  describe('.factorial', () => {
    it('returns the multiple of all the integers before it', () => {
      const testNumber = 5;
      const expectedAnswer = 120;

      const result = Calculate.factorial(testNumber);

      assert.equal(result, expectedAnswer);
    });
    it('returns the multiple of all the integers before it', () => {
      const testNumber = 3;
      const expectedAnswer = 6;

      const result = Calculate.factorial(testNumber);

      assert.equal(result, expectedAnswer);
    });
    it('returns 1 when you pass in 0.', ()=>{
      const testNumber = 0;
      const expectedAnswer = 1;

      const result = Calculate.factorial(testNumber);

      assert.equal(result, expectedAnswer);
    })
  });
});
*/

This is what is in the package.json file

{
  "name": "testing-suite",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "directories": {
    "test": "test"
  },
  "scripts": {
    "test": "mocha indexTest.js"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "mocha": "^10.7.3"
  }
}


Solution

  • Managed to resolve this one -

    The issue with my test comes from how I am requiring and using the function returnThree. In the code, I am exporting returnThree as a standalone function. However, in the test file, I treating returnThree as a method of the Calculate object.

    The updated code in the index.js file looks like this -

    exports.returnThree = () => {
      return 3;
    }
    exports.returnTwo = () => {
      return 2;
    };
    

    You'll notice const has been replaced with the export.{functionName} -

    Here is a link to an article

    This is because after getting a single funtions working, I wanted to add multiple.

    Within the indexTest.js file the code now looks like this -

    const assert = require("assert");
    const myFunctions = require('./index.js');  // All functions are under 'myFunctions'
    
    
    describe('returnThree', () => {
      it('returns the number three', () =>{
          const expectedAnswer = 3;
          const result = myFunctions.returnThree();  // Call the function directly
          assert.strictEqual(result, expectedAnswer);
      });
    });
    
    describe('returnTwo', () => {
      it('returns the number two', () =>{
          const expectedAnswer = 2;
          const result = myFunctions.returnTwo();  // Call the function directly
          assert.strictEqual(result, expectedAnswer);
      });
    });
    

    when npm test is run on the terminal both tests pass