remix icon indicating copy to clipboard operation
remix copied to clipboard

"Error: Remix Vite plugin can't detect preamble. Something is wrong." when running component tests with Vitest

Open amanape opened this issue 11 months ago • 10 comments

Reproduction

Go to https://stackblitz.com/edit/remix-run-remix-b7fgeb?file=tests%2Fexample.spec.tsx and run npx vitest to see the error.

Regular unit tests run fine, problem appeared when integrating component testing with @testing-library/react (which in turn required me to install happy-dom or jsdom for the testing environment).

Defining the components inside the test do not cause an error.

I was able to trace the issue to the vite.config.ts file, where including any configurations related to vitest will cause it to fail. Moving the vitest configurations to vitest.config.ts and merging the two configuration files as advised by the official documentation also causes the same error.

Temporary Solution

The only (temporary) solution I found was to separate vitest.config.ts and vite.config.ts, then everything works as expected. But that is not a good solution as according to the official documentation, overrides the vite file.

System Info

System:
    OS: macOS 14.2.1
    CPU: (8) arm64 Apple M1
    Memory: 116.03 MB / 8.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.11.0 - /usr/local/bin/node
    Yarn: 1.22.21 - ~/.yarn/bin/yarn
    npm: 10.3.0 - /opt/homebrew/bin/npm
    pnpm: 8.15.1 - /opt/homebrew/bin/pnpm
    bun: 1.0.29 - ~/.bun/bin/bun
    Watchman: 2024.01.22.00 - /opt/homebrew/bin/watchman
  Browsers:
    Brave Browser: 120.1.61.109
    Safari: 17.2.1
  npmPackages:
    @remix-run/dev: ^2.8.0 => 2.8.0 
    @remix-run/node: ^2.8.0 => 2.8.0 
    @remix-run/react: ^2.8.0 => 2.8.0 
    @remix-run/serve: ^2.8.0 => 2.8.0 
    @remix-run/testing: ^2.8.0 => 2.8.0 
    vite: ^5.1.0 => 5.1.5

Used Package Manager

npm

Expected Behavior

Tests should work as expecting without throwing

Actual Behavior

Test fail because of reasons unrelated to tests

amanape avatar Mar 05 '24 16:03 amanape

It ran into the same error too while trying to run react component tests

martialanouman avatar Mar 13 '24 17:03 martialanouman

I am also having this exact error.

CharlesErrington avatar Mar 16 '24 14:03 CharlesErrington

Any update on this ?

martialanouman avatar Mar 19 '24 08:03 martialanouman

@martialanouman

The only (temporary) solution I found was to separate vitest.config.ts and vite.config.ts, then everything works as expected. But that is not a good solution as according to the official documentation, overrides the vite file.

amanape avatar Mar 19 '24 14:03 amanape

Thanks @amanape

I saw that and use it for now, but that doesn't feel clean

martialanouman avatar Mar 19 '24 19:03 martialanouman

Currently we recommend that the remix plugin is omitted when running with Vitest. There's a bit of work to do on our end to figure out how best to integrate with tools like Vitest and Storybook so that this sort of workaround isn't required.

pcattori avatar Apr 18 '24 16:04 pcattori

If I did the conditional logic of process.env.VITEST && remix() I would get a new error in some of my tests:

ReferenceError: React is not defined

If you were on a Vite React SPA previously, I'm finding that you would need to keep your @vitejs/plugin-react around just for vitest:

import reactVitest from '@vitejs/plugin-react'
import { vitePlugin as remix } from '@remix-run/dev';
import { defineConfig, loadEnv } from "vite";

export default defineConfig({
  plugins: [process.env.VITEST ? reactVitest() : remix()],
  // rest of the config
});

yegorkay avatar May 10 '24 15:05 yegorkay

Currently we recommend that the remix plugin is omitted when running with Vitest. There's a bit of work to do on our end to figure out how best to integrate with tools like Vitest and Storybook so that this sort of workaround isn't required.

This generally works well, but I'm facing some issues when trying to test remix routes using @remix-run/testing. Basically, no matter what I pass, when rendering the RemixStub, it always results in a 404 error page.

Could that be a side effect of not loading the remix vite plugin while running tests with vite?

acelaya avatar May 21 '24 07:05 acelaya

Currently we recommend that the remix plugin is omitted when running with Vitest. There's a bit of work to do on our end to figure out how best to integrate with tools like Vitest and Storybook so that this sort of workaround isn't required.

This generally works well, but I'm facing some issues when trying to test remix routes using @remix-run/testing. Basically, no matter what I pass, when rendering the RemixStub, it always results in a 404 error page.

Could that be a side effect of not loading the remix vite plugin while running tests with vite?

Currently facing the same issue. The "Remix Vite plugin can't detect preamble. Something is wrong." was solved. However now my tests using createRemixStub always render 404 error page which makes them fail.

Any solution?

R0BB3RT avatar Jun 21 '24 09:06 R0BB3RT

I am running into this exact error with a fresh remix app which attempts to use vitest with jsdom. I have another project which uses React Router which I want to port over to remix. While the app works, the tests cannot due to this issue and I am blocked.

Reproduction

https://github.com/brianbancroft/remix-cant-use-vitest

brianbancroft avatar Aug 06 '24 20:08 brianbancroft