用摩卡测试 NodeJS:'Require is not defined'



编辑:

根据对以下答案的评论:删除";类型":"模块";从package.json,据我所知,这正是Node理解"import"one_answers"export"语句的原因,并将所有内容还原为"require"one_answers"module.exports"解决了这个问题。

有没有办法保持"进口"one_answers"出口",同时还能让Mocha发挥作用?


我有一个非常简单的Node文件,我正在尝试用Mocha/Chai测试它。实际的代码很琐碎,这只是为了了解一下Mocha以及如何使用它。但当我运行Mocha测试时,我得到了错误ERROR: ReferenceError: require is not defined`

我在谷歌上搜索了一些遇到同样问题的人,但我想到的例子是他们在浏览器中运行测试时(例如,Mocha,在浏览器中测试时"未定义需求"(。

我要测试的文件,index.js


const argv = require('minimist')(process.argv.slice(2));

const digitTester = /d/g;
const capTester = /[A-Z]/g;
const dict  = {
length:10,
must_have_numbers: true,
must_have_caps: true
}
export default function passwordCheck(password) {
if (!password) return false;
if (typeof password !== "string") return false;
if (password.length < dict.length) return false; // assumes that 10 is a MINIMUM length
if (dict.must_have_numbers && !digitTester.test(password)) return false;
return !(dict.must_have_caps && !capTester.test(password));
}
if (argv._.length) {
console.log(passwordCheck(argv._[0]))
}
/**
* alternate version to check a lot of passwords:
*
* if (argv._.length) {
*  for (const pwd of argv_) console.log(passwordCheck(pwd)
*}
*
*/

摩卡文件,test/index.test.js


const chai = require('chai')
const expect = chai.expect
const passwordCheck = require('../index.js')
const tooShort = "A2a"
const noCaps = "a2abcdefghijklmnop"
const noNumber = "Aabcdefghijklmnop"
const good = "A2abcdefghijklmnop"
describe('password checking', () => {
it('should return false for passwords less than length 10', () => {
expect(passwordCheck(tooShort)).to.be.false;
});
it('should return false for passwords without a capital letter', () => {
expect(passwordCheck(noCaps)).to.be.false;
});
it('should return false for passwords without a number', () => {
expect(passwordCheck(noNumber)).to.be.false;
});
it('should return true for passwords that match criteria', () => {
expect(passwordCheck(good)).to.be.true;
});
});

package.json

{
"name": "codetest",
"version": "1.0.0",
"main": "index.js",
"type": "module",
"scripts": {
"test": "mocha"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"@types/minimist": "^1.2.1",
"@types/node": "^14.14.20",
"chai": "^4.2.0",
"minimist": "^1.2.5",
"mocha": "^8.2.1"
}
}

并且错误消息是

✖ ERROR: ReferenceError: require is not defined
at file:///Users/r/Documents/Projects/sandbox/pwd_checker/index.js:2:14
at ModuleJob.run (node:internal/modules/esm/module_job:152:23)
at async Loader.import (node:internal/modules/esm/loader:166:24)
at async exports.handleRequires (/Users/r/Documents/Projects/sandbox/pwd_checker/node_modules/mocha/lib/cli/run-helpers.js:94:28)
at async /Users/r/Documents/Projects/sandbox/pwd_checker/node_modules/mocha/lib/cli/run.js:341:25
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

节点15。

删除此行-"类型":"模块";从package.json,检查它是否工作。

使用以下内容准备测试:

import { createRequire } from 'module';
const require = createRequire(import.meta.url);

这是因为您不能要求ESM模块;有关更多信息,请参阅关于nodejs问题的评论。

文件:https://nodejs.org/api/esm.html#differences-es模块与commonjs 之间

最新更新