![](/img/trans.png)
[英]eslint throws `no-undef` errors when linting Jest test files
[英]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 的全局函數,例如describe
、 test
、 it
、 beforeAll
等。 ESLint 不會報告錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.