如何使用 Typescript 解决 Nextjs 中的“错误:‘描述’未定义”?

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

我正在尝试构建用于生产的 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

typescript jestjs next.js
2个回答
1
投票

我在 eslint 文件中修复了它:

{
...
  env: {
    browser: true,
    es2021: true,
    node: true,
    jest: true, // << Add this line
  },
...
}

0
投票

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
];
© www.soinside.com 2019 - 2024. All rights reserved.