23

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).

Community
  • 1
  • 1
Geert Olaerts
  • 1,155
  • 2
  • 9
  • 17
  • And for *angular* use a global, like the answer https://stackoverflow.com/a/41552223/1038726 – None Jul 05 '22 at 21:04

4 Answers4

45

Adding

"env": {
 "jasmine": true
}

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

Geert Olaerts
  • 1,155
  • 2
  • 9
  • 17
7

The way I found to configure the validations for all Jasmine functions and do it in a single place, but still flag them when they are used in a "non-spec" file is as follows.

Have the common configuration defined at config level. Then, for those specific configurations (such as Typescript or Jasmin) do an override. That way, the "global configuration" is applied only to specific files.

{
    "env": {
        "jasmine": true
    },
    "files": ["**/*.spec.js"]
}

Fragment of .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": {...}
};
gian1200
  • 3,670
  • 2
  • 30
  • 59
3

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 */
Bruno Garcia
  • 6,029
  • 3
  • 25
  • 38
  • 2
    Hej, that works. But i think it is quite ugly to put that line in all my test files. I thought that would have been something the jasmine plugin fixed? Is there another way so that i put this in one spot and it is applied to all my spec files? – Geert Olaerts Apr 30 '16 at 10:23
  • The rule is doing its job: warning you the usage of a global variable. You can disable this rule when linting your .spec files (run eslint with separate settings for .spec and code. Or Don't use linting at all on your spec files. Or disable this rule altogether. – Bruno Garcia Apr 30 '16 at 12:09
2

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.)

Jan Aagaard
  • 10,940
  • 8
  • 45
  • 80