fix: normalize scoped vitest filter paths

This commit is contained in:
Peter Steinberger
2026-03-22 23:30:16 -07:00
parent dda347eda3
commit 2467fa4c5b
2 changed files with 55 additions and 3 deletions
+22
View File
@@ -1,6 +1,7 @@
import { describe, expect, it } from "vitest";
import channelsConfig from "../vitest.channels.config.ts";
import extensionsConfig from "../vitest.extensions.config.ts";
import gatewayConfig from "../vitest.gateway.config.ts";
import { createScopedVitestConfig, resolveVitestIsolation } from "../vitest.scoped-config.ts";
describe("resolveVitestIsolation", () => {
@@ -26,6 +27,17 @@ describe("createScopedVitestConfig", () => {
dir: "src",
});
expect(config.test?.dir).toBe("src");
expect(config.test?.include).toEqual(["example.test.ts"]);
});
it("relativizes scoped include and exclude patterns to the configured dir", () => {
const config = createScopedVitestConfig(["extensions/**/*.test.ts"], {
dir: "extensions",
exclude: ["extensions/channel/**", "dist/**"],
});
expect(config.test?.include).toEqual(["**/*.test.ts"]);
expect(config.test?.exclude).toEqual(expect.arrayContaining(["channel/**", "dist/**"]));
});
});
@@ -37,4 +49,14 @@ describe("scoped vitest configs", () => {
it("defaults extension tests to non-isolated mode", () => {
expect(extensionsConfig.test?.isolate).toBe(false);
});
it("normalizes extension include patterns relative to the scoped dir", () => {
expect(extensionsConfig.test?.dir).toBe("extensions");
expect(extensionsConfig.test?.include).toEqual(["**/*.test.ts"]);
});
it("normalizes gateway include patterns relative to the scoped dir", () => {
expect(gatewayConfig.test?.dir).toBe("src/gateway");
expect(gatewayConfig.test?.include).toEqual(["**/*.test.ts"]);
});
});
+33 -3
View File
@@ -1,6 +1,32 @@
import { defineConfig } from "vitest/config";
import baseConfig from "./vitest.config.ts";
function normalizePathPattern(value: string): string {
return value.replaceAll("\\", "/");
}
function relativizeScopedPattern(value: string, dir: string): string {
const normalizedValue = normalizePathPattern(value);
const normalizedDir = normalizePathPattern(dir).replace(/\/+$/u, "");
if (!normalizedDir) {
return normalizedValue;
}
if (normalizedValue === normalizedDir) {
return ".";
}
const prefix = `${normalizedDir}/`;
return normalizedValue.startsWith(prefix)
? normalizedValue.slice(prefix.length)
: normalizedValue;
}
function relativizeScopedPatterns(values: string[], dir?: string): string[] {
if (!dir) {
return values.map(normalizePathPattern);
}
return values.map((value) => relativizeScopedPattern(value, dir));
}
export function resolveVitestIsolation(
env: Record<string, string | undefined> = process.env,
): boolean {
@@ -32,15 +58,19 @@ export function createScopedVitestConfig(
};
}
).test ?? {};
const exclude = [...(baseTest.exclude ?? []), ...(options?.exclude ?? [])];
const scopedDir = options?.dir;
const exclude = relativizeScopedPatterns(
[...(baseTest.exclude ?? []), ...(options?.exclude ?? [])],
scopedDir,
);
return defineConfig({
...base,
test: {
...baseTest,
isolate: resolveVitestIsolation(),
...(options?.dir ? { dir: options.dir } : {}),
include,
...(scopedDir ? { dir: scopedDir } : {}),
include: relativizeScopedPatterns(include, scopedDir),
exclude,
...(options?.pool ? { pool: options.pool } : {}),
...(options?.passWithNoTests !== undefined