Skip to content

Commit 8f66e67

Browse files
t3dotggclaude
andauthored
test: local test runs use package configs and a clean env (#22714)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent a1dc856 commit 8f66e67

8 files changed

Lines changed: 114 additions & 13 deletions

File tree

‎apps/desktop/vite.config.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ export default defineConfig({
150150
// The Windows lane runs workspace suites concurrently; filesystem-heavy
151151
// desktop integration tests can exceed Vitest's 5 second default there.
152152
testTimeout: 15_000,
153-
setupFiles: ["../../packages/shared/src/testing/longTempDir.ts"],
153+
setupFiles: [
154+
"../../packages/shared/src/testing/longTempDir.ts",
155+
"../../packages/shared/src/testing/providerEnv.ts",
156+
],
154157
},
155158
});

‎apps/mobile/vite.config.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ export default defineConfig({
2020
name: "unit",
2121
include: ["**/*.test.{ts,tsx,mjs}"],
2222
exclude: ["**/node_modules/**", ...reactTests],
23-
setupFiles: ["../../packages/shared/src/testing/longTempDir.ts"],
23+
setupFiles: [
24+
"../../packages/shared/src/testing/longTempDir.ts",
25+
"../../packages/shared/src/testing/providerEnv.ts",
26+
],
2427
},
2528
},
2629
{
@@ -30,6 +33,7 @@ export default defineConfig({
3033
include: reactTests,
3134
// Native components mock React Native and render through react-dom in these tests.
3235
environment: "jsdom",
36+
setupFiles: ["../../packages/shared/src/testing/providerEnv.ts"],
3337
},
3438
},
3539
],

‎apps/server/src/testUtils/gitConfig.setup.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
// @effect-diagnostics nodeBuiltinImport:off - runs once at test setup, outside any Effect runtime.
2+
import * as NodeChildProcess from "node:child_process";
3+
import * as NodeFS from "node:fs";
4+
import * as NodePath from "node:path";
5+
import * as HostProcess from "@t3tools/shared/HostProcess";
6+
17
// Git for Windows installs with core.autocrlf=true, which checks committed LF
28
// files out as CRLF and breaks every byte-exact content assertion; a signing
39
// key or a non-default init branch on the developer's machine breaks fixtures
@@ -18,3 +24,24 @@ entries.forEach(([key, value], index) => {
1824
process.env[`GIT_CONFIG_KEY_${existing + index}`] = key;
1925
process.env[`GIT_CONFIG_VALUE_${existing + index}`] = value;
2026
});
27+
28+
// Without Homebrew git, macOS resolves `git` to /usr/bin/git, an xcrun shim that costs
29+
// about 8 ms per spawn against 3 ms for the real binary. Git-heavy files such as
30+
// GitVcsDriverCore.test.ts and GitManager.test.ts run twice as fast with the real one.
31+
if (HostProcess.Platform.defaultValue() === "darwin") {
32+
const gitOnPath = (process.env.PATH ?? "")
33+
.split(NodePath.delimiter)
34+
.map((dir) => NodePath.join(dir, "git"))
35+
.find((candidate) => NodeFS.existsSync(candidate));
36+
if (gitOnPath === "/usr/bin/git") {
37+
try {
38+
const realGit = NodeChildProcess.execFileSync("xcrun", ["-f", "git"], {
39+
encoding: "utf8",
40+
stdio: ["ignore", "pipe", "ignore"],
41+
}).trim();
42+
if (NodePath.isAbsolute(realGit)) {
43+
process.env.PATH = `${NodePath.dirname(realGit)}${NodePath.delimiter}${process.env.PATH}`;
44+
}
45+
} catch {}
46+
}
47+
}

‎apps/server/vite.config.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,12 @@ const packExecutableTargets = packExecutableTarget
5858
]
5959
: undefined;
6060

