我有一个使用 Mocha 和 Expect 进行测试的 node.js 应用程序。所有的测试都很好,直到我安装了 webpack 来做出反应。现在,当我运行“npm test”时,出现以下错误:
Error: Cannot find module 'should'
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (C:\Users\Brian\version-control\tysons-tech-map-redone\node_modules\watchpack\test\DirectoryWatcher.test.js:2:1)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at C:\Users\Brian\version-control\tysons-tech-map-redone\node_modules\mocha\lib\mocha.js:230:27
at Array.forEach (native)
at Mocha.loadFiles (C:\Users\Brian\version-control\tysons-tech-map-redone\node_modules\mocha\lib\mocha.js:227:14)
at Mocha.run (C:\Users\Brian\version-control\tysons-tech-map-redone\node_modules\mocha\lib\mocha.js:495:10)
at Object.<anonymous> (C:\Users\Brian\version-control\tysons-tech-map-redone\node_modules\mocha\bin\_mocha:469:18)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:393:7)
at startup (bootstrap_node.js:150:9)
at bootstrap_node.js:508:3
npm ERR! Test failed. See above for more details.
下面是我的 package.json:
{
"name": "cool-map",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node server.js",
"test": "export NODE_ENV=test || SET \"NODE_ENV=test\" && mocha **/*.test.js",
"test-watch": "nodemon --exec \"npm test\""
},
"engines": {
"node": "6.2.2"
},
"license": "ISC",
"dependencies": {
"bcryptjs": "^2.3.0",
"body-parser": "^1.15.2",
"express": "^4.14.0",
"jsonwebtoken": "^7.1.9",
"lodash": "^4.15.0",
"mongodb": "^2.2.5",
"mongoose": "^4.5.9",
"validator": "^5.6.0",
"hbs": "^4.0.0",
"babel-preset-stage-0": "^6.24.1",
"express": "^4.15.2",
"react": "^0.14.7",
"react-dom": "^0.14.7"
},
"devDependencies": {
"expect": "^1.20.2",
"mocha": "^3.0.2",
"nodemon": "^1.10.2",
"supertest": "^2.0.0",
"babel-core": "^6.5.1",
"babel-loader": "^6.2.2",
"babel-preset-es2015": "^6.5.0",
"babel-preset-react": "^6.5.0",
"webpack": "^1.12.13"
}
}
我不确定是否需要发布 webpack.config,但以防万一:
module.exports = {
entry: './app/app.jsx',
output: {
path: __dirname,
filename: './public/bundle.js'
},
resolve: {
root: __dirname,
alias: {
AdminUserTable: 'app/components/AdminUserTable.jsx'
},
extensions: ['', '.js', '.jsx']
},
module: {
loaders: [
{
loader: 'babel-loader',
query: {
presets: ['react', 'es2015']
},
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/
}
]
}
};
当我卸载 webpack 时,运行命令 npm test 效果很好,我不确定为什么它会破坏 mocha。