【问题标题】:Jest / ESLint fail is not definedJest / ESLint 失败未定义
【发布时间】:2021-02-01 10:30:36
【问题描述】:

我刚刚使用npx react-native init 生成了一个新项目,而 ESLint 在我的一个测试文件中抱怨:

   9:1   error  'describe' is not defined    no-undef
  12:5   error  'beforeEach' is not defined  no-undef
  16:5   error  'afterEach' is not defined   no-undef
  20:5   error  'test' is not defined        no-undef
  28:17  error  'fail' is not defined        no-undef
  30:13  error  'expect' is not defined      no-undef

基于docs 和这个thread,我添加了:

env: {
    jest: true,
},

到我的.eslintrc.js 文件。但是,ESLint 仍然在抱怨:

  28:17  error  'fail' is not defined  no-undef

有没有人已经经历过并解决了这个问题?

这是package.json中的笑话依赖版本:

"babel-jest": "^26.5.2",
"jest": "^26.5.3",

【问题讨论】:

  • 如果您在编辑器中看到此消息,请确保将其关闭然后再打开。有时编辑器不会发现 ESLint 配置发生了变化。
  • 不幸的是,我是从我手动执行命令的终端看到的。
  • 除此之外,我不太确定。我昨天遇到了和你完全相同的问题,我通过在配置中添加 env 属性解决了这个问题。我使用了this approach,所以它只对测试文件启用(我必须调整过滤器,以匹配我的*.test.ts 命名方案)但它确实有效 - ESLint 停止抱怨。我还在覆盖中添加了一个小的 "rules" 部分,以调整测试的一些风格规则。
  • 您是否也在您的一项测试中使用fail
  • 好的,所以我添加了一个非常简单的测试它("fails", function() { fail("always"); })`并且测试失败(预期)。 ESLint 也抱怨no-undef,但它对describe 或`expect.我得看看那个不起作用,这很奇怪。

标签: react-native jestjs eslint babel-jest


【解决方案1】:

好吧,事实证明 Jest 使用 Jasmine 的 fail()

通过添加 Jasmine 更新了 .eslintrc.js,它可以工作了。没有更多错误。

env: {
    jasmine: true,
    jest: true,
},

【讨论】:

    【解决方案2】:

    自 27.0.0 起,前运行程序 jest-jasmine2 在 Jest 中已弃用 取而代之的是 jest-circus。

    更新 .eslintrc.js

    env: {
        jest: true,
        circus: true,
    }
    

    【讨论】:

      猜你喜欢
      • 2018-02-26
      • 2021-06-12
      • 2018-02-12
      • 2021-05-19
      • 2021-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-26
      相关资源
      最近更新 更多