-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
vite
SSR module error when importing isomorphic library within globalSetup
file
#522
Comments
To setup client connections please use setupFiles with You cannot share data or exports directly from globalSetup to test files. In regards to the error it looks like supabase-js may be packaged in a way that vite.ssrLoadModule tries to load its cjs entry as esm |
My understanding is that I don't plan to share any data or exports directly to test files from supabase-js is exporting When I import supabase-js inside my test files, it works without any issue during |
I am facing somewhat similar issue when I import
I have updated the playground for easy reproduction. |
supabase only declares a "module" field in package.json, not an exports map and it also use .js extension for both cjs and mjs instead of using explict .mjs. From a node perspective, supabase is a commonjs package. (module field is only a convention for bundlers, nothing official by node) cross-fetch seems to be cjs too, you may get around that one by using node-fetch (v3 is esm) directly. This seems to be a bug or implementation error that prevents working with cjs packages in globalSetup, I'd like to learn more about your usecase/intentions. The way you're using isomorphic packages in globalSetup - which is only ever run in a node environment - still makes me wonder if you're trying to do something it is not meant for. |
Thanks @dominikg for your reply. About my use case - I am setting up couple of dataset in the database before my test runs begin. I am fetching some of the data from other services using fetch. I would like to do this before any test is run and would like to dismantle them once all tests have run to keep the state cleaned up. |
hmm. Will raise this as an issue to supabase team |
I tried node-fetch (3.1.0) in my playground and got following error.
|
I am just wondering why both |
I'm getting the same error even for simple server startup, like here: import { createServer } from 'vite'
export async function globalSetup() {
const server = await createServer({
root: resolve(__dirname, '..'),
server: {
port: 3000,
},
})
await server.listen()
return async () => {
await server.close()
}
} So probably not related to isomorphic libs? |
It is related to cjs. Even though vite itself is primarily working with es modules, it is itself distributed as cjs. dist/node/index.js 'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var build = require('./chunks/dep-f5552faa.js');
require('fs'); related bug in vite repo: vitejs/vite#2579 |
As a workaround you can use import { createRequire } from 'module'
const require = createRequire(import.meta.url) |
i had implemented it with dynamic import before changing it to ssrLoadModule, there are also issues with that approach and using vite has the benefit of builtin ts support |
@dominikg its working pretty well. Here's the playground where it is working. When I run In general, is it expected to run once for the entire test run or will it run once per worker thread? |
once per run |
can confirm that with --ui globalSetup is executed twice, thats a different issue though. |
reported as #679 |
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.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
Used Package Manager
pnpm
Validations
The text was updated successfully, but these errors were encountered: