Skip to content

Commit

Permalink
fix: correctly resolve external dependencies loaded by custom environ…
Browse files Browse the repository at this point in the history
…ments (#4196)
  • Loading branch information
sheremet-va committed Sep 29, 2023
1 parent 730b29e commit e340802
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 80 deletions.
5 changes: 2 additions & 3 deletions packages/vitest/src/runtime/child.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ async function init(ctx: ChildContext) {

const environment = await loadEnvironment(ctx.environment.name, {
root: ctx.config.root,
fetchModule(id) {
return rpc.fetch(id, 'ssr')
},
fetchModule: id => rpc.fetch(id, 'ssr'),
resolveId: (id, importer) => rpc.resolveId(id, importer, 'ssr'),
})
if (ctx.environment.transformMode)
environment.transformMode = ctx.environment.transformMode
Expand Down
5 changes: 2 additions & 3 deletions packages/vitest/src/runtime/vm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ export async function run(ctx: WorkerContext) {

const environment = await loadEnvironment(ctx.environment.name, {
root: ctx.config.root,
fetchModule(id) {
return rpc.fetch(id, 'ssr')
},
fetchModule: id => rpc.fetch(id, 'ssr'),
resolveId: (id, importer) => rpc.resolveId(id, importer, 'ssr'),
})

if (!environment.setupVM) {
Expand Down
5 changes: 2 additions & 3 deletions packages/vitest/src/runtime/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@ async function init(ctx: WorkerContext) {

const environment = await loadEnvironment(ctx.environment.name, {
root: ctx.config.root,
fetchModule(id) {
return rpc.fetch(id, 'ssr')
},
fetchModule: id => rpc.fetch(id, 'ssr'),
resolveId: (id, importer) => rpc.resolveId(id, importer, 'ssr'),
})
if (ctx.environment.transformMode)
environment.transformMode = ctx.environment.transformMode
Expand Down
97 changes: 26 additions & 71 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions test/env-custom/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"coverage": "vitest run --coverage"
},
"devDependencies": {
"@types/debug": "^4.1.8",
"debug": "^4.3.4",
"vitest": "workspace:*",
"vitest-environment-custom": "file:./vitest-environment-custom"
}
Expand Down
7 changes: 7 additions & 0 deletions test/env-custom/vitest-environment-custom/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import vm from 'node:vm'
import debug from 'debug'
import type { Environment } from 'vitest'

// test that external packages (debug) are loaded correctly
const log = debug('test:env')

export default <Environment>{
name: 'custom',
transformMode: 'ssr',
Expand Down Expand Up @@ -28,6 +32,9 @@ export default <Environment>{
teardown() {
delete global.testEnvironment
delete global.option

if (global.__exists)
log('should not log')
},
}
},
Expand Down

0 comments on commit e340802

Please sign in to comment.