From 39647774560d3d83f287e9871935065bb7f12ad3 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Fri, 29 Sep 2023 09:29:06 +0200 Subject: [PATCH] fix: correctly resolve external dependencies loaded by custom environments (#4196) --- packages/vitest/src/runtime/child.ts | 5 +- packages/vitest/src/runtime/vm.ts | 5 +- packages/vitest/src/runtime/worker.ts | 5 +- pnpm-lock.yaml | 97 +++++-------------- test/env-custom/package.json | 2 + .../vitest-environment-custom/index.ts | 7 ++ 6 files changed, 41 insertions(+), 80 deletions(-) diff --git a/packages/vitest/src/runtime/child.ts b/packages/vitest/src/runtime/child.ts index 446ad4ac5d97..7314444ea3f3 100644 --- a/packages/vitest/src/runtime/child.ts +++ b/packages/vitest/src/runtime/child.ts @@ -54,9 +54,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 diff --git a/packages/vitest/src/runtime/vm.ts b/packages/vitest/src/runtime/vm.ts index 9a9e5bf246f9..6f4ce418d22f 100644 --- a/packages/vitest/src/runtime/vm.ts +++ b/packages/vitest/src/runtime/vm.ts @@ -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) { diff --git a/packages/vitest/src/runtime/worker.ts b/packages/vitest/src/runtime/worker.ts index d90944456110..4111e5c78d5d 100644 --- a/packages/vitest/src/runtime/worker.ts +++ b/packages/vitest/src/runtime/worker.ts @@ -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 diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 8e987c0bfae3..2bd2b2ddd4a9 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1680,6 +1680,12 @@ importers: test/env-custom: devDependencies: + '@types/debug': + specifier: ^4.1.8 + version: 4.1.8 + debug: + specifier: ^4.3.4 + version: 4.3.4(supports-color@8.1.1) vitest: specifier: workspace:* version: link:../../packages/vitest @@ -2743,24 +2749,6 @@ packages: semver: 6.3.1 dev: true - /@babel/helper-create-class-features-plugin@7.22.15(@babel/core@7.22.9): - resolution: {integrity: sha512-jKkwA59IXcvSaiK2UN45kKwSC9o+KuoXsBDvHvU/7BecYIp8GQ2UwrVvFgJASUT+hBnwJx6MhvMCuMzwZZ7jlg==} - engines: {node: '>=6.9.0'} - peerDependencies: - '@babel/core': ^7.0.0 - dependencies: - '@babel/core': 7.22.9 - '@babel/helper-annotate-as-pure': 7.22.5 - '@babel/helper-environment-visitor': 7.22.5 - '@babel/helper-function-name': 7.22.5 - '@babel/helper-member-expression-to-functions': 7.22.15 - '@babel/helper-optimise-call-expression': 7.22.5 - '@babel/helper-replace-supers': 7.22.9(@babel/core@7.22.9) - '@babel/helper-skip-transparent-expression-wrappers': 7.22.5 - '@babel/helper-split-export-declaration': 7.22.6 - semver: 6.3.1 - dev: true - /@babel/helper-create-regexp-features-plugin@7.18.6(@babel/core@7.18.13): resolution: {integrity: sha512-7LcpH1wnQLGrI+4v+nPp+zUvIkF9x0ddv1Hkdue10tg3gmRnLy97DXh4STiOf1qeIInyD69Qv5kKSZzKD8B/7A==} engines: {node: '>=6.9.0'} @@ -3122,18 +3110,6 @@ packages: '@babel/helper-optimise-call-expression': 7.22.5 dev: true - /@babel/helper-replace-supers@7.22.9(@babel/core@7.22.9): - resolution: {integrity: sha512-LJIKvvpgPOPUThdYqcX6IXRuIcTkcAub0IaDRGCZH0p5GPUp7PhRU9QVgFcDDd51BaPkk77ZjqFwh6DZTAEmGg==} - engines: {node: '>=6.9.0'} - peerDependencies: - '@babel/core': ^7.0.0 - dependencies: - '@babel/core': 7.22.9 - '@babel/helper-environment-visitor': 7.22.5 - '@babel/helper-member-expression-to-functions': 7.22.15 - '@babel/helper-optimise-call-expression': 7.22.5 - dev: true - /@babel/helper-simple-access@7.22.5: resolution: {integrity: sha512-n0H99E/K+Bika3++WNL17POvo4rKWZ7lZEp1Q+fStVbUi8nxPQEBOlTmCOxW/0JsS56SKKQ+ojAe2pHKJHN35w==} engines: {node: '>=6.9.0'} @@ -4488,16 +4464,6 @@ packages: '@babel/helper-plugin-utils': 7.22.5 dev: true - /@babel/plugin-syntax-typescript@7.22.5(@babel/core@7.22.9): - resolution: {integrity: sha512-1mS2o03i7t1c6VzH6fdQ3OA8tcEIxwG18zIPRp+UY1Ihv6W+XZzBCVxExF9upussPXJ0xE9XRHwMoNs1ep/nRQ==} - engines: {node: '>=6.9.0'} - peerDependencies: - '@babel/core': ^7.0.0-0 - dependencies: - '@babel/core': 7.22.9 - '@babel/helper-plugin-utils': 7.22.5 - dev: true - /@babel/plugin-transform-arrow-functions@7.18.6(@babel/core@7.18.13): resolution: {integrity: sha512-9S9X9RUefzrsHZmKMbDXxweEH+YlE8JJEuat9FdvW9Qh1cw7W64jELCtWNkPBPX5En45uy28KGvA/AySqUh8CQ==} engines: {node: '>=6.9.0'} @@ -5736,19 +5702,6 @@ packages: '@babel/plugin-syntax-typescript': 7.22.5(@babel/core@7.22.15) dev: true - /@babel/plugin-transform-typescript@7.22.15(@babel/core@7.22.9): - resolution: {integrity: sha512-1uirS0TnijxvQLnlv5wQBwOX3E1wCFX7ITv+9pBV2wKEk4K+M5tqDaoNXnTH8tjEIYHLO98MwiTWO04Ggz4XuA==} - engines: {node: '>=6.9.0'} - peerDependencies: - '@babel/core': ^7.0.0-0 - dependencies: - '@babel/core': 7.22.9 - '@babel/helper-annotate-as-pure': 7.22.5 - '@babel/helper-create-class-features-plugin': 7.22.15(@babel/core@7.22.9) - '@babel/helper-plugin-utils': 7.22.5 - '@babel/plugin-syntax-typescript': 7.22.5(@babel/core@7.22.9) - dev: true - /@babel/plugin-transform-unicode-escapes@7.18.10(@babel/core@7.18.13): resolution: {integrity: sha512-kKAdAI+YzPgGY/ftStBFXTI1LZFju38rYThnfMykS+IXy8BVx+res7s2fxf1l8I35DV2T97ezo6+SGrXz6B3iQ==} engines: {node: '>=6.9.0'} @@ -6189,7 +6142,9 @@ packages: '@babel/core': 7.22.9 '@babel/helper-plugin-utils': 7.22.5 '@babel/helper-validator-option': 7.22.5 - '@babel/plugin-transform-typescript': 7.22.15(@babel/core@7.22.9) + '@babel/plugin-transform-typescript': 7.21.3(@babel/core@7.22.9) + transitivePeerDependencies: + - supports-color dev: true /@babel/register@7.18.9(@babel/core@7.22.9): @@ -7234,7 +7189,7 @@ packages: engines: {node: ^10.13.0 || ^12.13.0 || ^14.15.0 || >=15.0.0} dependencies: '@jest/types': 27.5.1 - '@types/node': 20.6.0 + '@types/node': 18.16.19 chalk: 4.1.2 jest-message-util: 27.5.1 jest-util: 27.5.1 @@ -7255,7 +7210,7 @@ packages: '@jest/test-result': 27.5.1 '@jest/transform': 27.5.1 '@jest/types': 27.5.1 - '@types/node': 20.6.0 + '@types/node': 18.16.19 ansi-escapes: 4.3.2 chalk: 4.1.2 emittery: 0.8.1 @@ -7292,7 +7247,7 @@ packages: dependencies: '@jest/fake-timers': 27.5.1 '@jest/types': 27.5.1 - '@types/node': 20.6.0 + '@types/node': 18.16.19 jest-mock: 27.5.1 dev: true @@ -7309,7 +7264,7 @@ packages: dependencies: '@jest/types': 27.5.1 '@sinonjs/fake-timers': 8.1.0 - '@types/node': 20.6.0 + '@types/node': 18.16.19 jest-message-util: 27.5.1 jest-mock: 27.5.1 jest-util: 27.5.1 @@ -7338,7 +7293,7 @@ packages: '@jest/test-result': 27.5.1 '@jest/transform': 27.5.1 '@jest/types': 27.5.1 - '@types/node': 20.6.0 + '@types/node': 18.16.19 chalk: 4.1.2 collect-v8-coverage: 1.0.1 exit: 0.1.2 @@ -7462,7 +7417,7 @@ packages: dependencies: '@types/istanbul-lib-coverage': 2.0.4 '@types/istanbul-reports': 3.0.1 - '@types/node': 20.6.0 + '@types/node': 18.16.19 '@types/yargs': 16.0.5 chalk: 4.1.2 dev: true @@ -10456,7 +10411,7 @@ packages: /@types/resolve@1.17.1: resolution: {integrity: sha512-yy7HuzQhj0dhGpD8RLXSZWEkLsV9ibvxvi6EiJ3bkqLAO1RGo0WbkWQiwpRlSFymTJRz0d3k5LM3kkx8ArDbLw==} dependencies: - '@types/node': 20.6.0 + '@types/node': 18.16.19 dev: true /@types/resolve@1.20.2: @@ -19126,7 +19081,7 @@ packages: '@jest/environment': 27.5.1 '@jest/test-result': 27.5.1 '@jest/types': 27.5.1 - '@types/node': 20.6.0 + '@types/node': 18.16.19 chalk: 4.1.2 co: 4.6.0 dedent: 0.7.0 @@ -19262,7 +19217,7 @@ packages: '@jest/environment': 27.5.1 '@jest/fake-timers': 27.5.1 '@jest/types': 27.5.1 - '@types/node': 20.6.0 + '@types/node': 18.16.19 jest-mock: 27.5.1 jest-util: 27.5.1 jsdom: 16.7.0 @@ -19280,7 +19235,7 @@ packages: '@jest/environment': 27.5.1 '@jest/fake-timers': 27.5.1 '@jest/types': 27.5.1 - '@types/node': 20.6.0 + '@types/node': 18.16.19 jest-mock: 27.5.1 jest-util: 27.5.1 dev: true @@ -19324,7 +19279,7 @@ packages: dependencies: '@jest/types': 27.5.1 '@types/graceful-fs': 4.1.5 - '@types/node': 20.6.0 + '@types/node': 18.16.19 anymatch: 3.1.2 fb-watchman: 2.0.1 graceful-fs: 4.2.10 @@ -19364,7 +19319,7 @@ packages: '@jest/source-map': 27.5.1 '@jest/test-result': 27.5.1 '@jest/types': 27.5.1 - '@types/node': 20.6.0 + '@types/node': 18.16.19 chalk: 4.1.2 co: 4.6.0 expect: 27.5.1 @@ -19444,7 +19399,7 @@ packages: engines: {node: ^10.13.0 || ^12.13.0 || ^14.15.0 || >=15.0.0} dependencies: '@jest/types': 27.5.1 - '@types/node': 20.6.0 + '@types/node': 18.16.19 dev: true /jest-pnp-resolver@1.2.3(jest-resolve@27.5.1): @@ -19505,7 +19460,7 @@ packages: '@jest/test-result': 27.5.1 '@jest/transform': 27.5.1 '@jest/types': 27.5.1 - '@types/node': 20.6.0 + '@types/node': 18.16.19 chalk: 4.1.2 emittery: 0.8.1 graceful-fs: 4.2.10 @@ -19570,7 +19525,7 @@ packages: resolution: {integrity: sha512-jZCyo6iIxO1aqUxpuBlwTDMkzOAJS4a3eYz3YzgxxVQFwLeSA7Jfq5cbqCY+JLvTDrWirgusI/0KwxKMgrdf7w==} engines: {node: ^10.13.0 || ^12.13.0 || ^14.15.0 || >=15.0.0} dependencies: - '@types/node': 20.6.0 + '@types/node': 18.16.19 graceful-fs: 4.2.10 dev: true @@ -19621,7 +19576,7 @@ packages: engines: {node: ^10.13.0 || ^12.13.0 || ^14.15.0 || >=15.0.0} dependencies: '@jest/types': 27.5.1 - '@types/node': 20.6.0 + '@types/node': 18.16.19 chalk: 4.1.2 ci-info: 3.8.0 graceful-fs: 4.2.10 @@ -19658,7 +19613,7 @@ packages: dependencies: '@jest/test-result': 27.5.1 '@jest/types': 27.5.1 - '@types/node': 20.6.0 + '@types/node': 18.16.19 ansi-escapes: 4.3.2 chalk: 4.1.2 jest-util: 27.5.1 diff --git a/test/env-custom/package.json b/test/env-custom/package.json index f80be0434a0c..3382bbbc5662 100644 --- a/test/env-custom/package.json +++ b/test/env-custom/package.json @@ -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" } diff --git a/test/env-custom/vitest-environment-custom/index.ts b/test/env-custom/vitest-environment-custom/index.ts index e0d82ce17fd7..ca4013fe7a57 100644 --- a/test/env-custom/vitest-environment-custom/index.ts +++ b/test/env-custom/vitest-environment-custom/index.ts @@ -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 { name: 'custom', transformMode: 'ssr', @@ -28,6 +32,9 @@ export default { teardown() { delete global.testEnvironment delete global.option + + if (global.__exists) + log('should not log') }, } },