Skip to content
TypeScript

test: local test runs use package configs and a clean env (#22714)

Theo Browne8f66e67de35e
diff --git a/apps/desktop/vite.config.ts b/apps/desktop/vite.config.ts
index 65a6e7a2892..c0a77e9a48b 100644
--- a/apps/desktop/vite.config.ts
+++ b/apps/desktop/vite.config.ts
@@ -150,6 +150,9 @@ export default defineConfig({
// The Windows lane runs workspace suites concurrently; filesystem-heavy
// desktop integration tests can exceed Vitest's 5 second default there.
testTimeout: 15_000,
- setupFiles: ["../../packages/shared/src/testing/longTempDir.ts"],
+ setupFiles: [
+ "../../packages/shared/src/testing/longTempDir.ts",
+ "../../packages/shared/src/testing/providerEnv.ts",
+ ],
},
});
diff --git a/apps/mobile/vite.config.ts b/apps/mobile/vite.config.ts
index 8fc51ff74ab..3f66714c688 100644
--- a/apps/mobile/vite.config.ts
+++ b/apps/mobile/vite.config.ts
@@ -20,7 +20,10 @@ export default defineConfig({
name: "unit",
include: ["**/*.test.{ts,tsx,mjs}"],
exclude: ["**/node_modules/**", ...reactTests],
- setupFiles: ["../../packages/shared/src/testing/longTempDir.ts"],
+ setupFiles: [
+ "../../packages/shared/src/testing/longTempDir.ts",
+ "../../packages/shared/src/testing/providerEnv.ts",
+ ],
},
},
{
@@ -30,6 +33,7 @@ export default defineConfig({
include: reactTests,
// Native components mock React Native and render through react-dom in these tests.
environment: "jsdom",
+ setupFiles: ["../../packages/shared/src/testing/providerEnv.ts"],
},
},
],
diff --git a/apps/server/src/testUtils/gitConfig.setup.ts b/apps/server/src/testUtils/gitConfig.setup.ts
index a8139563ff3..19065fae6a9 100644
--- a/apps/server/src/testUtils/gitConfig.setup.ts
+++ b/apps/server/src/testUtils/gitConfig.setup.ts
@@ -1,3 +1,9 @@
+// @effect-diagnostics nodeBuiltinImport:off - runs once at test setup, outside any Effect runtime.
+import * as NodeChildProcess from "node:child_process";
+import * as NodeFS from "node:fs";
+import * as NodePath from "node:path";
+import * as HostProcess from "@t3tools/shared/HostProcess";
+
// Git for Windows installs with core.autocrlf=true, which checks committed LF
// files out as CRLF and breaks every byte-exact content assertion; a signing
// key or a non-default init branch on the developer's machine breaks fixtures
@@ -18,3 +24,24 @@ entries.forEach(([key, value], index) => {
process.env[`GIT_CONFIG_KEY_${existing + index}`] = key;
process.env[`GIT_CONFIG_VALUE_${existing + index}`] = value;
});
+
+// Without Homebrew git, macOS resolves `git` to /usr/bin/git, an xcrun shim that costs
+// about 8 ms per spawn against 3 ms for the real binary. Git-heavy files such as
+// GitVcsDriverCore.test.ts and GitManager.test.ts run twice as fast with the real one.
+if (HostProcess.Platform.defaultValue() === "darwin") {
+ const gitOnPath = (process.env.PATH ?? "")
+ .split(NodePath.delimiter)
+ .map((dir) => NodePath.join(dir, "git"))
+ .find((candidate) => NodeFS.existsSync(candidate));
+ if (gitOnPath === "/usr/bin/git") {
+ try {
+ const realGit = NodeChildProcess.execFileSync("xcrun", ["-f", "git"], {
+ encoding: "utf8",
+ stdio: ["ignore", "pipe", "ignore"],
+ }).trim();
+ if (NodePath.isAbsolute(realGit)) {
+ process.env.PATH = `${NodePath.dirname(realGit)}${NodePath.delimiter}${process.env.PATH}`;
+ }
+ } catch {}
+ }
+}
diff --git a/apps/server/vite.config.ts b/apps/server/vite.config.ts
index 5c6176b28c0..57549bec067 100644
--- a/apps/server/vite.config.ts
+++ b/apps/server/vite.config.ts
@@ -58,8 +58,12 @@ const packExecutableTargets = packExecutableTarget
]
: undefined;
+// The root config's `projects` route repo-root runs to package configs,
+// including this one. Inheriting them would make this config a container.
+const { projects: _rootTestProjects, ...baseTestConfig } = baseConfig.test ?? {};
+
export default mergeConfig(
- baseConfig,
+ { ...baseConfig, test: baseTestConfig },
defineConfig({
run: {
tasks: {
diff --git a/apps/web/vite.config.ts b/apps/web/vite.config.ts
index 8158f66367d..6b91bf21de6 100644
--- a/apps/web/vite.config.ts
+++ b/apps/web/vite.config.ts
@@ -80,7 +80,10 @@ const unitTestProject = {
exclude: ["src/**/*.browser.test.{ts,tsx}"],
hookTimeout: 15_000,
testTimeout: 15_000,
- setupFiles: ["../../packages/shared/src/testing/longTempDir.ts"],
+ setupFiles: [
+ "../../packages/shared/src/testing/longTempDir.ts",
+ "../../packages/shared/src/testing/providerEnv.ts",
+ ],
},
} satisfies TestProjectInlineConfiguration;
diff --git a/packages/client-runtime/vite.config.ts b/packages/client-runtime/vite.config.ts
index 7f4ff74eca2..dbb29b7a4ef 100644
--- a/packages/client-runtime/vite.config.ts
+++ b/packages/client-runtime/vite.config.ts
@@ -5,6 +5,6 @@ export default defineConfig({
test: {
environment: "node",
include: ["src/**/*.test.ts"],
- setupFiles: ["../shared/src/testing/longTempDir.ts"],
+ setupFiles: ["../shared/src/testing/longTempDir.ts", "../shared/src/testing/providerEnv.ts"],
},
});
diff --git a/packages/shared/src/testing/providerEnv.ts b/packages/shared/src/testing/providerEnv.ts
new file mode 100644
index 00000000000..21bf59e52b5
--- /dev/null
+++ b/packages/shared/src/testing/providerEnv.ts
@@ -0,0 +1,22 @@
+// Agent shells carry real provider credentials (CLIProxy sets ANTHROPIC_AUTH_TOKEN and
+// ANTHROPIC_BASE_URL) and the agent's own T3 state. Tests that read the ambient environment
+// then pass or fail by who runs them. Tests that need one of these variables set it
+// themselves. Set T3CODE_TEST_KEEP_PROVIDER_ENV=1 to keep them, for example for a live run.
+const PROVIDER_ENV_PREFIXES = ["ANTHROPIC_", "CLAUDE_", "CODEX_", "OPENAI_", "T3CODE_"];
+
+const KEPT_ENV = new Set([
+ // CI sets these so the transfer budget integration test writes its report for the job summary.
+ "T3CODE_TRANSFER_BUDGET_REPORT_PATH",
+ "T3CODE_TRANSFER_BUDGET_RESULT_PATH",
+ // Opt-in: points the HtmlRender tests at a headless browser, which they skip without.
+ "T3CODE_TEST_HEADLESS_SHELL",
+]);
+
+if (process.env.T3CODE_TEST_KEEP_PROVIDER_ENV !== "1") {
+ for (const key of Object.keys(process.env)) {
+ if (KEPT_ENV.has(key)) continue;
+ if (PROVIDER_ENV_PREFIXES.some((prefix) => key.startsWith(prefix))) {
+ delete process.env[key];
+ }
+ }
+}
diff --git a/vite.config.ts b/vite.config.ts
index 3c66ae3a5d3..61430801adc 100644
--- a/vite.config.ts
+++ b/vite.config.ts
@@ -4,6 +4,30 @@ import * as NodeURL from "node:url";
const DOM_TEST_ENVIRONMENT_ALLOWLIST: Array<string> = [];
+const TEST_EXCLUDE = [
+ "**/.repos/**",
+ "**/.t3/**",
+ "**/node_modules/**",
+ "**/dist/**",
+ "**/dist-electron/**",
+ "**/.{idea,git,cache,output,temp}/**",
+];
+
+// Directories whose tests belong to a package's own test config. desktop also
+// owns the kde-snap-shot tests through its `include`.
+const TEST_CONFIG_PACKAGES = [
+ "apps/desktop",
+ "apps/mobile",
+ "apps/server",
+ "apps/web",
+ "packages/client-runtime",
+];
+
+// `vp test run <file>` from the repo root hands each file to its package's
+// config. Packages that borrow this config (`--config ../../vite.config.ts`) run
+// from their own directory, as CI does, so they keep one plain project.
+const isRepoRootTestRun = process.cwd() === import.meta.dirname;
+
const RESTRICTED_IMPORT_PATHS = [
{
name: "@t3tools/client-runtime",
@@ -56,14 +80,7 @@ export default defineConfig({
},
test: {
environment: "node",
- exclude: [
- "**/.repos/**",
- "**/.t3/**",
- "**/node_modules/**",
- "**/dist/**",
- "**/dist-electron/**",
- "**/.{idea,git,cache,output,temp}/**",
- ],
+ exclude: TEST_EXCLUDE,
hookTimeout: 60_000,
testTimeout: 60_000,
allowOnly: !process.env.CI,
@@ -89,7 +106,28 @@ export default defineConfig({
NodeURL.fileURLToPath(
new URL("./packages/shared/src/testing/longTempDir.ts", import.meta.url),
),
+ NodeURL.fileURLToPath(
+ new URL("./packages/shared/src/testing/providerEnv.ts", import.meta.url),
+ ),
],
+ ...(isRepoRootTestRun
+ ? {
+ projects: [
+ ...TEST_CONFIG_PACKAGES,
+ {
+ extends: true,
+ test: {
+ name: "root",
+ exclude: [
+ ...TEST_EXCLUDE,
+ ...TEST_CONFIG_PACKAGES.map((dir) => `${dir}/**`),
+ "native/kde-snap-shot/**",
+ ],
+ },
+ },
+ ],
+ }
+ : {}),
},
staged: {
"*": "vp fmt --no-error-on-unmatched-pattern",