[READ-ONLY] Mirror of https://github.com/vitest-dev/vitest. Next generation testing framework powered by Vite. vitest.dev
test testing-tools vite
12

Configure Feed

Select the types of activity you want to include in your feed.

fix: don't resolve `setupFiles` from parent directory (#9960)

authored by

Hiroshi Ogawa and committed by
GitHub
(Mar 24, 2026, 9:27 AM +0100) 7aa93777 36a6fd83

+93 -2
+42
test/cli/test/setup-files.test.ts
··· 41 41 expect(stdout).toContain('1 passed') 42 42 }) 43 43 }) 44 + 45 + it('setup files resolution in nested folder', async () => { 46 + const result = await runVitest({ 47 + root: 'fixtures/setup-files-resolve/nested', 48 + }) 49 + expect(result.stderr).toMatchInlineSnapshot(`""`) 50 + expect(result.errorTree()).toMatchInlineSnapshot(` 51 + { 52 + "basic.test.ts": { 53 + "basic": "passed", 54 + }, 55 + } 56 + `) 57 + }) 58 + 59 + it('setup files resolution in nested folder without extension', async () => { 60 + const result = await runVitest({ 61 + root: 'fixtures/setup-files-resolve/nested-no-ext', 62 + }) 63 + expect(result.stderr).toMatchInlineSnapshot(`""`) 64 + expect(result.errorTree()).toMatchInlineSnapshot(` 65 + { 66 + "basic.test.ts": { 67 + "basic": "passed", 68 + }, 69 + } 70 + `) 71 + }) 72 + 73 + it('setup files resolution in nested folder with bare name', async () => { 74 + const result = await runVitest({ 75 + root: 'fixtures/setup-files-resolve/nested-bare', 76 + }) 77 + expect(result.stderr).toMatchInlineSnapshot(`""`) 78 + expect(result.errorTree()).toMatchInlineSnapshot(` 79 + { 80 + "basic.test.ts": { 81 + "basic": "passed", 82 + }, 83 + } 84 + `) 85 + })
+1
test/cli/fixtures/setup-files-resolve/setup.ts
··· 1 + (globalThis as any).__testSetupResolve = "not-this";
+11 -2
packages/vitest/src/node/config/resolveConfig.ts
··· 13 13 import { pathToFileURL } from 'node:url' 14 14 import { slash, toArray } from '@vitest/utils/helpers' 15 15 import { resolveModule } from 'local-pkg' 16 - import { normalize, relative, resolve } from 'pathe' 16 + import { join, normalize, relative, resolve } from 'pathe' 17 17 import c from 'tinyrainbow' 18 18 import { mergeConfig } from 'vite' 19 19 import { ··· 29 29 import { RandomSequencer } from '../sequencers/RandomSequencer' 30 30 31 31 function resolvePath(path: string, root: string) { 32 + // local-pkg (mlly)'s resolveModule("./file", { paths: ["/some/root"] }) tries 33 + // /some/file 34 + // /some/file.js 35 + // /some/root/file 36 + // /some/root/file.js 37 + // etc. 38 + // but we don't want to resolve files from parent directories, 39 + // so we ensure passing "/" suffix such as "/some/root/" 40 + // https://github.com/unjs/mlly/blob/401d42983f6f3a9112658d67b0a92ba4fb1d7efa/src/resolve.ts#L104-L110 32 41 return normalize( 33 - /* @__PURE__ */ resolveModule(path, { paths: [root] }) 42 + /* @__PURE__ */ resolveModule(path, { paths: [join(root, '/')] }) 34 43 ?? resolve(root, path), 35 44 ) 36 45 }
+5
test/cli/fixtures/setup-files-resolve/nested-bare/basic.test.ts
··· 1 + import { test, expect } from "vitest"; 2 + 3 + test("basic", () => { 4 + expect((globalThis as any).__testSetupResolve).toBe("ok"); 5 + });
+1
test/cli/fixtures/setup-files-resolve/nested-bare/setup.ts
··· 1 + (globalThis as any).__testSetupResolve = "ok";
+7
test/cli/fixtures/setup-files-resolve/nested-bare/vitest.config.ts
··· 1 + import { defineConfig } from "vitest/config"; 2 + 3 + export default defineConfig({ 4 + test: { 5 + setupFiles: ["setup.ts"], 6 + }, 7 + });
+5
test/cli/fixtures/setup-files-resolve/nested-no-ext/basic.test.ts
··· 1 + import { test, expect } from "vitest"; 2 + 3 + test("basic", () => { 4 + expect((globalThis as any).__testSetupResolve).toBe("ok"); 5 + });
+1
test/cli/fixtures/setup-files-resolve/nested-no-ext/setup.ts
··· 1 + (globalThis as any).__testSetupResolve = "ok";
+7
test/cli/fixtures/setup-files-resolve/nested-no-ext/vitest.config.ts
··· 1 + import { defineConfig } from "vitest/config"; 2 + 3 + export default defineConfig({ 4 + test: { 5 + setupFiles: ["./setup"], 6 + }, 7 + });
+5
test/cli/fixtures/setup-files-resolve/nested/basic.test.ts
··· 1 + import { test, expect } from "vitest"; 2 + 3 + test("basic", () => { 4 + expect((globalThis as any).__testSetupResolve).toBe("ok"); 5 + });
+1
test/cli/fixtures/setup-files-resolve/nested/setup.ts
··· 1 + (globalThis as any).__testSetupResolve = "ok";
+7
test/cli/fixtures/setup-files-resolve/nested/vitest.config.ts
··· 1 + import { defineConfig } from "vitest/config"; 2 + 3 + export default defineConfig({ 4 + test: { 5 + setupFiles: ["./setup.ts"], 6 + }, 7 + });