Eslint angular and jasmine:未定义为no-undef

Gee*_*rts 8 jasmine angularjs eslint

我试图使用角度,eslint:推荐和茉莉花插件推荐设置lint我的角度代码.但是我得到的不是茉莉花的错误.我的eslint文件看起来像这样:

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

我收到以下错误:

3:1错误'describe'未定义no-undef

4:5错误'it'​​未定义no-undef

5:9错误'expect'未定义no-undef

7:5错误'it'​​未定义no-undef

8:9错误'expect'未定义no-undef

而在我的package.json我有版本2.3.0eslint1.8.1eslint-plugin-jasmine.我也有0.5.0for eslint-config-angular1.0.0for的版本eslint-plugin-angular.

我也试过没有"plugins": ["jasmine"]在我的eslint文件中指定,但后来我得到一个错误,告诉我茉莉花规则没有定义(例如Definition for rule 'jasmine/no-focused-tests' was not found).

Gee*_*rts 23

添加

"env": {
 "jasmine": true
}
Run Code Online (Sandbox Code Playgroud)

解决了这个问题.这是我通过eslint jasmine插件的github页面的建议.

  • 对于那些对所有配置文件感到困惑的人(就像我一样):设置转到“eslintrc” (2认同)

gia*_*200 7

我发现为所有 Jasmine 函数配置验证并在一个地方执行它,但在“非规范”文件中使用它们时仍然标记它们的方法如下。

在配置级别定义通用配置。然后,对于那些特定配置(例如 Typescript 或 Jasmin)进行覆盖。这样,“全局配置”仅应用于特定文件。

{
    "env": {
        "jasmine": true
    },
    "files": ["**/*.spec.js"]
}
Run Code Online (Sandbox Code Playgroud)

片段.eslintrc.js

/**
 * @type {import("eslint").Linter.Config}
 */
module.exports = {
    "env": {...},
    "extends": [
        "eslint:recommended"
    ],
    "overrides": [
        {
            "extends": [
                "plugin:@typescript-eslint/eslint-recommended",
                "plugin:@typescript-eslint/recommended"
            ],
            "files": ["**/*.ts"],
            "parser": "@typescript-eslint/parser",
            "parserOptions": {
                "project": "tsconfig.json",
                "tsconfigRootDir": __dirname
            },
            "plugins": [
                "@typescript-eslint"
            ]
        },
        // This is the important part
        {
            "env": {
                "jasmine": true
            },
            "files": ["**/*.spec.js"] 
        }
    ],
    "root": true,
    "rules": {...}
};
Run Code Online (Sandbox Code Playgroud)


Bru*_*cia 3

使用此规则集,任何未显式声明的变量都会导致警告。您可以在规范文件的顶部设置:

/* global describe it expect */
Run Code Online (Sandbox Code Playgroud)

  • 嘿,这有效。但我认为将这一行放在我的所有测试文件中是相当难看的。我以为茉莉花插件可以解决这个问题?有没有另一种方法可以让我将其放在一个位置并将其应用于我的所有规范文件? (2认同)