我正在尝试构建用于生产的 Nextjs 项目,但出现下一个错误:
./components/Layout/Header/Header.test.tsx
6:1 Error: 'describe' is not defined. no-undef
7:20 Error: 'jest' is not defined. no-undef
9:3 Error: 'it' is not defined. no-undef
11:5 Error: 'expect' is not defined. no-undef
14:3 Error: 'it' is not defined. no-undef
19:5 Error: 'expect' is not defined. no-undef
我在 eslint 文件中修复了它:
{
...
env: {
browser: true,
es2021: true,
node: true,
jest: true, // << Add this line
},
...
}
eslint-plugin-mocha
(npmjs) 是使用 ESLint 更新版本解决该问题的方法:
npm install --save-dev eslint-plugin-mocha
.eslintrc.json
然后在 eslint 配置中添加对此插件的引用和选定的规则:
{
"plugins": [
"mocha"
]
}
eslint.config.js
(需要 eslint >= 8.23.0)
要将此插件与新的 eslint 配置格式(平面配置)一起使用:
import mochaPlugin from 'eslint-plugin-mocha';
export default [
mochaPlugin.configs.flat.recommended // or `mochaPlugin.configs.flat.all` to enable all
// ... Your configurations here
];