@multiplatform.one/config 8.0.0 → 8.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/design-lint.mjs +16 -4
- package/lib/index.js +1 -1
- package/lib/vitest-C2MTsd5m.js +255 -0
- package/lib/vitest.js +1 -238
- package/package.json +6 -2
- package/src/testTiers.d.ts +3 -0
- package/src/testTiers.js +33 -0
- package/src/vitest.spec.ts +87 -1
- package/src/vitest.ts +4 -2
- package/types/vitest.d.ts +1 -1
- package/types/vitest.d.ts.map +1 -1
package/design-lint.mjs
CHANGED
|
@@ -233,15 +233,27 @@ export const houseProperNames = [
|
|
|
233
233
|
// A file of proper-noun data opts out of copy-title-case with a
|
|
234
234
|
// `copy-title-case-escape: <reason>` comment; lint-conventions holds each one
|
|
235
235
|
// to a row in the spec's `## Title-case escapes` table.
|
|
236
|
-
const titleCaseEscapeComment =
|
|
237
|
-
/(?<!`)(?:\/\/|\/\*)\s*copy-title-case-escape:\s*(.*?)\s*(?:\*\/.*)?$/;
|
|
236
|
+
const titleCaseEscapeComment = /(?<!`)(?:\/\/|\/\*)\s*copy-title-case-escape:/g;
|
|
238
237
|
export function findTitleCaseEscapes(source) {
|
|
239
238
|
if (!source.includes("copy-title-case-escape:")) return [];
|
|
240
239
|
return source.split("\n").flatMap((text, index) => {
|
|
241
|
-
const
|
|
242
|
-
return
|
|
240
|
+
const reason = text.trim().startsWith("*") ? undefined : titleCaseEscapeReason(text);
|
|
241
|
+
return reason === undefined ? [] : [{ line: index + 1, reason }];
|
|
243
242
|
});
|
|
244
243
|
}
|
|
244
|
+
const escapeLineBreak = /[\n\r\u2028\u2029]/;
|
|
245
|
+
// The text after the first escape comment whose reason fits on the line, up to
|
|
246
|
+
// the first `*/` with no line break after it, trimmed.
|
|
247
|
+
function titleCaseEscapeReason(text) {
|
|
248
|
+
for (const escape of text.matchAll(titleCaseEscapeComment)) {
|
|
249
|
+
const rest = text.slice(escape.index + escape[0].length).trimStart();
|
|
250
|
+
const lastBreak = Math.max(...["\n", "\r", "\u2028", "\u2029"].map((c) => rest.lastIndexOf(c)));
|
|
251
|
+
const close = rest.indexOf("*/", lastBreak + 1);
|
|
252
|
+
const reason = (close === -1 ? rest : rest.slice(0, close)).trimEnd();
|
|
253
|
+
if (!escapeLineBreak.test(reason)) return reason;
|
|
254
|
+
}
|
|
255
|
+
return undefined;
|
|
256
|
+
}
|
|
245
257
|
const copyAttribute =
|
|
246
258
|
/^(?:label|title|subtitle|description|placeholder|aria-label|aria-description|accessibilityLabel|accessibilityHint|helperText|hint|message|heading|caption|tooltip|emptyText|emptyTitle|emptyDescription|confirmLabel|confirmText|cancelLabel|cancelText|actionLabel|successMessage)$/;
|
|
247
259
|
const errorKey = /(?:^error|Error|^fail|Fail|Failure)\w*$/;
|
package/lib/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { i as phosphorTokenColorPlugin, n as tamaguiWorkspacePathsFile, t as ensureTamaguiWorkspacePaths } from "./tamaguiWorkspacePaths-DR3dud79.js";
|
|
2
2
|
import { t as createStorybookViteConfig } from "./storybook-BpaziEtP.js";
|
|
3
3
|
import { t as createViteConfig } from "./vite-CeDmMMjx.js";
|
|
4
|
-
import { createVitestConfig } from "./vitest.js";
|
|
4
|
+
import { t as createVitestConfig } from "./vitest-C2MTsd5m.js";
|
|
5
5
|
|
|
6
6
|
export { createStorybookViteConfig, createViteConfig, createVitestConfig, ensureTamaguiWorkspacePaths, phosphorTokenColorPlugin, tamaguiWorkspacePathsFile };
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
import { i as phosphorTokenColorPlugin, r as phosphorBarrelBundlePlugin, t as ensureTamaguiWorkspacePaths } from "./tamaguiWorkspacePaths-DR3dud79.js";
|
|
2
|
+
import Module from "node:module";
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
6
|
+
import { tamaguiPlugin } from "@tamagui/vite-plugin";
|
|
7
|
+
import { resolvePathSync } from "mlly";
|
|
8
|
+
import react from "@vitejs/plugin-react";
|
|
9
|
+
import { configDefaults } from "vitest/config";
|
|
10
|
+
|
|
11
|
+
//#region src/testTiers.js
|
|
12
|
+
const testFile = "{spec,test}.?(c|m)[jt]s?(x)";
|
|
13
|
+
/**
|
|
14
|
+
* The integration tier, by file name (FZ-10556). A spec named
|
|
15
|
+
* `*.integration.spec.ts` or `*.live.spec.ts` needs a live service such as a
|
|
16
|
+
* Frappe bench or Keycloak. The dot before the tier word is the rule:
|
|
17
|
+
* rich-text's `integration.spec.tsx` is a unit spec and stays one. The unit
|
|
18
|
+
* run excludes these (createVitestConfig does it for every factory config)
|
|
19
|
+
* and the integration run is exactly these, so neither tier skips on a
|
|
20
|
+
* missing service: unit never needs one, and integration fails without one.
|
|
21
|
+
*/
|
|
22
|
+
const integrationSpecs = [`**/*.integration.${testFile}`, `**/*.live.${testFile}`];
|
|
23
|
+
|
|
24
|
+
//#endregion
|
|
25
|
+
//#region src/vitest.ts
|
|
26
|
+
const pinReactRequireSetup = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../pin-react-require.cjs");
|
|
27
|
+
function resolveSetupFile(file, conditions) {
|
|
28
|
+
if (file.startsWith(".") || path.isAbsolute(file) || /^[a-z][a-z\d+.-]*:/i.test(file)) return file;
|
|
29
|
+
try {
|
|
30
|
+
return resolvePathSync(file, {
|
|
31
|
+
url: pathToFileURL(path.join(process.cwd(), "vitest.config.js")),
|
|
32
|
+
conditions: [...conditions, "import"],
|
|
33
|
+
extensions: []
|
|
34
|
+
});
|
|
35
|
+
} catch (error) {
|
|
36
|
+
if (error.code === "ERR_MODULE_NOT_FOUND") return file;
|
|
37
|
+
throw error;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* A gate on a loaded box caps its workers and, only if specs still time out
|
|
42
|
+
* under that cap, lengthens the per-test timeout (FZ-7915). Unset, the package
|
|
43
|
+
* values stand, so CI keeps the defaults. Vitest 4.1 reads VITEST_MAX_WORKERS
|
|
44
|
+
* itself as well; naming it here keeps the cap if that ever moves.
|
|
45
|
+
*/
|
|
46
|
+
function positiveIntEnv(name) {
|
|
47
|
+
const value = Number(process.env[name]);
|
|
48
|
+
return Number.isInteger(value) && value > 0 ? value : void 0;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* The coverage SonarQube reads (FZ-8866). With MPO_COVERAGE=1, CI's test-vitest
|
|
52
|
+
* measures every run as lcov, even a failing one. Each vitest process writes
|
|
53
|
+
* its own directory because a package can run twice, as components runs its
|
|
54
|
+
* web and native suites, and `clean` would wipe the first report.
|
|
55
|
+
* scripts/coverage-lcov.mjs joins them with repo-relative paths.
|
|
56
|
+
*/
|
|
57
|
+
function ciCoverage() {
|
|
58
|
+
if (process.env.MPO_COVERAGE !== "1") return {};
|
|
59
|
+
return {
|
|
60
|
+
enabled: true,
|
|
61
|
+
reportOnFailure: true,
|
|
62
|
+
reporter: ["lcovonly"],
|
|
63
|
+
reportsDirectory: `./coverage/lcov-${process.pid}`
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* CJS `require('react')` does not go through Vite aliases. `use-sync-external-store`
|
|
68
|
+
* is a valid Node import, so Vitest leaves it external even when we ask to inline
|
|
69
|
+
* it, and Node then loads the hoisted root copy — a different inode than the
|
|
70
|
+
* `.pnpm` copy Vite binds. Patching Module._resolveFilename in the vitest
|
|
71
|
+
* process (config eval + workers that re-load this factory) makes that
|
|
72
|
+
* require land on the same physical copy as the aliases.
|
|
73
|
+
*/
|
|
74
|
+
function pinNodeReactRequire(reactDir, reactDomDir, schedulerDir) {
|
|
75
|
+
const ctor = Module;
|
|
76
|
+
if (ctor._mpoOneReact) return;
|
|
77
|
+
ctor._mpoOneReact = true;
|
|
78
|
+
const orig = ctor._resolveFilename;
|
|
79
|
+
ctor._resolveFilename = function pinned(request, parent, isMain, options) {
|
|
80
|
+
const next = request === "react" ? reactDir : request.startsWith("react/") ? path.join(reactDir, request.slice(6)) : request === "react-dom" ? reactDomDir : request.startsWith("react-dom/") ? path.join(reactDomDir, request.slice(10)) : request === "scheduler" ? schedulerDir : null;
|
|
81
|
+
if (next) try {
|
|
82
|
+
return orig.call(this, next, parent, isMain, options);
|
|
83
|
+
} catch {}
|
|
84
|
+
return orig.call(this, request, parent, isMain, options);
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Creates a shared Vitest configuration for multiplatform.one packages.
|
|
89
|
+
*
|
|
90
|
+
* This eliminates the ~100 lines of duplicated vitest config across 13+ packages,
|
|
91
|
+
* handling Tamagui transforms, react-native-web replacement, React deduplication,
|
|
92
|
+
* and consistent coverage/environment settings.
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* ```ts
|
|
96
|
+
* // packages/components/vitest.config.mjs
|
|
97
|
+
* import { createVitestConfig } from "@multiplatform.one/config/vitest";
|
|
98
|
+
*
|
|
99
|
+
* export default createVitestConfig({
|
|
100
|
+
* setupFiles: ["./tests/setup.tsx"],
|
|
101
|
+
* tamaguiConfig: "../../features/tamagui.config.ts",
|
|
102
|
+
* });
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
function createVitestConfig(options = {}) {
|
|
106
|
+
ensureTamaguiWorkspacePaths();
|
|
107
|
+
const { tamaguiConfig = "./tests/tamagui.config.ts", environment = "jsdom", setupFiles = [], testTimeout = 3e4, inlineDeps = [], externalDeps = [], aliases = {}, coverage, replaceReactNative = true, dedupeReact = true, rootNodeModules: _rootNodeModules, conditions = [
|
|
108
|
+
"source",
|
|
109
|
+
"test",
|
|
110
|
+
"browser"
|
|
111
|
+
], include, exclude, passWithNoTests } = options;
|
|
112
|
+
const rootNodeModules = _rootNodeModules || path.resolve(process.cwd(), "../../node_modules");
|
|
113
|
+
const plugins = [
|
|
114
|
+
react({ jsxRuntime: "automatic" }),
|
|
115
|
+
phosphorTokenColorPlugin(),
|
|
116
|
+
phosphorBarrelBundlePlugin()
|
|
117
|
+
];
|
|
118
|
+
if (replaceReactNative) plugins.push({
|
|
119
|
+
name: "replace-react-native",
|
|
120
|
+
transform(code, id) {
|
|
121
|
+
if (id.includes("node_modules")) return;
|
|
122
|
+
return {
|
|
123
|
+
code: code.replace(/from ['"]react-native['"]/g, "from \"react-native-web\""),
|
|
124
|
+
map: null
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
if (tamaguiConfig !== false) plugins.push(tamaguiPlugin({
|
|
129
|
+
components: ["tamagui"],
|
|
130
|
+
config: tamaguiConfig
|
|
131
|
+
}));
|
|
132
|
+
const resolveAliases = {};
|
|
133
|
+
if (replaceReactNative) {
|
|
134
|
+
resolveAliases["react-native"] = "react-native-web";
|
|
135
|
+
resolveAliases["react-native-svg"] = "@tamagui/react-native-svg";
|
|
136
|
+
}
|
|
137
|
+
if (dedupeReact) {
|
|
138
|
+
const onePhysicalCopy = (pkg) => {
|
|
139
|
+
for (const base of [path.resolve(process.cwd(), "node_modules"), rootNodeModules]) try {
|
|
140
|
+
return fs.realpathSync(path.resolve(base, pkg));
|
|
141
|
+
} catch {}
|
|
142
|
+
return path.resolve(rootNodeModules, pkg);
|
|
143
|
+
};
|
|
144
|
+
const reactDir = onePhysicalCopy("react");
|
|
145
|
+
const reactDomDir = onePhysicalCopy("react-dom");
|
|
146
|
+
resolveAliases.react = reactDir;
|
|
147
|
+
resolveAliases["react-dom"] = reactDomDir;
|
|
148
|
+
resolveAliases["react-dom/client"] = path.join(reactDomDir, "client");
|
|
149
|
+
resolveAliases["react-dom/server"] = path.join(reactDomDir, "server");
|
|
150
|
+
resolveAliases["react/jsx-runtime"] = path.join(reactDir, "jsx-runtime");
|
|
151
|
+
resolveAliases["react/jsx-dev-runtime"] = path.join(reactDir, "jsx-dev-runtime");
|
|
152
|
+
const schedulerDir = onePhysicalCopy("scheduler");
|
|
153
|
+
resolveAliases.scheduler = schedulerDir;
|
|
154
|
+
process.env.MPO_VITEST_REACT = reactDir;
|
|
155
|
+
process.env.MPO_VITEST_REACT_DOM = reactDomDir;
|
|
156
|
+
process.env.MPO_VITEST_SCHEDULER = schedulerDir;
|
|
157
|
+
pinNodeReactRequire(reactDir, reactDomDir, schedulerDir);
|
|
158
|
+
}
|
|
159
|
+
const resolvedSetupFiles = setupFiles.map((file) => resolveSetupFile(file, conditions));
|
|
160
|
+
const testConfig = {
|
|
161
|
+
environment,
|
|
162
|
+
globals: true,
|
|
163
|
+
setupFiles: dedupeReact ? [pinReactRequireSetup, ...resolvedSetupFiles] : resolvedSetupFiles,
|
|
164
|
+
testTimeout: positiveIntEnv("VITEST_TEST_TIMEOUT") ?? testTimeout,
|
|
165
|
+
...positiveIntEnv("VITEST_MAX_WORKERS") ? { maxWorkers: positiveIntEnv("VITEST_MAX_WORKERS") } : null,
|
|
166
|
+
coverage: {
|
|
167
|
+
provider: "v8",
|
|
168
|
+
reporter: [
|
|
169
|
+
"text",
|
|
170
|
+
"lcov",
|
|
171
|
+
"html"
|
|
172
|
+
],
|
|
173
|
+
reportsDirectory: "./coverage",
|
|
174
|
+
all: true,
|
|
175
|
+
clean: true,
|
|
176
|
+
exclude: [
|
|
177
|
+
"node_modules/",
|
|
178
|
+
"tests/",
|
|
179
|
+
"*.config.*",
|
|
180
|
+
"**/*.stories.*",
|
|
181
|
+
"**/*.spec.*",
|
|
182
|
+
"**/*.test.*",
|
|
183
|
+
...coverage?.exclude || []
|
|
184
|
+
],
|
|
185
|
+
...ciCoverage()
|
|
186
|
+
},
|
|
187
|
+
server: { deps: {
|
|
188
|
+
inline: [
|
|
189
|
+
"react-native-web",
|
|
190
|
+
"one",
|
|
191
|
+
/@tamagui\//,
|
|
192
|
+
"tamagui",
|
|
193
|
+
/@tanstack\//,
|
|
194
|
+
"use-sync-external-store",
|
|
195
|
+
"react-i18next",
|
|
196
|
+
"@phosphor-icons/react",
|
|
197
|
+
...inlineDeps
|
|
198
|
+
],
|
|
199
|
+
...externalDeps.length > 0 ? { external: [pinReactRequireSetup, ...externalDeps] } : { external: [pinReactRequireSetup] },
|
|
200
|
+
interopDefault: true
|
|
201
|
+
} }
|
|
202
|
+
};
|
|
203
|
+
if (include) testConfig.include = include;
|
|
204
|
+
testConfig.exclude = [...exclude ?? configDefaults.exclude, ...integrationSpecs];
|
|
205
|
+
if (passWithNoTests) testConfig.passWithNoTests = true;
|
|
206
|
+
return {
|
|
207
|
+
plugins,
|
|
208
|
+
test: testConfig,
|
|
209
|
+
resolve: {
|
|
210
|
+
extensions: [
|
|
211
|
+
".web.ts",
|
|
212
|
+
".web.tsx",
|
|
213
|
+
".web.js",
|
|
214
|
+
".web.jsx",
|
|
215
|
+
".ts",
|
|
216
|
+
".tsx",
|
|
217
|
+
".js",
|
|
218
|
+
".jsx"
|
|
219
|
+
],
|
|
220
|
+
mainFields: [
|
|
221
|
+
"browser",
|
|
222
|
+
"module",
|
|
223
|
+
"main"
|
|
224
|
+
],
|
|
225
|
+
alias: {
|
|
226
|
+
...resolveAliases,
|
|
227
|
+
...aliases
|
|
228
|
+
},
|
|
229
|
+
conditions,
|
|
230
|
+
dedupe: []
|
|
231
|
+
},
|
|
232
|
+
define: {
|
|
233
|
+
__DEV__: true,
|
|
234
|
+
global: "globalThis",
|
|
235
|
+
process: JSON.stringify({
|
|
236
|
+
env: {
|
|
237
|
+
NODE_ENV: "test",
|
|
238
|
+
NODE_DEBUG: false,
|
|
239
|
+
...dedupeReact ? {
|
|
240
|
+
MPO_VITEST_REACT: process.env.MPO_VITEST_REACT,
|
|
241
|
+
MPO_VITEST_REACT_DOM: process.env.MPO_VITEST_REACT_DOM,
|
|
242
|
+
MPO_VITEST_SCHEDULER: process.env.MPO_VITEST_SCHEDULER
|
|
243
|
+
} : {}
|
|
244
|
+
},
|
|
245
|
+
platform: process.platform,
|
|
246
|
+
version: process.version,
|
|
247
|
+
type: "renderer"
|
|
248
|
+
}),
|
|
249
|
+
"Buffer.isBuffer": "((obj) => obj?.constructor?.name === 'Buffer')"
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
//#endregion
|
|
255
|
+
export { createVitestConfig as t };
|
package/lib/vitest.js
CHANGED
|
@@ -1,240 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import Module from "node:module";
|
|
3
|
-
import fs from "node:fs";
|
|
4
|
-
import path from "node:path";
|
|
5
|
-
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
6
|
-
import { tamaguiPlugin } from "@tamagui/vite-plugin";
|
|
7
|
-
import { resolvePathSync } from "mlly";
|
|
8
|
-
import react from "@vitejs/plugin-react";
|
|
1
|
+
import { t as createVitestConfig } from "./vitest-C2MTsd5m.js";
|
|
9
2
|
|
|
10
|
-
//#region src/vitest.ts
|
|
11
|
-
const pinReactRequireSetup = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../pin-react-require.cjs");
|
|
12
|
-
function resolveSetupFile(file, conditions) {
|
|
13
|
-
if (file.startsWith(".") || path.isAbsolute(file) || /^[a-z][a-z\d+.-]*:/i.test(file)) return file;
|
|
14
|
-
try {
|
|
15
|
-
return resolvePathSync(file, {
|
|
16
|
-
url: pathToFileURL(path.join(process.cwd(), "vitest.config.js")),
|
|
17
|
-
conditions: [...conditions, "import"],
|
|
18
|
-
extensions: []
|
|
19
|
-
});
|
|
20
|
-
} catch (error) {
|
|
21
|
-
if (error.code === "ERR_MODULE_NOT_FOUND") return file;
|
|
22
|
-
throw error;
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* A gate on a loaded box caps its workers and, only if specs still time out
|
|
27
|
-
* under that cap, lengthens the per-test timeout (FZ-7915). Unset, the package
|
|
28
|
-
* values stand, so CI keeps the defaults. Vitest 4.1 reads VITEST_MAX_WORKERS
|
|
29
|
-
* itself as well; naming it here keeps the cap if that ever moves.
|
|
30
|
-
*/
|
|
31
|
-
function positiveIntEnv(name) {
|
|
32
|
-
const value = Number(process.env[name]);
|
|
33
|
-
return Number.isInteger(value) && value > 0 ? value : void 0;
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* The coverage SonarQube reads (FZ-8866). With MPO_COVERAGE=1, CI's test-vitest
|
|
37
|
-
* measures every run as lcov, even a failing one. Each vitest process writes
|
|
38
|
-
* its own directory because a package can run twice, as components runs its
|
|
39
|
-
* web and native suites, and `clean` would wipe the first report.
|
|
40
|
-
* scripts/coverage-lcov.mjs joins them with repo-relative paths.
|
|
41
|
-
*/
|
|
42
|
-
function ciCoverage() {
|
|
43
|
-
if (process.env.MPO_COVERAGE !== "1") return {};
|
|
44
|
-
return {
|
|
45
|
-
enabled: true,
|
|
46
|
-
reportOnFailure: true,
|
|
47
|
-
reporter: ["lcovonly"],
|
|
48
|
-
reportsDirectory: `./coverage/lcov-${process.pid}`
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* CJS `require('react')` does not go through Vite aliases. `use-sync-external-store`
|
|
53
|
-
* is a valid Node import, so Vitest leaves it external even when we ask to inline
|
|
54
|
-
* it, and Node then loads the hoisted root copy — a different inode than the
|
|
55
|
-
* `.pnpm` copy Vite binds. Patching Module._resolveFilename in the vitest
|
|
56
|
-
* process (config eval + workers that re-load this factory) makes that
|
|
57
|
-
* require land on the same physical copy as the aliases.
|
|
58
|
-
*/
|
|
59
|
-
function pinNodeReactRequire(reactDir, reactDomDir, schedulerDir) {
|
|
60
|
-
const ctor = Module;
|
|
61
|
-
if (ctor._mpoOneReact) return;
|
|
62
|
-
ctor._mpoOneReact = true;
|
|
63
|
-
const orig = ctor._resolveFilename;
|
|
64
|
-
ctor._resolveFilename = function pinned(request, parent, isMain, options) {
|
|
65
|
-
const next = request === "react" ? reactDir : request.startsWith("react/") ? path.join(reactDir, request.slice(6)) : request === "react-dom" ? reactDomDir : request.startsWith("react-dom/") ? path.join(reactDomDir, request.slice(10)) : request === "scheduler" ? schedulerDir : null;
|
|
66
|
-
if (next) try {
|
|
67
|
-
return orig.call(this, next, parent, isMain, options);
|
|
68
|
-
} catch {}
|
|
69
|
-
return orig.call(this, request, parent, isMain, options);
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* Creates a shared Vitest configuration for multiplatform.one packages.
|
|
74
|
-
*
|
|
75
|
-
* This eliminates the ~100 lines of duplicated vitest config across 13+ packages,
|
|
76
|
-
* handling Tamagui transforms, react-native-web replacement, React deduplication,
|
|
77
|
-
* and consistent coverage/environment settings.
|
|
78
|
-
*
|
|
79
|
-
* @example
|
|
80
|
-
* ```ts
|
|
81
|
-
* // packages/components/vitest.config.mjs
|
|
82
|
-
* import { createVitestConfig } from "@multiplatform.one/config/vitest";
|
|
83
|
-
*
|
|
84
|
-
* export default createVitestConfig({
|
|
85
|
-
* setupFiles: ["./tests/setup.tsx"],
|
|
86
|
-
* tamaguiConfig: "../../features/tamagui.config.ts",
|
|
87
|
-
* });
|
|
88
|
-
* ```
|
|
89
|
-
*/
|
|
90
|
-
function createVitestConfig(options = {}) {
|
|
91
|
-
ensureTamaguiWorkspacePaths();
|
|
92
|
-
const { tamaguiConfig = "./tests/tamagui.config.ts", environment = "jsdom", setupFiles = [], testTimeout = 3e4, inlineDeps = [], externalDeps = [], aliases = {}, coverage, replaceReactNative = true, dedupeReact = true, rootNodeModules: _rootNodeModules, conditions = [
|
|
93
|
-
"source",
|
|
94
|
-
"test",
|
|
95
|
-
"browser"
|
|
96
|
-
], include, exclude, passWithNoTests } = options;
|
|
97
|
-
const rootNodeModules = _rootNodeModules || path.resolve(process.cwd(), "../../node_modules");
|
|
98
|
-
const plugins = [
|
|
99
|
-
react({ jsxRuntime: "automatic" }),
|
|
100
|
-
phosphorTokenColorPlugin(),
|
|
101
|
-
phosphorBarrelBundlePlugin()
|
|
102
|
-
];
|
|
103
|
-
if (replaceReactNative) plugins.push({
|
|
104
|
-
name: "replace-react-native",
|
|
105
|
-
transform(code, id) {
|
|
106
|
-
if (id.includes("node_modules")) return;
|
|
107
|
-
return {
|
|
108
|
-
code: code.replace(/from ['"]react-native['"]/g, "from \"react-native-web\""),
|
|
109
|
-
map: null
|
|
110
|
-
};
|
|
111
|
-
}
|
|
112
|
-
});
|
|
113
|
-
if (tamaguiConfig !== false) plugins.push(tamaguiPlugin({
|
|
114
|
-
components: ["tamagui"],
|
|
115
|
-
config: tamaguiConfig
|
|
116
|
-
}));
|
|
117
|
-
const resolveAliases = {};
|
|
118
|
-
if (replaceReactNative) {
|
|
119
|
-
resolveAliases["react-native"] = "react-native-web";
|
|
120
|
-
resolveAliases["react-native-svg"] = "@tamagui/react-native-svg";
|
|
121
|
-
}
|
|
122
|
-
if (dedupeReact) {
|
|
123
|
-
const onePhysicalCopy = (pkg) => {
|
|
124
|
-
for (const base of [path.resolve(process.cwd(), "node_modules"), rootNodeModules]) try {
|
|
125
|
-
return fs.realpathSync(path.resolve(base, pkg));
|
|
126
|
-
} catch {}
|
|
127
|
-
return path.resolve(rootNodeModules, pkg);
|
|
128
|
-
};
|
|
129
|
-
const reactDir = onePhysicalCopy("react");
|
|
130
|
-
const reactDomDir = onePhysicalCopy("react-dom");
|
|
131
|
-
resolveAliases.react = reactDir;
|
|
132
|
-
resolveAliases["react-dom"] = reactDomDir;
|
|
133
|
-
resolveAliases["react-dom/client"] = path.join(reactDomDir, "client");
|
|
134
|
-
resolveAliases["react-dom/server"] = path.join(reactDomDir, "server");
|
|
135
|
-
resolveAliases["react/jsx-runtime"] = path.join(reactDir, "jsx-runtime");
|
|
136
|
-
resolveAliases["react/jsx-dev-runtime"] = path.join(reactDir, "jsx-dev-runtime");
|
|
137
|
-
const schedulerDir = onePhysicalCopy("scheduler");
|
|
138
|
-
resolveAliases.scheduler = schedulerDir;
|
|
139
|
-
process.env.MPO_VITEST_REACT = reactDir;
|
|
140
|
-
process.env.MPO_VITEST_REACT_DOM = reactDomDir;
|
|
141
|
-
process.env.MPO_VITEST_SCHEDULER = schedulerDir;
|
|
142
|
-
pinNodeReactRequire(reactDir, reactDomDir, schedulerDir);
|
|
143
|
-
}
|
|
144
|
-
const resolvedSetupFiles = setupFiles.map((file) => resolveSetupFile(file, conditions));
|
|
145
|
-
const testConfig = {
|
|
146
|
-
environment,
|
|
147
|
-
globals: true,
|
|
148
|
-
setupFiles: dedupeReact ? [pinReactRequireSetup, ...resolvedSetupFiles] : resolvedSetupFiles,
|
|
149
|
-
testTimeout: positiveIntEnv("VITEST_TEST_TIMEOUT") ?? testTimeout,
|
|
150
|
-
...positiveIntEnv("VITEST_MAX_WORKERS") ? { maxWorkers: positiveIntEnv("VITEST_MAX_WORKERS") } : null,
|
|
151
|
-
coverage: {
|
|
152
|
-
provider: "v8",
|
|
153
|
-
reporter: [
|
|
154
|
-
"text",
|
|
155
|
-
"lcov",
|
|
156
|
-
"html"
|
|
157
|
-
],
|
|
158
|
-
reportsDirectory: "./coverage",
|
|
159
|
-
all: true,
|
|
160
|
-
clean: true,
|
|
161
|
-
exclude: [
|
|
162
|
-
"node_modules/",
|
|
163
|
-
"tests/",
|
|
164
|
-
"*.config.*",
|
|
165
|
-
"**/*.stories.*",
|
|
166
|
-
"**/*.spec.*",
|
|
167
|
-
"**/*.test.*",
|
|
168
|
-
...coverage?.exclude || []
|
|
169
|
-
],
|
|
170
|
-
...ciCoverage()
|
|
171
|
-
},
|
|
172
|
-
server: { deps: {
|
|
173
|
-
inline: [
|
|
174
|
-
"react-native-web",
|
|
175
|
-
"one",
|
|
176
|
-
/@tamagui\//,
|
|
177
|
-
"tamagui",
|
|
178
|
-
/@tanstack\//,
|
|
179
|
-
"use-sync-external-store",
|
|
180
|
-
"react-i18next",
|
|
181
|
-
"@phosphor-icons/react",
|
|
182
|
-
...inlineDeps
|
|
183
|
-
],
|
|
184
|
-
...externalDeps.length > 0 ? { external: [pinReactRequireSetup, ...externalDeps] } : { external: [pinReactRequireSetup] },
|
|
185
|
-
interopDefault: true
|
|
186
|
-
} }
|
|
187
|
-
};
|
|
188
|
-
if (include) testConfig.include = include;
|
|
189
|
-
if (exclude) testConfig.exclude = exclude;
|
|
190
|
-
if (passWithNoTests) testConfig.passWithNoTests = true;
|
|
191
|
-
return {
|
|
192
|
-
plugins,
|
|
193
|
-
test: testConfig,
|
|
194
|
-
resolve: {
|
|
195
|
-
extensions: [
|
|
196
|
-
".web.ts",
|
|
197
|
-
".web.tsx",
|
|
198
|
-
".web.js",
|
|
199
|
-
".web.jsx",
|
|
200
|
-
".ts",
|
|
201
|
-
".tsx",
|
|
202
|
-
".js",
|
|
203
|
-
".jsx"
|
|
204
|
-
],
|
|
205
|
-
mainFields: [
|
|
206
|
-
"browser",
|
|
207
|
-
"module",
|
|
208
|
-
"main"
|
|
209
|
-
],
|
|
210
|
-
alias: {
|
|
211
|
-
...resolveAliases,
|
|
212
|
-
...aliases
|
|
213
|
-
},
|
|
214
|
-
conditions,
|
|
215
|
-
dedupe: []
|
|
216
|
-
},
|
|
217
|
-
define: {
|
|
218
|
-
__DEV__: true,
|
|
219
|
-
global: "globalThis",
|
|
220
|
-
process: JSON.stringify({
|
|
221
|
-
env: {
|
|
222
|
-
NODE_ENV: "test",
|
|
223
|
-
NODE_DEBUG: false,
|
|
224
|
-
...dedupeReact ? {
|
|
225
|
-
MPO_VITEST_REACT: process.env.MPO_VITEST_REACT,
|
|
226
|
-
MPO_VITEST_REACT_DOM: process.env.MPO_VITEST_REACT_DOM,
|
|
227
|
-
MPO_VITEST_SCHEDULER: process.env.MPO_VITEST_SCHEDULER
|
|
228
|
-
} : {}
|
|
229
|
-
},
|
|
230
|
-
platform: process.platform,
|
|
231
|
-
version: process.version,
|
|
232
|
-
type: "renderer"
|
|
233
|
-
}),
|
|
234
|
-
"Buffer.isBuffer": "((obj) => obj?.constructor?.name === 'Buffer')"
|
|
235
|
-
}
|
|
236
|
-
};
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
//#endregion
|
|
240
3
|
export { createVitestConfig };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@multiplatform.one/config",
|
|
3
|
-
"version": "8.
|
|
3
|
+
"version": "8.2.0",
|
|
4
4
|
"description": "Shared build and test configuration presets for multiplatform.one",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"config",
|
|
@@ -81,6 +81,10 @@
|
|
|
81
81
|
"import": "./lib/vitest.js",
|
|
82
82
|
"require": "./lib/vitest.js"
|
|
83
83
|
},
|
|
84
|
+
"./test-tiers": {
|
|
85
|
+
"types": "./src/testTiers.d.ts",
|
|
86
|
+
"default": "./src/testTiers.js"
|
|
87
|
+
},
|
|
84
88
|
"./playwright": {
|
|
85
89
|
"source": "./src/playwright.ts",
|
|
86
90
|
"types": "./types/playwright.d.ts",
|
|
@@ -118,7 +122,7 @@
|
|
|
118
122
|
"@babel/parser": "^7.29.9",
|
|
119
123
|
"@babel/plugin-transform-block-scoping": "^7.29.7",
|
|
120
124
|
"@babel/traverse": "^7.29.10",
|
|
121
|
-
"@multiplatform.one/utils": "^8.
|
|
125
|
+
"@multiplatform.one/utils": "^8.2.0",
|
|
122
126
|
"@tamagui/vite-plugin": "2.7.6",
|
|
123
127
|
"@vitejs/plugin-react": "^6.1.2",
|
|
124
128
|
"dotenv": "^17.4.2",
|
package/src/testTiers.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// Plain JS so a vitest config file can import it unbuilt: Vite loads config
|
|
2
|
+
// files under Node's own conditions, which reach lib/ for the ./vitest entry.
|
|
3
|
+
import { configDefaults } from "vitest/config";
|
|
4
|
+
|
|
5
|
+
const testFile = "{spec,test}.?(c|m)[jt]s?(x)";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The integration tier, by file name (FZ-10556). A spec named
|
|
9
|
+
* `*.integration.spec.ts` or `*.live.spec.ts` needs a live service such as a
|
|
10
|
+
* Frappe bench or Keycloak. The dot before the tier word is the rule:
|
|
11
|
+
* rich-text's `integration.spec.tsx` is a unit spec and stays one. The unit
|
|
12
|
+
* run excludes these (createVitestConfig does it for every factory config)
|
|
13
|
+
* and the integration run is exactly these, so neither tier skips on a
|
|
14
|
+
* missing service: unit never needs one, and integration fails without one.
|
|
15
|
+
*/
|
|
16
|
+
export const integrationSpecs = [`**/*.integration.${testFile}`, `**/*.live.${testFile}`];
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* A package's integration run: its unit config with the test files swapped
|
|
20
|
+
* for `integrationSpecs`. It takes the default export of any vitest config, a
|
|
21
|
+
* factory one or a hand-rolled one, so a package keeps one config and derives
|
|
22
|
+
* the other in `vitest.integration.mjs`.
|
|
23
|
+
*/
|
|
24
|
+
export function integrationConfig(config) {
|
|
25
|
+
const test = config.test ?? {};
|
|
26
|
+
const exclude = (test.exclude ?? configDefaults.exclude).filter(
|
|
27
|
+
(pattern) => !integrationSpecs.includes(pattern),
|
|
28
|
+
);
|
|
29
|
+
return {
|
|
30
|
+
...config,
|
|
31
|
+
test: { ...test, include: integrationSpecs, exclude, passWithNoTests: false },
|
|
32
|
+
};
|
|
33
|
+
}
|
package/src/vitest.spec.ts
CHANGED
|
@@ -19,8 +19,9 @@ import path from "node:path";
|
|
|
19
19
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
20
20
|
import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
21
21
|
import type { ViteUserConfig } from "vitest/config";
|
|
22
|
-
import { resolveConfig } from "vitest/node";
|
|
22
|
+
import { createVitest, resolveConfig } from "vitest/node";
|
|
23
23
|
import { createServer, type ViteDevServer } from "vite";
|
|
24
|
+
import { integrationConfig, integrationSpecs } from "./testTiers.js";
|
|
24
25
|
import { createVitestConfig } from "./vitest.js";
|
|
25
26
|
|
|
26
27
|
const fixtureDir = path.resolve(
|
|
@@ -319,3 +320,88 @@ describe("gate load overrides (FZ-7915)", () => {
|
|
|
319
320
|
vi.unstubAllEnvs();
|
|
320
321
|
});
|
|
321
322
|
});
|
|
323
|
+
|
|
324
|
+
describe("test tiers by file name (FZ-10556)", () => {
|
|
325
|
+
const files = [
|
|
326
|
+
"src/a.spec.ts",
|
|
327
|
+
"tests/b.test.tsx",
|
|
328
|
+
"tests/evm.anvil.spec.ts",
|
|
329
|
+
"src/integration.spec.tsx",
|
|
330
|
+
"tests/integration-env.ts",
|
|
331
|
+
"tests/integration.live.spec.ts",
|
|
332
|
+
"tests/integration.live.spec.tsx",
|
|
333
|
+
"tests/keycloak.integration.spec.ts",
|
|
334
|
+
"tests/lifecycle.live.spec.tsx",
|
|
335
|
+
];
|
|
336
|
+
let root: string;
|
|
337
|
+
|
|
338
|
+
beforeEach(() => {
|
|
339
|
+
root = fs.mkdtempSync(path.join(os.tmpdir(), "mpo-test-tiers-"));
|
|
340
|
+
for (const file of files) {
|
|
341
|
+
fs.mkdirSync(path.dirname(path.join(root, file)), { recursive: true });
|
|
342
|
+
fs.writeFileSync(path.join(root, file), "");
|
|
343
|
+
}
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
afterEach(() => {
|
|
347
|
+
fs.rmSync(root, { recursive: true, force: true });
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
// vitest's own discovery decides, so the globs are proven, not re-implemented.
|
|
351
|
+
async function collected(config: object): Promise<string[]> {
|
|
352
|
+
const { include, exclude } = (config as ViteUserConfig).test ?? {};
|
|
353
|
+
const vitest = await createVitest(
|
|
354
|
+
"test",
|
|
355
|
+
{ root, config: false, watch: false, ...(include ? { include } : {}), exclude },
|
|
356
|
+
{ logLevel: "silent" },
|
|
357
|
+
);
|
|
358
|
+
try {
|
|
359
|
+
const specs = await vitest.globTestSpecifications();
|
|
360
|
+
return specs.map((spec) => path.relative(root, spec.moduleId)).sort();
|
|
361
|
+
} finally {
|
|
362
|
+
await vitest.close();
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
const unit = () => createVitestConfig({ tamaguiConfig: false, dedupeReact: false });
|
|
367
|
+
|
|
368
|
+
it("the unit run collects no integration or live spec", async () => {
|
|
369
|
+
expect(await collected(unit())).toEqual([
|
|
370
|
+
"src/a.spec.ts",
|
|
371
|
+
"src/integration.spec.tsx",
|
|
372
|
+
"tests/b.test.tsx",
|
|
373
|
+
"tests/evm.anvil.spec.ts",
|
|
374
|
+
]);
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
it("the integration run collects exactly those", async () => {
|
|
378
|
+
expect(await collected(integrationConfig(unit()))).toEqual([
|
|
379
|
+
"tests/integration.live.spec.ts",
|
|
380
|
+
"tests/integration.live.spec.tsx",
|
|
381
|
+
"tests/keycloak.integration.spec.ts",
|
|
382
|
+
"tests/lifecycle.live.spec.tsx",
|
|
383
|
+
]);
|
|
384
|
+
});
|
|
385
|
+
|
|
386
|
+
it("keeps a package's own excludes in both tiers", () => {
|
|
387
|
+
const own = createVitestConfig({ tamaguiConfig: false, exclude: ["dist"] }) as ViteUserConfig;
|
|
388
|
+
expect(own.test?.exclude).toEqual(["dist", ...integrationSpecs]);
|
|
389
|
+
expect((integrationConfig(own) as ViteUserConfig).test?.exclude).toEqual(["dist"]);
|
|
390
|
+
});
|
|
391
|
+
|
|
392
|
+
it("derives the integration run from a hand-rolled config too", async () => {
|
|
393
|
+
const handRolled = {
|
|
394
|
+
resolve: { conditions: ["source"] },
|
|
395
|
+
test: { environment: "node", exclude: ["node_modules", "dist", ...integrationSpecs] },
|
|
396
|
+
};
|
|
397
|
+
const derived = integrationConfig(handRolled);
|
|
398
|
+
expect(derived.resolve).toBe(handRolled.resolve);
|
|
399
|
+
expect(derived.test).toMatchObject({ environment: "node", exclude: ["node_modules", "dist"] });
|
|
400
|
+
expect(await collected(derived)).toEqual([
|
|
401
|
+
"tests/integration.live.spec.ts",
|
|
402
|
+
"tests/integration.live.spec.tsx",
|
|
403
|
+
"tests/keycloak.integration.spec.ts",
|
|
404
|
+
"tests/lifecycle.live.spec.tsx",
|
|
405
|
+
]);
|
|
406
|
+
});
|
|
407
|
+
});
|
package/src/vitest.ts
CHANGED
|
@@ -6,8 +6,10 @@ import { resolvePathSync } from "mlly";
|
|
|
6
6
|
import { tamaguiPlugin } from "@tamagui/vite-plugin";
|
|
7
7
|
import react from "@vitejs/plugin-react";
|
|
8
8
|
import type { UserConfig } from "vite";
|
|
9
|
+
import { configDefaults } from "vitest/config";
|
|
9
10
|
import { phosphorBarrelBundlePlugin, phosphorTokenColorPlugin } from "./phosphorTokenColor.js";
|
|
10
11
|
import { ensureTamaguiWorkspacePaths } from "./tamaguiWorkspacePaths.js";
|
|
12
|
+
import { integrationSpecs } from "./testTiers.js";
|
|
11
13
|
|
|
12
14
|
const pinReactRequireSetup = path.resolve(
|
|
13
15
|
path.dirname(fileURLToPath(import.meta.url)),
|
|
@@ -176,7 +178,7 @@ export interface CreateVitestConfigOptions {
|
|
|
176
178
|
include?: string[];
|
|
177
179
|
|
|
178
180
|
/**
|
|
179
|
-
* Test exclude patterns
|
|
181
|
+
* Test exclude patterns, on top of `integrationSpecs`. Defaults to vitest default.
|
|
180
182
|
*/
|
|
181
183
|
exclude?: string[];
|
|
182
184
|
|
|
@@ -375,7 +377,7 @@ export function createVitestConfig(options: CreateVitestConfigOptions = {}): Use
|
|
|
375
377
|
},
|
|
376
378
|
};
|
|
377
379
|
if (include) testConfig.include = include;
|
|
378
|
-
|
|
380
|
+
testConfig.exclude = [...(exclude ?? configDefaults.exclude), ...integrationSpecs];
|
|
379
381
|
if (passWithNoTests) testConfig.passWithNoTests = true;
|
|
380
382
|
|
|
381
383
|
return {
|
package/types/vitest.d.ts
CHANGED
package/types/vitest.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"vitest.d.ts","sourceRoot":"","sources":["../src/vitest.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"vitest.d.ts","sourceRoot":"","sources":["../src/vitest.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,MAAM,CAAC;AAmGvC,MAAM,WAAW,yBAAyB;IACxC;;;;OAIG;IACH,aAAa,CAAC,EAAE,MAAM,GAAG,KAAK,CAAC;IAE/B;;OAEG;IACH,WAAW,CAAC,EAAE,OAAO,GAAG,WAAW,GAAG,MAAM,CAAC;IAE7C;;OAEG;IACH,UAAU,CAAC,EAAE,MAAM,EAAE,CAAC;IAEtB;;OAEG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IAErB;;OAEG;IACH,UAAU,CAAC,EAAE,MAAM,EAAE,CAAC;IAEtB;;OAEG;IACH,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;IAExB;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAEjC;;OAEG;IACH,QAAQ,CAAC,EAAE;QACT,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;KACpB,CAAC;IAEF;;;OAGG;IACH,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAE7B;;;OAGG;IACH,WAAW,CAAC,EAAE,OAAO,CAAC;IAEtB;;;OAGG;IACH,eAAe,CAAC,EAAE,MAAM,CAAC;IAEzB;;OAEG;IACH,UAAU,CAAC,EAAE,MAAM,EAAE,CAAC;IAEtB;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IAEnB;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IAEnB;;OAEG;IACH,eAAe,CAAC,EAAE,OAAO,CAAC;CAC3B;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,GAAE,yBAA8B,GAAG,UAAU,CA+NtF"}
|