Jestjs-uncaughtException:描述未定义\ nReferenceError:描述未定义

时间:2020-02-03 13:22:49

标签: javascript node.js unit-testing jestjs

在我的Nodejs应用程序中使用Jestjs进行单元测试时,出现了此错误uncaughtException: describe is not defined\nReferenceError: describe is not defined。但是,所有测试在npm run test下仍然可以正常运行。

我注意到,在根文件夹下2层的子文件夹中创建了新的测试文件后,出现了错误。

我该如何解决?

目录树

...
app.test.js // this doesn't cause the error
modules/
   ├── utils.js
   ├── utils.test.js // this doesn't cause the error
   ├── database
        ├── database.js
        ├── database.test.js // the error appeared after this file created
...

package.json

...
"devDependencies": {
    "eslint": "^6.8.0",
    "eslint-config-airbnb-base": "^14.0.0",
    "eslint-plugin-import": "^2.19.1",
    "eslint-plugin-jest": "^23.6.0",
    "jest": "^24.9.0",
    "jsdoc": "^3.6.3",
    "nodemon": "^2.0.2",
    "supertest": "^4.0.2"
  },
"scripts": {
    "test": "jest --watchAll --verbose",
    "start": "nodemon server.js"
  },
  "jest": {
    "testEnvironment": "node",
    "coveragePathIgnorePatterns": [
      "/node_modules/"
    ]
  },
...

eslintrc.json

{
  "env": {
    "browser": true,
    "commonjs": true,
    "es6": true,
    "jest": true
  },
  "extends": [
    "airbnb-base"
  ],
  "plugins": ["jest"],
  "globals": {
    "Atomics": "readonly",
    "SharedArrayBuffer": "readonly"
  },
  "parserOptions": {
    "ecmaVersion": 2018
  },
  "rules": {
    "no-shadow": 0
  }
}

1 个答案:

答案 0 :(得分:0)

原来是由于require-directory

排除数据库文件夹下index.js中的所有.test.js文件,可以解决此问题:

const requireDirectory = require('require-directory');

const exclude = (path) => path.includes('test.js');
module.exports = requireDirectory(module, { exclude });