vitest: `vite` SSR module error when importing isomorphic library within `globalSetup` file

Describe the bug

I was trying out this feature and came across this peculiar issue. I am using an isomorphic library supabase-js. When I import it in a file that is sourced by globalSetup, it throws following error.

21:38:19 [vite] Error when evaluating SSR module /node_modules/@supabase/supabase-js/dist/main/index.js:
ReferenceError: exports is not defined
    at eval (/node_modules/@supabase/supabase-js/dist/main/index.js:17:23)
    at instantiateModule (/home/projects/node-3cxxqh/node_modules/vite/dist/node/chunks/dep-76613303.js:60094:15)
21:38:19 [vite] Error when evaluating SSR module ./test/globalSetup.ts:
ReferenceError: exports is not defined
    at eval (/node_modules/@supabase/supabase-js/dist/main/index.js:17:23)
    at instantiateModule (/home/projects/node-3cxxqh/node_modules/vite/dist/node/chunks/dep-76613303.js:60094:15)
(node:6) UnhandledPromiseRejectionWarning: ReferenceError: exports is not defined
    at eval (/node_modules/@supabase/supabase-js/dist/main/index.js:17:23)
    at instantiateModule (/home/projects/node-3cxxqh/node_modules/vite/dist/node/chunks/dep-76613303.js:60094:15)

When I import the same in a normal test file it works fine.

Reproduction

Here’s the playground where the error is reproduced.

System Info

System:
    OS: macOS 12.1
    CPU: (4) x64 Intel(R) Core(TM) i5-6267U CPU @ 2.90GHz
    Memory: 75.13 MB / 16.00 GB
    Shell: 3.3.1 - /usr/local/bin/fish
  Binaries:
    Node: 16.13.1 - /var/folders/n0/qyzlydqj6cv8qt68qd6dbprh0000gn/T/fnm_multishells/47821_1641998710149/bin/node
    npm: 8.1.2 - /var/folders/n0/qyzlydqj6cv8qt68qd6dbprh0000gn/T/fnm_multishells/47821_1641998710149/bin/npm
  Browsers:
    Chrome: 97.0.4692.71
    Chrome Canary: 99.0.4826.0
    Edge: 96.0.1054.43
    Firefox: 95.0.2
    Safari: 15.2

Used Package Manager

pnpm

Validations

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 17 (9 by maintainers)

Most upvoted comments

once per run

@dominikg its working pretty well. Here’s the playground where it is working.

When I run vitest --ui, globalSetup seems to be running twice. However, in vitest run globalsetup runs only once. Could this be due to the way stackblitz is setup?

In general, is it expected to run once for the entire test run or will it run once per worker thread?