Skip to content

Commit

Permalink
fix(vitest): always inline setup files (#6689)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored Oct 11, 2024
1 parent 47dde76 commit 19d64e2
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 21 deletions.
1 change: 1 addition & 0 deletions packages/vite-node/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ function parseServerOptions(
...serverOptions,
deps: {
...serverOptions.deps,
inlineFiles: toArray(serverOptions.deps?.inlineFiles),
inline:
inlineOptions !== true
? inlineOptions.map((dep) => {
Expand Down
3 changes: 3 additions & 0 deletions packages/vite-node/src/externalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
11 changes: 11 additions & 0 deletions packages/vite-node/src/server.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions packages/vite-node/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type Awaitable<T> = T | PromiseLike<T>
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
Expand Down
44 changes: 23 additions & 21 deletions packages/vitest/src/node/config/resolveConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ??= {}

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions test/config/fixtures/inline-setup-file/file-setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
if (typeof __vite_ssr_import__ === 'undefined') {
throw new Error('Setup file was externalized')
}
3 changes: 3 additions & 0 deletions test/config/fixtures/inline-setup-file/test.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { test } from 'vitest'

test('test', () => {})
13 changes: 13 additions & 0 deletions test/config/fixtures/inline-setup-file/vitest.config.js
Original file line number Diff line number Diff line change
@@ -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/]
}
}
}
})
11 changes: 11 additions & 0 deletions test/config/test/inline-setup-file.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
1 change: 1 addition & 0 deletions vitest.workspace.vscode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ import { defineWorkspace } from 'vitest/config'
export default defineWorkspace([
'./test/core',
'./test/cli',
'./test/config',
])

0 comments on commit 19d64e2

Please sign in to comment.