Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use alias with vitest workspace #6503

Open
4 tasks done
andrew-vdb opened this issue Sep 14, 2024 · 5 comments
Open
4 tasks done

How to use alias with vitest workspace #6503

andrew-vdb opened this issue Sep 14, 2024 · 5 comments

Comments

@andrew-vdb
Copy link

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
Copy link
Member

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.

Copy link

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
Copy link
Author

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
Copy link
Member

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
Copy link
Author

it can be faq or troubleshoot

when following conditions are true

  • using alias
  • typescript intellisense is working
  • using vitest workspace
  • vitest extensions in vs code give error of failed to import

Please add alias in vitest.workspace.ts

worse case they can also find this issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants