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

Adding ANY config fails all tests #4937

Closed
thany opened this issue Jan 11, 2024 · 7 comments
Closed

Adding ANY config fails all tests #4937

thany opened this issue Jan 11, 2024 · 7 comments

Comments

@thany
Copy link

thany commented Jan 11, 2024

Describe the bug

Adding any config in vitest.config.ts, will fail every single test. If I leave the file empty or non-existent, tests run normally.

Even exporting an empty config will fail all tests, like this:

Error: Invariant violation: "new TextEncoder().encode("") instanceof Uint8Array" is incorrectly false

This indicates that your JavaScript environment is broken. You cannot use
esbuild in this environment because esbuild relies on this invariant. This
is not a problem with esbuild. You need to fix your environment instead.

 ❯ Object.<anonymous> ../../node_modules/esbuild/lib/main.js:201:9
 ❯ Module._compile node:internal/modules/cjs/loader:1376:14
 ❯ Object.Module._extensions..js node:internal/modules/cjs/loader:1435:10
 ❯ Module.load node:internal/modules/cjs/loader:1207:32
 ❯ Function.Module._load node:internal/modules/cjs/loader:1023:12
 ❯ cjsLoader node:internal/modules/esm/translators:345:17
 ❯ ModuleWrap.<anonymous> node:internal/modules/esm/translators:294:7
 ❯ ModuleJob.run node:internal/modules/esm/module_job:218:25
 ❯ ModuleLoader.import node:internal/modules/esm/loader:329:24
 ❯ VitestExecutor.interopedImport ../../node_modules/vite-node/dist/client.mjs:377:28

Reproduction

Just add config. Sorry but that's literally what I'm doing.

import { defineConfig } from 'vitest/config'

export default defineConfig({
 
});

or this:

import { defineConfig } from 'vitest/config'

export default defineConfig({
  test: {
  }
});

It always makes them fail.

I do also have to have import '@testing-library/jest-dom/vitest'; in there, but that one doesn't do anything with config.

System Info

>npx envinfo --system --npmPackages '{vitest,@vitest/*,vite,@vitejs/*}' --binaries --browsers

  System:
    OS: Windows 10 10.0.19045
    CPU: (20) x64 12th Gen Intel(R) Core(TM) i9-12900H
    Memory: 14.89 GB / 31.68 GB
  Binaries:
    Node: 20.10.0 - C:\Program Files\nodejs\node.EXE
    npm: 10.2.5 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Chrome: 120.0.6099.216
    Edge: Chromium (120.0.2210.121)

All packages are up to date to their latest stable versions.



### Used Package Manager

npm

### Validations

- [X] Follow our [Code of Conduct](https://github.com/vitest-dev/vitest/blob/main/CODE_OF_CONDUCT.md)
- [X] Read the [Contributing Guidelines](https://github.com/vitest-dev/vitest/blob/main/CONTRIBUTING.md).
- [X] Read the [docs](https://vitest.dev/guide/).
- [X] Check that there isn't [already an issue](https://github.com/vitest-dev/vitest/issues) that reports the same bug to avoid creating a duplicate.
- [X] Check that this is a concrete bug. For Q&A open a [GitHub Discussion](https://github.com/vitest-dev/vitest/discussions) or join our [Discord Chat Server](https://chat.vitest.dev).
- [X] The provided reproduction is a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) of the bug.
@sheremet-va
Copy link
Member

This is not what you are literally doing, and you can see that if you literally did that. Please provide a minimal reproduction.

Copy link

Hello @thany. Please provide a minimal reproduction using a GitHub repository or StackBlitz. Issues marked with need reproduction will be closed if they have no activity within 3 days.

@thany
Copy link
Author

thany commented Jan 12, 2024

https://github.com/thany/vitest-error

Make sure you're on Node.js 20.10.
Then run npm i and npm run test on it.

It should succeed.

But then, try uncommenting the config in vitest.setup.ts.

@hi-ogawa
Copy link
Contributor

Hey, thanks for the repro. It looks like you're using defineConfig in setupFiles (vitest.setup.ts), but that's not intended to be used there. You already have configuration in vite.config.ts and you can simply remove defineConfig from vitest.setup.ts.

This documentation might help you understand the difference:

@thany
Copy link
Author

thany commented Jan 23, 2024

I see. Whoops.

But you've got to admit, the error you get is pretty obscure, isn't it? It doesn't look like it has anything to do with where it expects (or doesn't expect) a config to be placed.

On a related note: isn't it possible then, to separate Vite and Vitest configs into different files? Apparently the setup file is not the right place, so does it look for a different one in addition?

@Smrtnyk
Copy link

Smrtnyk commented Jan 23, 2024

On a related note: isn't it possible then, to separate Vite and Vitest configs into different files? Apparently the setup file is not the right place, so does it look for a different one in addition?

its literally in the docs
https://vitest.dev/config/#configuration

@hi-ogawa
Copy link
Contributor

But you've got to admit, the error you get is pretty obscure, isn't it? It doesn't look like it has anything to do with where it expects (or doesn't expect) a config to be placed.

I get that it's obscure but I think that error is happening due to import "vitest/config" (which then importing vite, esbuild and some heavy stuff) inside jsdom environment. Probably import "vite" might even work inside non-jsdom environment, so not much we can do. If this is such a pitfall, then Vitest might try to do something, but so far that doesn't seem the case.

Closing since this is working intended. To separate configs, you can use vite.config.ts and vitest.config.ts as explained in the doc.

@github-actions github-actions bot locked and limited conversation to collaborators Feb 7, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants