From 1aecd650e221e83c0df9a13a1e3ac6687c0bf932 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Fri, 16 Feb 2024 22:37:55 +0900 Subject: [PATCH] fix(vitest): fix `optimizeDeps.disabled` warnings on Vite 5.1 (#5215) --- packages/vitest/src/node/plugins/utils.ts | 13 ++++- pnpm-lock.yaml | 66 ++++++++++++++--------- test/optimize-deps/dep-url/index.js | 1 + test/optimize-deps/dep-url/package.json | 5 ++ test/optimize-deps/package.json | 12 +++++ test/optimize-deps/test/ssr.test.ts | 12 +++++ test/optimize-deps/test/web.test.ts | 10 ++++ test/optimize-deps/vitest.config.ts | 40 ++++++++++++++ 8 files changed, 132 insertions(+), 27 deletions(-) create mode 100644 test/optimize-deps/dep-url/index.js create mode 100644 test/optimize-deps/dep-url/package.json create mode 100644 test/optimize-deps/package.json create mode 100644 test/optimize-deps/test/ssr.test.ts create mode 100644 test/optimize-deps/test/web.test.ts create mode 100644 test/optimize-deps/vitest.config.ts diff --git a/packages/vitest/src/node/plugins/utils.ts b/packages/vitest/src/node/plugins/utils.ts index d399217bc955..cb4495c890e8 100644 --- a/packages/vitest/src/node/plugins/utils.ts +++ b/packages/vitest/src/node/plugins/utils.ts @@ -13,7 +13,7 @@ export function resolveOptimizerConfig(_testOptions: DepsOptimizationOptions | u if (!allowed && testOptions?.enabled === true) console.warn(`Vitest: "deps.optimizer" is only available in Vite >= 4.3.2, current Vite version: ${viteVersion}`) else - // enable by default + // disabled by default testOptions.enabled ??= false if (!allowed || testOptions?.enabled !== true) { newConfig.cacheDir = undefined @@ -50,6 +50,17 @@ export function resolveOptimizerConfig(_testOptions: DepsOptimizationOptions | u include, } } + + // `optimizeDeps.disabled` is deprecated since v5.1.0-beta.1 + // https://github.com/vitejs/vite/pull/15184 + if (major >= 5 && minor >= 1) { + if (newConfig.optimizeDeps.disabled) { + newConfig.optimizeDeps.noDiscovery = true + newConfig.optimizeDeps.include = [] + } + delete newConfig.optimizeDeps.disabled + } + return newConfig } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index dc49060037de..3de9dffa5223 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1839,6 +1839,15 @@ importers: specifier: workspace:* version: link:../../packages/vitest + test/optimize-deps: + devDependencies: + '@vitest/test-dep-url': + specifier: file:./dep-url + version: file:test/optimize-deps/dep-url + vitest: + specifier: workspace:* + version: link:../../packages/vitest + test/path-resolution: devDependencies: '@edge-runtime/vm': @@ -5756,7 +5765,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.11.18 + '@types/node': 20.11.19 chalk: 4.1.2 jest-message-util: 27.5.1 jest-util: 27.5.1 @@ -5777,7 +5786,7 @@ packages: '@jest/test-result': 27.5.1 '@jest/transform': 27.5.1 '@jest/types': 27.5.1 - '@types/node': 20.11.18 + '@types/node': 20.11.19 ansi-escapes: 4.3.2 chalk: 4.1.2 emittery: 0.8.1 @@ -5814,7 +5823,7 @@ packages: dependencies: '@jest/fake-timers': 27.5.1 '@jest/types': 27.5.1 - '@types/node': 20.11.18 + '@types/node': 20.11.19 jest-mock: 27.5.1 dev: true @@ -5831,7 +5840,7 @@ packages: dependencies: '@jest/types': 27.5.1 '@sinonjs/fake-timers': 8.1.0 - '@types/node': 20.11.18 + '@types/node': 20.11.19 jest-message-util: 27.5.1 jest-mock: 27.5.1 jest-util: 27.5.1 @@ -5860,7 +5869,7 @@ packages: '@jest/test-result': 27.5.1 '@jest/transform': 27.5.1 '@jest/types': 27.5.1 - '@types/node': 20.11.18 + '@types/node': 20.11.19 chalk: 4.1.2 collect-v8-coverage: 1.0.2 exit: 0.1.2 @@ -5991,7 +6000,7 @@ packages: dependencies: '@types/istanbul-lib-coverage': 2.0.6 '@types/istanbul-reports': 3.0.4 - '@types/node': 20.11.18 + '@types/node': 20.11.19 '@types/yargs': 16.0.7 chalk: 4.1.2 dev: true @@ -9107,7 +9116,7 @@ packages: resolution: {integrity: sha512-N7UDG0/xiPQa2D/XrVJXjkWbpqHCd2sBaB32ggRF2l83RhPfamgKGF8gwwqyksS95qUS5ZYF9aF+lLPRlwI2UA==} dependencies: '@types/connect': 3.4.37 - '@types/node': 20.11.18 + '@types/node': 20.11.19 dev: true /@types/braces@3.0.1: @@ -9128,7 +9137,7 @@ packages: /@types/connect@3.4.37: resolution: {integrity: sha512-zBUSRqkfZ59OcwXon4HVxhx5oWCJmc0OtBTK05M+p0dYjgN6iTwIL2T/WbsQZrEsdnwaF9cWQ+azOnpPvIqY3Q==} dependencies: - '@types/node': 20.11.18 + '@types/node': 20.11.19 dev: true /@types/cookie@0.4.1: @@ -9195,7 +9204,7 @@ packages: /@types/express-serve-static-core@4.17.39: resolution: {integrity: sha512-BiEUfAiGCOllomsRAZOiMFP7LAnrifHpt56pc4Z7l9K6ACyN06Ns1JLMBxwkfLOjJRlSf06NwWsT7yzfpaVpyQ==} dependencies: - '@types/node': 20.11.18 + '@types/node': 20.11.19 '@types/qs': 6.9.9 '@types/range-parser': 1.2.6 '@types/send': 0.17.3 @@ -9256,7 +9265,7 @@ packages: /@types/graceful-fs@4.1.8: resolution: {integrity: sha512-NhRH7YzWq8WiNKVavKPBmtLYZHxNY19Hh+az28O/phfp68CF45pMFud+ZzJ8ewnxnC5smIdF3dqFeiSUQ5I+pw==} dependencies: - '@types/node': 20.11.18 + '@types/node': 20.11.19 dev: true /@types/hast@2.3.4: @@ -9416,7 +9425,7 @@ packages: /@types/node-fetch@2.6.7: resolution: {integrity: sha512-lX17GZVpJ/fuCjguZ5b3TjEbSENxmEk1B2z02yoXSK9WMEWRivhdSY73wWMn6bpcCDAOh6qAdktpKHIlkDk2lg==} dependencies: - '@types/node': 20.11.18 + '@types/node': 20.11.19 form-data: 4.0.0 dev: true @@ -9437,8 +9446,8 @@ packages: undici-types: 5.26.5 dev: true - /@types/node@20.11.18: - resolution: {integrity: sha512-ABT5VWnnYneSBcNWYSCuR05M826RoMyMSGiFivXGx6ZUIsXb9vn4643IEwkg2zbEOSgAiSogtapN2fgc4mAPlw==} + /@types/node@20.11.19: + resolution: {integrity: sha512-7xMnVEcZFu0DikYjWOlRq7NTPETrm7teqUT2WkQjrTIkEgUyyGdWsj/Zg8bEJt5TNklzbPD1X3fqfsHw3SpapQ==} dependencies: undici-types: 5.26.5 dev: true @@ -9602,7 +9611,7 @@ packages: /@types/resolve@1.17.1: resolution: {integrity: sha512-yy7HuzQhj0dhGpD8RLXSZWEkLsV9ibvxvi6EiJ3bkqLAO1RGo0WbkWQiwpRlSFymTJRz0d3k5LM3kkx8ArDbLw==} dependencies: - '@types/node': 20.11.18 + '@types/node': 20.11.19 dev: true /@types/resolve@1.20.2: @@ -9620,7 +9629,7 @@ packages: resolution: {integrity: sha512-/7fKxvKUoETxjFUsuFlPB9YndePpxxRAOfGC/yJdc9kTjTeP5kRCTzfnE8kPUKCeyiyIZu0YQ76s50hCedI1ug==} dependencies: '@types/mime': 1.3.4 - '@types/node': 20.11.18 + '@types/node': 20.11.19 dev: true /@types/serve-static@1.15.4: @@ -9628,7 +9637,7 @@ packages: dependencies: '@types/http-errors': 2.0.3 '@types/mime': 3.0.3 - '@types/node': 20.11.18 + '@types/node': 20.11.19 dev: true /@types/set-cookie-parser@2.4.2: @@ -18622,7 +18631,7 @@ packages: '@jest/environment': 27.5.1 '@jest/test-result': 27.5.1 '@jest/types': 27.5.1 - '@types/node': 20.11.18 + '@types/node': 20.11.19 chalk: 4.1.2 co: 4.6.0 dedent: 0.7.0 @@ -18757,7 +18766,7 @@ packages: '@jest/environment': 27.5.1 '@jest/fake-timers': 27.5.1 '@jest/types': 27.5.1 - '@types/node': 20.11.18 + '@types/node': 20.11.19 jest-mock: 27.5.1 jest-util: 27.5.1 jsdom: 16.7.0 @@ -18775,7 +18784,7 @@ packages: '@jest/environment': 27.5.1 '@jest/fake-timers': 27.5.1 '@jest/types': 27.5.1 - '@types/node': 20.11.18 + '@types/node': 20.11.19 jest-mock: 27.5.1 jest-util: 27.5.1 dev: true @@ -18819,7 +18828,7 @@ packages: dependencies: '@jest/types': 27.5.1 '@types/graceful-fs': 4.1.8 - '@types/node': 20.11.18 + '@types/node': 20.11.19 anymatch: 3.1.3 fb-watchman: 2.0.2 graceful-fs: 4.2.11 @@ -18859,7 +18868,7 @@ packages: '@jest/source-map': 27.5.1 '@jest/test-result': 27.5.1 '@jest/types': 27.5.1 - '@types/node': 20.11.18 + '@types/node': 20.11.19 chalk: 4.1.2 co: 4.6.0 expect: 27.5.1 @@ -18939,7 +18948,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.11.18 + '@types/node': 20.11.19 dev: true /jest-pnp-resolver@1.2.3(jest-resolve@27.5.1): @@ -19000,7 +19009,7 @@ packages: '@jest/test-result': 27.5.1 '@jest/transform': 27.5.1 '@jest/types': 27.5.1 - '@types/node': 20.11.18 + '@types/node': 20.11.19 chalk: 4.1.2 emittery: 0.8.1 graceful-fs: 4.2.11 @@ -19065,7 +19074,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.11.18 + '@types/node': 20.11.19 graceful-fs: 4.2.11 dev: true @@ -19116,7 +19125,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.11.18 + '@types/node': 20.11.19 chalk: 4.1.2 ci-info: 3.9.0 graceful-fs: 4.2.11 @@ -19153,7 +19162,7 @@ packages: dependencies: '@jest/test-result': 27.5.1 '@jest/types': 27.5.1 - '@types/node': 20.11.18 + '@types/node': 20.11.19 ansi-escapes: 4.3.2 chalk: 4.1.2 jest-util: 27.5.1 @@ -27617,3 +27626,8 @@ packages: resolution: {directory: test/env-custom/vitest-environment-custom, type: directory} name: vitest-environment-custom dev: true + + file:test/optimize-deps/dep-url: + resolution: {directory: test/optimize-deps/dep-url, type: directory} + name: '@vitest/test-deps-url' + dev: true diff --git a/test/optimize-deps/dep-url/index.js b/test/optimize-deps/dep-url/index.js new file mode 100644 index 000000000000..017d53893833 --- /dev/null +++ b/test/optimize-deps/dep-url/index.js @@ -0,0 +1 @@ +export const importMetaUrl = import.meta.url diff --git a/test/optimize-deps/dep-url/package.json b/test/optimize-deps/dep-url/package.json new file mode 100644 index 000000000000..a7c531b2c9b3 --- /dev/null +++ b/test/optimize-deps/dep-url/package.json @@ -0,0 +1,5 @@ +{ + "name": "@vitest/test-deps-url", + "type": "module", + "exports": "./index.js" +} diff --git a/test/optimize-deps/package.json b/test/optimize-deps/package.json new file mode 100644 index 000000000000..eb990c90e435 --- /dev/null +++ b/test/optimize-deps/package.json @@ -0,0 +1,12 @@ +{ + "name": "@vitest/test-optimize-deps", + "type": "module", + "private": true, + "scripts": { + "test": "vitest" + }, + "devDependencies": { + "@vitest/test-dep-url": "file:./dep-url", + "vitest": "workspace:*" + } +} diff --git a/test/optimize-deps/test/ssr.test.ts b/test/optimize-deps/test/ssr.test.ts new file mode 100644 index 000000000000..dc59d558b84b --- /dev/null +++ b/test/optimize-deps/test/ssr.test.ts @@ -0,0 +1,12 @@ +// @vitest-environment node + +import { expect, test } from 'vitest' + +// @ts-expect-error untyped +import { importMetaUrl } from '@vitest/test-dep-url' + +// TODO: flaky on Windows +// https://github.com/vitest-dev/vitest/pull/5215#discussion_r1492066033 +test.skipIf(process.platform === 'win32')('import.meta.url', () => { + expect(importMetaUrl).toContain('/node_modules/.vitest/deps_ssr/') +}) diff --git a/test/optimize-deps/test/web.test.ts b/test/optimize-deps/test/web.test.ts new file mode 100644 index 000000000000..308e7730640f --- /dev/null +++ b/test/optimize-deps/test/web.test.ts @@ -0,0 +1,10 @@ +// @vitest-environment happy-dom + +import { expect, test } from 'vitest' + +// @ts-expect-error untyped +import { importMetaUrl } from '@vitest/test-dep-url' + +test('import.meta.url', () => { + expect(importMetaUrl).toContain('/node_modules/.vitest/deps/') +}) diff --git a/test/optimize-deps/vitest.config.ts b/test/optimize-deps/vitest.config.ts new file mode 100644 index 000000000000..00426c41fa22 --- /dev/null +++ b/test/optimize-deps/vitest.config.ts @@ -0,0 +1,40 @@ +import { defineConfig } from 'vitest/config' + +export default defineConfig({ + optimizeDeps: { + include: ['@vitest/test-dep-url'], + }, + ssr: { + optimizeDeps: { + include: ['@vitest/test-dep-url'], + }, + }, + test: { + chaiConfig: { + truncateThreshold: 1000, + }, + deps: { + optimizer: { + web: { + enabled: true, + }, + ssr: { + enabled: true, + }, + }, + }, + }, + // use dummy ssrLoadModule to trigger ssr.optimizeDeps. + // this will be unnecessary from Vite 5.1 + // cf. https://github.com/vitejs/vite/pull/15561 + plugins: [ + { + name: 'force-ssr-optimize-deps', + configureServer(server) { + return async () => { + await server.ssrLoadModule('/package.json') + } + }, + }, + ], +})