Mocha Error: "isNumberPrime" function is not a function error

Mocha Error: "isNumberPrime" function is not a function error

我创建了一个 function 来检查 number 是否为质数并将其导出到 unit test,就像这样:

/**
 * 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 };

但是,当我 运行 npm test 时,我得到错误:TypeError: isNumberPrime is not a function。这是我的 mocha 文件:

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

您需要在 mocha 的导入中添加 {isPrimeNumber} 或设置 module.exports 等于您的函数

module.exports = isPrimeNumber;

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