Search code examples
unit-testingvisual-studio-codevitest

How do I configure VSCode to offer vitest auto imports?


In VSCode, I have a very small project with vite and vitest installed. With vitest, unlike with jasmine or jest, you need to explicitly import symbols like describe, it, expect etc.

Unfortunately, VS Code does not even offer auto-import (cmd + .):

VSCode after pressing cmd+.

I have Vitest extension installed if that matters (VSCode is not my primary IDE, so I might be missing something).

How do I configure my environment so that I can easily import those functions?


Solution

  • Setting up automatic imports for Vitest may help. Try to do the following

    • Open VSCode settings
    • Search for "Auto Import" in the search bar
    • Scroll down to the "JavaScript › Auto Import: Custom" section and click on the "Edit in settings.json".
    • Add the following config:

    "javascript.suggest.autoImports": true,
    "javascript.preferences.importModuleSpecifier": "relative", "javascript.preferences.importModuleSpecifiers": { "@vitest": "./node_modules/vitest/src" }

    • Save and restart vscode