VS Code showing eslint error but vitest is working. 'vi' is not defined
Asked Answered
T

3

13

I have the following tsconfig configuration:

{
  "include": ["tests/**/*.ts"],
  "exclude": [],
  "compilerOptions": {
    "composite": true,
    "lib": [],
    "skipLibCheck": true,
    "outDir": "lib",
    "types": ["vitest/globals"]
  }
}

As I have defined types for vitest/globals so yarn vitest cmd is working fine and executing the tests cases as well.

But in VS Code its showing me the following error:

vi is not defined

How I can fix or silence this issue in vs-code?

Thruway answered 17/8, 2022 at 16:56 Comment(0)
R
21

I had to add the following to my .eslintrc.json file to fix this issue in a test setup module:

"globals": {
  "vi": true
},

If you're using TypeScript, you should also add the following to your compilerOptions in your tsconfig.json. (You already had this, but I'm mentioning it anyway in case it helps someone else):

"types": ["vitest/globals"]
Ryals answered 21/9, 2022 at 23:9 Comment(0)
B
2

My solution for this problem was to increase the ecmaVersion in the .eslintrc.js file from 2020 to 2022 like this:

"parserOptions": {
  "ecmaVersion": 2022,
}

Since you are using TypeScript, you could also try setting the target to 2022 like this:

"compilerOptions": {
  "target": "ES2022"
}
Bop answered 6/11, 2023 at 11:17 Comment(0)
C
0

Note that the plugin eslint-vitest-plugin was renamed to @vitest/eslint-plugin because the author lost access to his npm account.

In the new package:

  • With the eslint legacy config (.eslintrc.*), this is how to use it:
{
    env: {
        '@vitest/env': true
    },
    extends: ['plugin:@vitest/legacy-recommended'],
    plugins: ['@vitest']
}
  • With the eslint new flat config (eslint.config.js), this is the way:
import vitest from '@vitest/eslint-plugin';
...
{
    files: '**/__tests__/**/*.whatever.[jt]s',
    plugins: {
        vitest
    },
    languageOptions: {
        globals: {
            ...vitest.environments.env.globals
        }
    },
    rules: {
        ...vitest.configs.recommended.rules
    }
}
Calcareous answered 11/8 at 22:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.