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

问题描述 投票:0回答:1

我在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
  }
}
javascript node.js unit-testing jestjs
1个回答
0
投票

结果是由于require-directory

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

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

const exclude = (path) => path.includes('test.js');
module.exports = requireDirectory(module, { exclude });
© www.soinside.com 2019 - 2024. All rights reserved.