Home > OS >  Mocha Error: "isNumberPrime" function is not a function error
Mocha Error: "isNumberPrime" function is not a function error

Time:10-18

I created a function to check if a number is prime & export it to unit test it like so:

/**
 * Method checks if the number is prime & returns true or false
 * @param {number} num - number to be checked if it's prime
 * @returns {boolean} - true if num is prime, false if not
 */
 const isNumberPrime = (num) => num < 10 ? [2, 3, 5, 7].includes(num) : ![2, 3, 5, 7].some(i => !(num % i));

exports.modules = { isNumberPrime };

However, when I ran npm test, I get the error: TypeError: isNumberPrime is not a function. This is my mocha file:

const { expect } = require('chai');
const isNumberPrime = require('../utils/prime-number.js');

describe('Util methods', () => {
  it('should return true if number is prime', () => {
    const primeNumber = isNumberPrime(7);

    expect(primeNumber).to.be.true;
  });
});

enter image description here

CodePudding user response:

you need to either add {isPrimeNumber} in the import on mocha or set module.exports equal too your function

module.exports = isPrimeNumber;

or

const {isNumberPrime} = require('../utils/prime-number.js');
  • Related