61+
// The root config's `projects` route repo-root runs to package configs,
62+
// including this one. Inheriting them would make this config a container.
63+
const { projects: _rootTestProjects, ...baseTestConfig } = baseConfig.test ?? {};
64+
6165
export default mergeConfig(
62-
baseConfig,
66+
{ ...baseConfig, test: baseTestConfig },
6367
defineConfig({
6468
run: {
6569
tasks: {

‎apps/web/vite.config.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,10 @@ const unitTestProject = {
8080
exclude: ["src/**/*.browser.test.{ts,tsx}"],
8181
hookTimeout: 15_000,
8282
testTimeout: 15_000,
83-
setupFiles: ["../../packages/shared/src/testing/longTempDir.ts"],
83+
setupFiles: [
84+
"../../packages/shared/src/testing/longTempDir.ts",
85+
"../../packages/shared/src/testing/providerEnv.ts",
86+
],
8487
},
8588
} satisfies TestProjectInlineConfiguration;
8689

‎packages/client-runtime/vite.config.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ export default defineConfig({
55
test: {
66
environment: "node",
77
include: ["src/**/*.test.ts"],
8-
setupFiles: ["../shared/src/testing/longTempDir.ts"],
8+
setupFiles: ["../shared/src/testing/longTempDir.ts", "../shared/src/testing/providerEnv.ts"],
99
},
1010
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Agent shells carry real provider credentials (CLIProxy sets ANTHROPIC_AUTH_TOKEN and
2+
// ANTHROPIC_BASE_URL) and the agent's own T3 state. Tests that read the ambient environment
3+
// then pass or fail by who runs them. Tests that need one of these variables set it
4+
// themselves. Set T3CODE_TEST_KEEP_PROVIDER_ENV=1 to keep them, for example for a live run.
5+
const PROVIDER_ENV_PREFIXES = ["ANTHROPIC_", "CLAUDE_", "CODEX_", "OPENAI_", "T3CODE_"];
6+
7+
const KEPT_ENV = new Set([
8+
// CI sets these so the transfer budget integration test writes its report for the job summary.
9+
"T3CODE_TRANSFER_BUDGET_REPORT_PATH",
10+
"T3CODE_TRANSFER_BUDGET_RESULT_PATH",
11+
// Opt-in: points the HtmlRender tests at a headless browser, which they skip without.
12+
"T3CODE_TEST_HEADLESS_SHELL",
13+
]);
14+
15+
if (process.env.T3CODE_TEST_KEEP_PROVIDER_ENV !== "1") {
16+
for (const key of Object.keys(process.env)) {
17+
if (KEPT_ENV.has(key)) continue;
18+
if (PROVIDER_ENV_PREFIXES.some((prefix) => key.startsWith(prefix))) {
19+
delete process.env[key];
20+
}
21+
}
22+
}

‎vite.config.ts‎

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,30 @@ import * as NodeURL from "node:url";
44

55
const DOM_TEST_ENVIRONMENT_ALLOWLIST: Array<string> = [];
66

7+
const TEST_EXCLUDE = [
8+
"**/.repos/**",
9+
"**/.t3/**",
10+
"**/node_modules/**",
11+
"**/dist/**",
12+
"**/dist-electron/**",
13+
"**/.{idea,git,cache,output,temp}/**",
14+
];
15+
16+
// Directories whose tests belong to a package's own test config. desktop also
17+
// owns the kde-snap-shot tests through its `include`.
18+
const TEST_CONFIG_PACKAGES = [
19+
"apps/desktop",
20+
"apps/mobile",
21+
"apps/server",
22+
"apps/web",
23+
"packages/client-runtime",
24+
];
25+
26+
// `vp test run <file>` from the repo root hands each file to its package's
27+
// config. Packages that borrow this config (`--config ../../vite.config.ts`) run
28+
// from their own directory, as CI does, so they keep one plain project.
29+
const isRepoRootTestRun = process.cwd() === import.meta.dirname;
30+
731
const RESTRICTED_IMPORT_PATHS = [
832
{
933
name: "@t3tools/client-runtime",
@@ -56,14 +80,7 @@ export default defineConfig({
5680
},
5781
test: {
5882
environment: "node",
59-
exclude: [
60-
"**/.repos/**",
61-
"**/.t3/**",
62-
"**/node_modules/**",
63-
"**/dist/**",
64-
"**/dist-electron/**",
65-
"**/.{idea,git,cache,output,temp}/**",
66-
],
83+
exclude: TEST_EXCLUDE,
6784
hookTimeout: 60_000,
6885
testTimeout: 60_000,
6986
allowOnly: !process.env.CI,
@@ -89,7 +106,28 @@ export default defineConfig({
89106
NodeURL.fileURLToPath(
90107
new URL("./packages/shared/src/testing/longTempDir.ts", import.meta.url),
91108
),
109+
NodeURL.fileURLToPath(
110+
new URL("./packages/shared/src/testing/providerEnv.ts", import.meta.url),
111+
),
92112
],
113+
...(isRepoRootTestRun
114+
? {
115+
projects: [
116+
...TEST_CONFIG_PACKAGES,
117+
{
118+
extends: true,
119+
test: {
120+
name: "root",
121+
exclude: [
122+
...TEST_EXCLUDE,
123+
...TEST_CONFIG_PACKAGES.map((dir) => `${dir}/**`),
124+
"native/kde-snap-shot/**",
125+
],
126+
},
127+
},
128+
],
129+
}
130+
: {}),
93131
},
94132
staged: {
95133
"*": "vp fmt --no-error-on-unmatched-pattern",

0 commit comments

Comments
 (0)