1

我有一个使用 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。

4

2 回答 2

2

package.json缺少should依赖项。

通过安装它;

npm install --save-dev should

另外,我建议您查看chai,我认为它提供了稍微不同的 API。

于 2017-07-05T06:51:20.213 回答
1

should 是一个表达性强、可读性强、与框架无关的断言库。这个库的主要目标是表达和提供帮助。它使您的测试代码保持干净,并且您的错误消息很有帮助。默认情况下(当您需要('should')时)应该使用单个不可枚举的 getter 扩展 Object.prototype,它允许您表达该对象的行为方式。它也会在需要时返回自身。也可以在没有 getter 的情况下使用 should.js(它甚至不会尝试扩展 Object.prototype),只需 require('should/as-function')。或者,如果您已经使用自动添加 getter 的版本,您可以调用 .noConflict 函数。(something).should getter 和 should(something) 在大多数情况下的结果是相同的

更好的你安装节点依赖项应该使用 npm 如下

npm install --save should

应该参考

于 2017-07-05T03:03:18.153 回答