繁体   English   中英

eslint 用玩笑忽略某些文件夹的 no-undef

[英]eslint ignore no-undef with certain folder with jest

我目前正在使用 jest 来测试我的 api 调用。 我也使用 eslint 来检查我的测试中的代码,但是因为使用 just,我不需要定义诸如test()expect()所以当我运行 eslint 时,我会得到诸如

   4:1  error  'test' is not defined    no-undef
   8:3  error  'expect' is not defined  no-undef

但是对于我的笑话文件,例如index.test.js我的代码将是

test('API test', async () => {
  const response = await axios.post('api call', {});
  const { status, statusText, message } = response;
  expect(status).toBe(400);
  expect(statusText).toBe('Bad Request');
  expect(message).toBe('No password.');
});

如前所述,我不需要定义test()expect 我正在阅读 eslint 文档,但我似乎没有看到一种可能的方法来只忽略某些文件而不检查这些规则。 不过,我仍然想保留其他规则。

在此先感谢您的任何帮助建议。

PS 我的.eslintrc看起来像

{
  "parser": "babel-eslint",
  "extends": "eslint:recommended",
  "env": {
    "commonjs": true,
    "es6": true,
    "node": true,
    "browser": false
  },
  "parserOptions": {
    "ecmaFeatures": {
      "experimentalObjectRestSpread": true,
      "jsx": false
    },
    "sourceType": "module"
  },
  "globals": {
    "strapi": true
  },
  "rules": {
    "indent": ["error", 2, { "SwitchCase": 1 }],
    "linebreak-style": ["error", "unix"],
    "no-console": 0,
    "quotes": ["error", "single"],
    "semi": ["error", "always"],
    "multiline-ternary": ["error", "always"]
  }
}

如果你告诉ESLint 环境是 jest test()并且expect()不会被认为是未定义的。 因为 jest 的默认测试文件是"**/*.spec.js", "**/__mocks__/**"我建议你添加

.eslintrc
{
  // your configuration
  "overrides": [
    {
      "files": ["**/*.spec.js", "**/__mocks__/**"],
      "env": {
        "jest": true
      }
    }
  ]

编辑:您可以在全局环境中添加"jest": true ,但缺点是如果您不小心使用了一些 jest 的全局函数,例如describetestitbeforeAll等。 ESLint 不会报告错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM