From e340802f68c2d9b54266e60d3028b36a7a20b4bc 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 d3df2af516ac..2cb4f176a81d 100644 --- a/packages/vitest/src/runtime/child.ts +++ b/packages/vitest/src/runtime/child.ts @@ -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 diff --git a/packages/vitest/src/runtime/vm.ts b/packages/vitest/src/runtime/vm.ts index 323249c16408..b302e68398e3 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 a9d7682888c0..07b82bd9d903 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1683,6 +1683,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 @@ -2746,24 +2752,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'} @@ -3125,18 +3113,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'} @@ -4491,16 +4467,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'} @@ -5739,19 +5705,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'} @@ -6192,7 +6145,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): @@ -7237,7 +7192,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 @@ -7258,7 +7213,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 @@ -7295,7 +7250,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 @@ -7312,7 +7267,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 @@ -7341,7 +7296,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 @@ -7465,7 +7420,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 @@ -10462,7 +10417,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: @@ -19132,7 +19087,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 @@ -19268,7 +19223,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 @@ -19286,7 +19241,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 @@ -19330,7 +19285,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 @@ -19370,7 +19325,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 @@ -19450,7 +19405,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): @@ -19511,7 +19466,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 @@ -19576,7 +19531,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 @@ -19627,7 +19582,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 @@ -19664,7 +19619,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') }, } },