I am a beginner of SinonJs, When I try to write some demo code, it cannot work, I don't know why.
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'
I user mocha to run the tests.
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"
}
}
I need to see the whole directory structure, but it's probably because
"scripts": {
"test": "mocha **/*.test.js"
}
is picking up unintended test files in './node_modules/@sinonjs/'.
Change the test script so that the tests only in your source directory are included. E.g.
"test": "mocha './src/**/*.test.js'"
or
"test": "mocha './{,!(node_modules)/**}/*.test.js'"
Most likely mocha is not finding your test files. Without knowing your folder structure it's hard to tell. However, you should always quote your globs in npm scripts. Not sure that will solve your problem here, though. I think it depends on where your *.test.js files live. Try simplifying and not using the glob to start. Just specify the folder. For example, if your test file lives in your src directory use
"test": "mocha './src/*.test.js'"
If you're going to have subdirectories under src
you can use
"test": "mocha './src/**/*.test.js'"
If you don't want to use single quotes you can escape the quotes like this
"test": "mocha \"./src/**/*.test.js\""
Penultimately, when I have test files at the root of the project and in subfolders I just specify both like this:
"test": "mocha *.test.js './src/**/*.test.js'"
Ultimately, I like running all my tests in strict mode so my test command would look like this:
"test": "mocha --use_strict *.test.js './src/**/*.test.js'"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With