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

[v3] workspace in config file not working #7325

Closed
6 tasks done
edison1105 opened this issue Jan 21, 2025 · 6 comments
Closed
6 tasks done

[v3] workspace in config file not working #7325

edison1105 opened this issue Jan 21, 2025 · 6 comments

Comments

@edison1105
Copy link

Describe the bug

configure different environments for different files in a single vitest.config.ts file.
The configuration file is as follows:

import { defineConfig } from 'vitest/config';

export default defineConfig({
  test: {
    name: 'client',
    setupFiles: './vitest.setup.ts',
    workspace: [
      {
        test: {
          include: ['**/*.tsx'],
          environment: 'jsdom',
        },
      },
      {
        test: {
          include: ['**/*.ts'],
          environment: 'node',
        },
      },
    ],
  },
});
  • example with environmentMatchGlobs works, see link
  • example with workspace does not work, see link

Since environmentMatchGlobs has been deprecated, is there a way to set different environments for different files within a single vitest.config.ts file without adding additional configuration files?

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-szru14k5?file=packages/client/vitest.config.ts,packages/client/test/test.ts,packages/client/test/basic.test.tsx&initialPath=__vitest__/

System Info

System:
    OS: macOS 14.5
    CPU: (8) arm64 Apple M1
    Memory: 152.63 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.18.2 - /usr/local/bin/node
    npm: 9.8.1 - /usr/local/bin/npm
    pnpm: 9.15.4 - /usr/local/bin/pnpm
    bun: 1.0.0 - /usr/local/bin/bun
  Browsers:
    Chrome: 132.0.6834.84
    Safari: 17.5

Used Package Manager

pnpm

Validations

@sheremet-va sheremet-va added p4-important Violate documented behavior or significantly improves performance (priority) and removed pending triage p4-important Violate documented behavior or significantly improves performance (priority) labels Jan 21, 2025
@sheremet-va
Copy link
Member

This is not a bug. Vitest runs tests with vitest.workspace.ts in the root, the inline workspace field is ignored because it can only be set in the top level config. You can use defineProject config instead to see if you are using unsupported field.

@edison1105
Copy link
Author

edison1105 commented Jan 21, 2025

If nested workspace are not working, how should I configure them to make them function properly? The documentation doesn't provide a clear solution.
I tried migrating environmentMatchGlobs to the workspace, but couldn't find complete documentation on how to configure different environments for different files in a single config file.

@sheremet-va
Copy link
Member

Every setup is unique. In you example you need to configure these options in the root config/workspace file.

// vitest.workspace.ts
import { defineWorkspace } from 'vitest/config'

export default defineWorkspace([
  'packages/server',
  {
    extends: 'packages/client/vitest.config.ts',
    root: './packages/client',
    test: { environment: 'node', name: 'node' },
  },
  {
    extends: 'packages/client/vitest.config.ts',
    root: './packages/client',
    test: { environment: 'jsdom', name: 'jsdom' },
  },
])

Just remember that Vitest will respect only one workspace.

@sheremet-va
Copy link
Member

It does seem like a useful feature to allow inline workspaces for first level projects though. Maybe we can support it

@pdanpdan
Copy link

And the reporters and coverage settings that were in vite.config where should be placed?

The part in the docs is not very clear (https://vitest.dev/guide/workspace)

Should we put them in the root of the workspace in a vitest.config.ts?

@edison1105
Copy link
Author

edison1105 commented Jan 22, 2025

@sheremet-va

First of all, thank you for your reply, but I don't think this configuration is good. This adds an additional test configuration, and it also requires modifying the scripts in package.json, "test:unit": "vitest --project=node --project=jsdom" originally only needed "test:unit": "vitest --project=unit", and there might be many --project=xxx in the future. In my opinion, whether unit tests depend on the node or jsdom, they should only have one project name called unit, rather than being distinguished based on the dependency environment.

Additionally, the documentation here is not clear https://vitest.dev/config/#environmentmatchglobs, it's just that both @pdanpdan and I made a mistake. I believe there will be more people making the same mistake.

It does seem like a useful feature to allow inline workspaces for first level projects though. Maybe we can support it.

Will this feature be considered for support in the 3.x version? I have already upgraded vitest in vuejs/core to v3, and this issue was encountered while migrating environmentMatchGlobs to the workspace. see vuejs/core#12759. If this feature can be supported in the 3.x version, I will wait for it.

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

No branches or pull requests

3 participants