vitejs / vite-plugin-react-swc

Speed up your Vite dev server with SWC
MIT License
823 stars 52 forks source link

usage with vitest? #2

Closed evertbouw closed 1 year ago

evertbouw commented 1 year ago

Trying this plugin in a prototype project and I get Error: React refresh preamble was not loaded. Something is wrong. while trying to run vitest. There is no need to have refresh during test ofc so should I just swap for the regular React plugin for test?

this does work

import { swcReactRefresh } from 'vite-plugin-swc-react-refresh';
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';

export default defineConfig((env) => ({
  plugins: [
    env.mode === 'test' ? react() : swcReactRefresh(),
  ],
  esbuild: { jsx: 'automatic' },
  test: {
    globals: true,
    environment: 'jsdom',
    setupFiles: ['./src/setupTests.ts'],
  },
}));
ArnaudBarre commented 1 year ago

I think I should just omit the refresh wrapper in test mode.

The babel plugin always insert the refresh wrapper but inside if (import.meta.hot) { so it's not executed in test mode.

I currently working on adding a caching option, so maybe the second option is simpler. Should be fixed later today

quolpr commented 11 months ago

@ArnaudBarre hey! Could we disable refresh mode but for vitest browser mode? https://vitest.dev/guide/browser.html

ArnaudBarre commented 11 months ago

Should be the case in the latest version I think. @sheremet-va does config.server.hmr === false with browser mode?

sheremet-va commented 11 months ago

Should be the case in the latest version I think. @sheremet-va does config.server.hmr === false with browser mode?

Yes, it should be disabled unless other plugins enable it.