Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

vite.config.ts

189 lines8.0 KB

1import * as NodeModule from "node:module";
2import * as NodeURL from "node:url";
3import { defineConfig, normalizePath } from "vite-plus";
4import "vite-plus/test/config";
5​
6const reactTests = [
7 "src/features/browser/LocalPreviewRouteScreen.test.tsx",
8 "src/features/threads/LinkedThreadSheet.test.tsx",
9 "src/features/workflows/WorkflowStepCard.test.tsx",
10 "src/features/threads/ThreadGuestShareSheet.test.tsx",
11 "src/features/threads/useSubmitAfterNativeImages.test.tsx",
12 "src/features/threads/git/GitBranchesSheet.test.tsx",
13 "src/features/threads/git/GitConfirmSheet.test.tsx",
14 "src/features/threads/new-task-flow-provider.test.tsx",
15 "src/features/threads/NewTaskRouteScreen.test.tsx",
16 "src/features/layout/workspace/workspace-tabs-state.test.ts",
17 "src/state/use-thread-pr.demand.test.tsx",
18 "src/features/threads/git/GitBranchesSheet.occupancy.test.tsx",
19 "src/features/files/useFileTreeEntries.test.tsx",
20 "src/features/files/thread-file-navigator-pane.test.tsx",
21 "src/features/files/source-file-document.test.ts",
22 "src/features/projects/AddProjectScreen.test.ts",
23 "src/lib/useMediaVideoPlayback.test.tsx",
24 "src/features/pull-requests/usePullRequestEnvironmentFallback.test.tsx",
25 "src/lib/filePreview.test.ts",
26 "src/features/threads/thread-inspector-navigation.test.tsx",
27 "src/features/diffs/nativeReviewDiffSurface.test.tsx",
28 "src/features/pull-requests/usePullRequestDiff.test.tsx",
29 "src/components/ProjectFavicon.test.tsx",
30 "src/components/ui/Dialog.android.test.tsx",
31 "src/components/ui/ListToggleRow.test.tsx",
32 "src/features/pull-requests/usePullRequestList.test.tsx",
33 "src/components/ui/AppBar.android.test.tsx",
34 "src/components/ui/AndroidAnchoredMenu.android.test.tsx",
35 "src/features/files/ZipArchiveView.test.tsx",
36 "src/features/pull-requests/guide/useReviewGuide.test.tsx",
37 "src/features/settings/SettingsServerControlsRouteScreen.test.tsx",
38 "src/features/settings/SettingsScheduledTasksRouteScreen.test.tsx",
39 "src/features/settings/components/BranchNamingSettings.test.tsx",
40 "src/features/settings/components/SettingsDragHandle.test.tsx",
41 "src/features/threads/ThreadMarkdownImage.test.tsx",
42 "src/components/ui/FilePreviewSession.test.tsx",
43 "src/features/threads/UsageLimitRecoveryCard.test.tsx",
44 "src/features/threads/PendingUserInputCard.test.tsx",
45 "src/features/threads/HtmlRenderWebView.test.tsx",
46 "src/features/threads/NewTaskDraftRouteScreen.test.tsx",
47 "src/native/NativeHeaderToolbar.test.tsx",
48 "src/components/ComposerEditor.ios.test.tsx",
49 "src/native/V5StackHeader.ios.test.tsx",
50 "src/native/createV5StackNavigator.ios.test.tsx",
51 "src/features/layout/useCollapsedSplitColumn.test.tsx",
52 "src/native/useRetryingImageSource.test.tsx",
53 "src/state/queries.debounce.test.tsx",
54 "src/state/use-selected-thread-git-state.test.tsx",
55 "src/state/use-vcs-action-state.test.tsx",
56 "src/features/terminal/useTerminalExitNavigation.test.tsx",
57 "src/features/threads/use-thread-list-now.test.tsx",
58 "src/features/threads/use-composer-command-menu.select.test.tsx",
59 "src/features/threads/useSidebarRowThreadKey.test.tsx",
60 "src/features/threads/ThreadScreenHost.test.tsx",
61 "src/features/threads/useThreadListV2.test.tsx",
62 "src/features/home/home-list-options.test.tsx",
63 "src/features/home/home-route-visibility.test.tsx",
64 "src/features/layout/workspace/useSearchScrollRestoration.test.tsx",
65 "src/features/connection/useRetryEnvironment.test.tsx",
66 "src/features/connection/ConnectionsNewRouteScreen.test.tsx",
67 "src/features/connection/LinkedEnvironmentsSection.test.tsx",
68 "src/features/devices/device-stream.browser.test.ts",
69 "src/features/keyboard/keyboardControllerState.test.tsx",
70 "src/features/voice-input/useVoiceInputController.test.tsx",
71];
72​
73const sourceRoot = `${normalizePath(NodeURL.fileURLToPath(new URL("./src", import.meta.url)))}/`;
74const require = NodeModule.createRequire(import.meta.url);
75const babel: {
76 transformAsync: (
77 code: string,
78 options: Record<string, unknown>,
79 ) => Promise<{ code?: string | null; map?: object | null } | null>;
80} = require("@babel/core");
81const babelConfig = require("./babel.config.js")({ cache: () => {} });
82const testPlatform = process.env.T3_MOBILE_TEST_PLATFORM === "android" ? "android" : "ios";
83​
84// gt-react-native loads react-native and its hooks need <GTProvider>; tests get GT's core set up the way gt-react's initializeGTSPA does, which renders English without a provider.
85const gtTestModule = "\0gt-react-native";
86const gtReactNativeEntry = require.resolve("gt-react-native");
87const gtTestConfig = JSON.stringify({ defaultLocale: require("./gt.config.json").defaultLocale });
88const gtTestModuleCode = `
89import { I18nStore, ReactI18nCache, initializeI18nConfig, setI18nStore, setReactI18nCache } from "@generaltranslation/react-core/pure";
90import { ReadonlyConditionStore, createConditionStoreSingleton } from "gt-i18n/internal";
91const config = ${gtTestConfig};
92initializeI18nConfig(config, "SPA");
93setReactI18nCache(new ReactI18nCache(config));
94createConditionStoreSingleton().setConditionStore(new ReadonlyConditionStore({ locale: config.defaultLocale }));
95setI18nStore(new I18nStore());
96export * from "@generaltranslation/react-core/components";
97export * from "@generaltranslation/react-core/hooks";
98export * from "@generaltranslation/react-core/pure";
99export const initializeGT = () => {};
100export const GTProvider = (props) => props.children;
101`;
102​
103export default defineConfig({
104 plugins: [
105 {
106 name: "gt-react-native-test",
107 enforce: "pre",
108 resolveId(source, importer) {
109 if (source === "gt-react-native") return gtTestModule;
110 if (importer === gtTestModule) {
111 return this.resolve(source, gtReactNativeEntry, { skipSelf: true });
112 }
113 },
114 load(id) {
115 if (id === gtTestModule) return gtTestModuleCode;
116 },
117 },
118 ],
119 test: {
120 hookTimeout: 60_000,
121 testTimeout: 60_000,
122 projects: [
123 {
124 extends: true,
125 test: {
126 name: "unit",
127 include: ["**/*.test.{ts,tsx,mjs}"],
128 exclude: ["**/node_modules/**", ...reactTests],
129 setupFiles: [
130 "../../packages/shared/src/testing/longTempDir.ts",
131 "../../packages/shared/src/testing/providerEnv.ts",
132 "../../packages/shared/src/testing/gitEnv.ts",
133 ],
134 },
135 },
136 {
137 extends: true,
138 plugins: [
139 {
140 name: `mobile-release-react-compiler-${testPlatform}`,
141 enforce: "pre",
142 async transform(code, id) {
143 if (
144 !normalizePath(id).startsWith(sourceRoot) ||
145 !/\.tsx?$/.test(id) ||
146 /\.test\.tsx?$/.test(id)
147 ) {
148 return;
149 }
150 const result = await babel.transformAsync(code, {
151 ...babelConfig,
152 presets: babelConfig.presets.map(([name, options]: [string, object]) => [
153 require.resolve(name),
154 // Vite runs in Node; inline Babel helpers instead of Metro runtime imports.
155 { ...options, enableBabelRuntime: false },
156 ]),
157 filename: id,
158 babelrc: false,
159 configFile: false,
160 sourceMaps: true,
161 caller: {
162 name: "metro",
163 bundler: "metro",
164 platform: testPlatform,
165 isDev: false,
166 isServer: false,
167 isNodeModule: false,
168 supportsStaticESM: true,
169 supportsReactCompiler: true,
170 },
171 });
172 if (result?.code) return { code: result.code, map: result.map };
173 },
174 },
175 ],
176 test: {
177 name: "react",
178 include: reactTests,
179 // Native components mock React Native and render through react-dom in these tests.
180 environment: "jsdom",
181 setupFiles: [
182 "../../packages/shared/src/testing/providerEnv.ts",
183 "../../packages/shared/src/testing/gitEnv.ts",
184 ],
185 },
186 },
187 ],
188 },
189});