Skip to content

Commit 7aa9377

Browse files
authored
fix: don't resolve setupFiles from parent directory (#9960)
1 parent 36a6fd8 commit 7aa9377

File tree

12 files changed

+93
-2
lines changed

12 files changed

+93
-2
lines changed

packages/vitest/src/node/config/resolveConfig.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import crypto from 'node:crypto'
1313
import { pathToFileURL } from 'node:url'
1414
import { slash, toArray } from '@vitest/utils/helpers'
1515
import { resolveModule } from 'local-pkg'
16-
import { normalize, relative, resolve } from 'pathe'
16+
import { join, normalize, relative, resolve } from 'pathe'
1717
import c from 'tinyrainbow'
1818
import { mergeConfig } from 'vite'
1919
import {
@@ -29,8 +29,17 @@ import { BaseSequencer } from '../sequencers/BaseSequencer'
2929
import { RandomSequencer } from '../sequencers/RandomSequencer'
3030

3131
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
3241
return normalize(
33-
/* @__PURE__ */ resolveModule(path, { paths: [root] })
42+
/* @__PURE__ */ resolveModule(path, { paths: [join(root, '/')] })
3443
?? resolve(root, path),
3544
)
3645
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { test, expect } from "vitest";
2+
3+
test("basic", () => {
4+
expect((globalThis as any).__testSetupResolve).toBe("ok");
5+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(globalThis as any).__testSetupResolve = "ok";
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { defineConfig } from "vitest/config";
2+
3+
export default defineConfig({
4+
test: {
5+
setupFiles: ["setup.ts"],
6+
},
7+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { test, expect } from "vitest";
2+
3+
test("basic", () => {
4+
expect((globalThis as any).__testSetupResolve).toBe("ok");
5+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(globalThis as any).__testSetupResolve = "ok";
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { defineConfig } from "vitest/config";
2+
3+
export default defineConfig({
4+
test: {
5+
setupFiles: ["./setup"],
6+
},
7+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { test, expect } from "vitest";
2+
3+
test("basic", () => {
4+
expect((globalThis as any).__testSetupResolve).toBe("ok");
5+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(globalThis as any).__testSetupResolve = "ok";
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { defineConfig } from "vitest/config";
2+
3+
export default defineConfig({
4+
test: {
5+
setupFiles: ["./setup.ts"],
6+
},
7+
});

0 commit comments

Comments
 (0)