Skip to content

🐛 BUG: import issue of packages with workerd runtime key[vitest-pool-workers] #6465

@m-shaka

Description

@m-shaka

Which Cloudflare product(s) does this pertain to?

Workers Vitest Integration

What version(s) of the tool(s) are you using?

0.4.21

What version of Node are you using?

20.11.0

What operating system and version are you using?

Mac Sonoma 14.1.2

Describe the Bug

Observed behavior

Importing packages with workerd runtime key from globalSetup fails.

I tried to use Postgres.js from a globalSetup file to run database migration, but this error was raised.

Error: Only URLs with a scheme in: file, data, and node are supported by the default ESM loader. Received protocol 'cloudflare:'

This is because

As proof of that, if you add this setting to your vitest config, globalSetup works

	ssr: {
		resolve: {
			conditions: ['import']
		}
	}

However, then, tests using Postgres.js fail

 FAIL  test/index.spec.ts [ test/index.spec.ts ]
Error: No such module "Users/xxx/repro-vitest-pool-workers-import-error/node_modules/.pnpm/[email protected]/node_modules/postgres/src/os".
  imported from "Users/xxx/repro-vitest-pool-workers-import-error/node_modules/.pnpm/[email protected]/node_modules/postgres/src/index.js"

I guess this is because resolve condition is no longer workerd. Vitest is trying to import files for runtime other than workerd.

Expected behavior

You can import postgres correctly

Steps to reproduce

Please provide the following:

  • run npm create cloudflare@latest and select Hello World example and then Hello World Worker
  • use Postgres.js in a global setup file and a test file
  • run npm run test

Please provide a link to a minimal reproduction

https://github.com/m-shaka/repro-vitest-pool-workers-import-issue

Please provide any relevant error logs

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething that isn't workingvitestRelating to the Workers Vitest integration

    Type

    No type

    Projects

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions