Eslint angular and jasmine: is not defined no-undef

夙愿已清 提交于 2021-02-11 16:51:27

问题


I am trying to lint my angular code using angular, eslint:recommended and the jasmine plugin recommended settings. However I get is not defined errors on the jasmine stuff. My eslint file looks like this:

{
  "plugins": ["jasmine"],
  "extends": ["angular", "eslint:recommended", 
  "plugin:jasmine/recommended"],
  "rules": {
   "quotes": [ 2, "single"],
   "strict": [2, "global"],
   "semi": ["error", "always"],
   "angular/on-watch": "warn"
  }
}

I get the following errors:

3:1 error 'describe' is not defined no-undef

4:5 error 'it' is not defined no-undef

5:9 error 'expect' is not defined no-undef

7:5 error 'it' is not defined no-undef

8:9 error 'expect' is not defined no-undef

And in my package.json I have version 2.3.0 for eslint and 1.8.1 for eslint-plugin-jasmine. I also have version 0.5.0 for eslint-config-angular and 1.0.0 for eslint-plugin-angular.

I have also tried without specifying the "plugins": ["jasmine"] in my eslint file but then I get an error telling me the jasmine rules are not defined (eg Definition for rule 'jasmine/no-focused-tests' was not found).


回答1:


Adding

"env": {
 "jasmine": true
}

solved the problem. This was the suggestion that i got through the github page of the eslint jasmine plugin.




回答2:


With this rule set, any variable non explicitly declared causes a warning. You can set at the top of your spec file:

/* global describe it expect */



回答3:


You can add /* eslint-env jasmine */ to the top of your .spec.ts files. This is basically the same solution as the one proposed by Geert, but with a per-file solution. The subtle difference is that it will still be an error to use some of the globally defined test methods like describe() and it() in your non-test files.

(There is probably a clever way to set up ESLint, so that you don't have add this line to all the .spec.ts files.)



来源:https://stackoverflow.com/questions/60764680/can-i-import-a-library-so-that-its-functions-are-available-as-globals-in-one-fi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!