我是SinonJs的初学者,当我尝试编写一些演示代码时,它无法正常工作,我不知道为什么。
app.js const db = require('。/ db');
module.exports.signUpUser = (user) => {
db.saveUser(user.email, user.password);
}
app.test.js
const sinon = require('sinon');
// without any other codes, it will throw Error: Cannot find module '@sinonjs/referee-sinon'
我用mocha来运行测试。
的package.json
{
"name": "sinon-test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "mocha **/*.test.js"
},
"author": "",
"license": "ISC",
"devDependencies": {
"mocha": "^5.2.0",
"sinon": "^7.2.3"
}
}
很可能mocha没有找到你的测试文件。不知道你的文件夹结构,很难说。但是,您应该始终在npm脚本中使用quote your globs。但不确定这会解决你的问题。我认为这取决于* .test.js文件的位置。尝试简化并不使用glob来启动。只需指定文件夹即可。例如,如果您的测试文件位于您的src目录中,请使用
"test": "mocha './src/*.test.js'"
如果你打算在src
下有子目录,你可以使用
"test": "mocha './src/**/*.test.js'"
如果您不想使用单引号,则可以像这样转义引号
"test": "mocha \"./src/**/*.test.js\""
倒数第二,当我在项目的根目录和子文件夹中测试文件时,我只是这样指定:
"test": "mocha *.test.js './src/**/*.test.js'"
最后,我喜欢在严格模式下运行所有测试,所以我的测试命令看起来像这样:
"test": "mocha --use_strict *.test.js './src/**/*.test.js'"