vitest-dev / vitest

Next generation testing framework powered by Vite.
https://vitest.dev
MIT License
12.68k stars 1.14k forks source link

How to use alias with vitest workspace #6503

Open andrew-vdb opened 1 week ago

andrew-vdb commented 1 week ago

Clear and concise description of the problem

No documentation.

intellisense is working in my spec file no import error but vitest extension for some reason can not load module with alias

Suggested solution

When using workspace, put the resolve in the "vitest.workspace.ts" !

import { defineWorkspace } from 'vitest/config'
import { resolve } from 'path';
export default defineWorkspace(
    [
        {
            test: {
                include: ['./packageName/**/*.spec.{ts,js}'],
                name: 'packageName',
                alias: {
                    '@': resolve(__dirname, './'),
                }
            }
        }
    ])

Alternative

No response

Additional context

No response

Validations

AriPerkkio commented 4 days ago

You could try something like:

import { defineWorkspace } from 'vitest/config'
import { resolve } from 'path';
export default defineWorkspace(
    [
        {
            test: {
                include: ['./packageName/**/*.spec.{ts,js}'],
                name: 'packageName',
                alias: {
-                    '@': resolve(__dirname, './'),
+                    '@': resolve(__dirname, './packageName'),
                }
            }
        }
    ])

But without seeing rest of your project's code it's impossible to help any further.

Here's also one example that uses alias with workspaces: https://stackblitz.com/~/edit/vitest-sonar-reporter-workspaces?file=packages/server/vitest.config.ts.

github-actions[bot] commented 4 days ago

Hello @andrew-vdb. Please provide a minimal reproduction using a GitHub repository or StackBlitz (you can also use examples). Issues marked with needs reproduction will be closed if they have no activity within 3 days.

andrew-vdb commented 4 days ago

The proposed solution is already working, sorry that i may not be clear enough. The issue is when i googled and reading the doc, no one mention to put alias in defineWorkspace

AriPerkkio commented 4 days ago

Doesn't https://vitest.dev/guide/workspace already cover configuring workspaces? What kind of change would you propose there? It shouldn't be alias specific as we simply cannot cover all possible configuration options there.

andrew-vdb commented 3 days ago

it can be faq or troubleshoot

when following conditions are true

Please add alias in vitest.workspace.ts

worse case they can also find this issue