From 19d64e20efc248aaaee78c51e334c2b35ebe3a98 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Fri, 11 Oct 2024 17:03:33 +0200 Subject: [PATCH] fix(vitest): always inline setup files (#6689) --- packages/vite-node/src/cli.ts | 1 + packages/vite-node/src/externalize.ts | 3 ++ packages/vite-node/src/server.ts | 11 +++++ packages/vite-node/src/types.ts | 1 + .../vitest/src/node/config/resolveConfig.ts | 44 ++++++++++--------- .../fixtures/inline-setup-file/file-setup.js | 3 ++ .../fixtures/inline-setup-file/test.test.js | 3 ++ .../inline-setup-file/vitest.config.js | 13 ++++++ test/config/test/inline-setup-file.test.ts | 11 +++++ vitest.workspace.vscode.ts | 1 + 10 files changed, 70 insertions(+), 21 deletions(-) create mode 100644 test/config/fixtures/inline-setup-file/file-setup.js create mode 100644 test/config/fixtures/inline-setup-file/test.test.js create mode 100644 test/config/fixtures/inline-setup-file/vitest.config.js create mode 100644 test/config/test/inline-setup-file.test.ts diff --git a/packages/vite-node/src/cli.ts b/packages/vite-node/src/cli.ts index f0ae153bee02..5b45ed715149 100644 --- a/packages/vite-node/src/cli.ts +++ b/packages/vite-node/src/cli.ts @@ -182,6 +182,7 @@ function parseServerOptions( ...serverOptions, deps: { ...serverOptions.deps, + inlineFiles: toArray(serverOptions.deps?.inlineFiles), inline: inlineOptions !== true ? inlineOptions.map((dep) => { diff --git a/packages/vite-node/src/externalize.ts b/packages/vite-node/src/externalize.ts index 567f3bfedfcc..ab79b1e5b1c0 100644 --- a/packages/vite-node/src/externalize.ts +++ b/packages/vite-node/src/externalize.ts @@ -119,6 +119,9 @@ async function _shouldExternalize( if (matchExternalizePattern(id, moduleDirectories, options?.inline)) { return false } + if (options?.inlineFiles && options?.inlineFiles.includes(id)) { + return false + } if (matchExternalizePattern(id, moduleDirectories, options?.external)) { return id } diff --git a/packages/vite-node/src/server.ts b/packages/vite-node/src/server.ts index 09256d2b1175..e5120a6f5826 100644 --- a/packages/vite-node/src/server.ts +++ b/packages/vite-node/src/server.ts @@ -1,6 +1,7 @@ import { performance } from 'node:perf_hooks' import { existsSync } from 'node:fs' import assert from 'node:assert' +import { pathToFileURL } from 'node:url' import { join, normalize, relative, resolve } from 'pathe' import type { TransformResult, ViteDevServer } from 'vite' import createDebug from 'debug' @@ -105,6 +106,16 @@ export class ViteNodeServer { this.debugger = new Debugger(server.config.root, options.debug!) } + if (options.deps.inlineFiles) { + options.deps.inlineFiles = options.deps.inlineFiles.flatMap((file) => { + if (file.startsWith('file://')) { + return file + } + const resolvedId = resolve(file) + return [resolvedId, pathToFileURL(resolvedId).href] + }) + } + options.deps.moduleDirectories ??= [] const envValue diff --git a/packages/vite-node/src/types.ts b/packages/vite-node/src/types.ts index c63104829437..f784170b4a15 100644 --- a/packages/vite-node/src/types.ts +++ b/packages/vite-node/src/types.ts @@ -9,6 +9,7 @@ export type Awaitable = T | PromiseLike export interface DepsHandlingOptions { external?: (string | RegExp)[] inline?: (string | RegExp)[] | true + inlineFiles?: string[] /** * A list of directories that are considered to hold Node.js modules * Have to include "/" at the start and end of the path diff --git a/packages/vitest/src/node/config/resolveConfig.ts b/packages/vitest/src/node/config/resolveConfig.ts index 51babc9840be..2a567c8703a8 100644 --- a/packages/vitest/src/node/config/resolveConfig.ts +++ b/packages/vitest/src/node/config/resolveConfig.ts @@ -307,6 +307,27 @@ export function resolveConfig( resolved.deps.web.transformCss ??= true resolved.deps.web.transformGlobPattern ??= [] + resolved.setupFiles = toArray(resolved.setupFiles || []).map(file => + resolvePath(file, resolved.root), + ) + resolved.globalSetup = toArray(resolved.globalSetup || []).map(file => + resolvePath(file, resolved.root), + ) + resolved.coverage.exclude.push( + ...resolved.setupFiles.map( + file => + `${resolved.coverage.allowExternal ? '**/' : ''}${relative( + resolved.root, + file, + )}`, + ), + ) + + resolved.forceRerunTriggers = [ + ...resolved.forceRerunTriggers, + ...resolved.setupFiles, + ] + resolved.server ??= {} resolved.server.deps ??= {} @@ -366,6 +387,8 @@ export function resolveConfig( } } + resolved.server.deps.inlineFiles ??= [] + resolved.server.deps.inlineFiles.push(...resolved.setupFiles) resolved.server.deps.moduleDirectories ??= [] resolved.server.deps.moduleDirectories.push( ...resolved.deps.moduleDirectories, @@ -554,27 +577,6 @@ export function resolveConfig( } } - resolved.setupFiles = toArray(resolved.setupFiles || []).map(file => - resolvePath(file, resolved.root), - ) - resolved.globalSetup = toArray(resolved.globalSetup || []).map(file => - resolvePath(file, resolved.root), - ) - resolved.coverage.exclude.push( - ...resolved.setupFiles.map( - file => - `${resolved.coverage.allowExternal ? '**/' : ''}${relative( - resolved.root, - file, - )}`, - ), - ) - - resolved.forceRerunTriggers = [ - ...resolved.forceRerunTriggers, - ...resolved.setupFiles, - ] - if (resolved.diff) { resolved.diff = resolvePath(resolved.diff, resolved.root) resolved.forceRerunTriggers.push(resolved.diff) diff --git a/test/config/fixtures/inline-setup-file/file-setup.js b/test/config/fixtures/inline-setup-file/file-setup.js new file mode 100644 index 000000000000..dd8219aaf8f9 --- /dev/null +++ b/test/config/fixtures/inline-setup-file/file-setup.js @@ -0,0 +1,3 @@ +if (typeof __vite_ssr_import__ === 'undefined') { + throw new Error('Setup file was externalized') +} diff --git a/test/config/fixtures/inline-setup-file/test.test.js b/test/config/fixtures/inline-setup-file/test.test.js new file mode 100644 index 000000000000..0c52ff3b2129 --- /dev/null +++ b/test/config/fixtures/inline-setup-file/test.test.js @@ -0,0 +1,3 @@ +import { test } from 'vitest' + +test('test', () => {}) diff --git a/test/config/fixtures/inline-setup-file/vitest.config.js b/test/config/fixtures/inline-setup-file/vitest.config.js new file mode 100644 index 000000000000..2d02b4ce7bc7 --- /dev/null +++ b/test/config/fixtures/inline-setup-file/vitest.config.js @@ -0,0 +1,13 @@ +import { defineConfig } from 'vitest/config'; + +export default defineConfig({ + test: { + setupFiles: ['./file-setup'], + server: { + deps: { + // try to force setup file to be external + external: [/file-setup/] + } + } + } +}) diff --git a/test/config/test/inline-setup-file.test.ts b/test/config/test/inline-setup-file.test.ts new file mode 100644 index 000000000000..491590698605 --- /dev/null +++ b/test/config/test/inline-setup-file.test.ts @@ -0,0 +1,11 @@ +import path from 'node:path' +import { expect, test } from 'vitest' +import { runVitest } from '../../test-utils' + +test('import a generated file', async () => { + const root = path.resolve(import.meta.dirname, '../fixtures/inline-setup-file') + + const { stderr, exitCode } = await runVitest({ root }) + expect(stderr).toBe('') + expect(exitCode).toBe(0) +}) diff --git a/vitest.workspace.vscode.ts b/vitest.workspace.vscode.ts index d6a950913e66..cb0d34767ee0 100644 --- a/vitest.workspace.vscode.ts +++ b/vitest.workspace.vscode.ts @@ -3,4 +3,5 @@ import { defineWorkspace } from 'vitest/config' export default defineWorkspace([ './test/core', './test/cli', + './test/config', ])