@terreno/test 57.17.0 → 57.19.2
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/CHANGELOG.md +1681 -0
- package/README.md +6 -0
- package/dist/assertions/expectAPIError.d.ts +18 -0
- package/dist/assertions/expectAPIError.js +33 -0
- package/dist/assertions/strictResponseProperties.d.ts +14 -0
- package/dist/assertions/strictResponseProperties.js +37 -0
- package/dist/cache/mongoTestCache.js +2 -2
- package/dist/cli/collectFiles.d.ts +38 -0
- package/dist/cli/collectFiles.js +73 -0
- package/dist/cli/testHygiene.d.ts +134 -0
- package/dist/cli/testHygiene.js +357 -0
- package/dist/cli/testHygieneCli.d.ts +2 -0
- package/dist/cli/testHygieneCli.js +5 -0
- package/dist/cli/testHygieneRules.d.ts +58 -0
- package/dist/cli/testHygieneRules.js +517 -0
- package/dist/cli/testShard.d.ts +155 -0
- package/dist/cli/testShard.js +377 -0
- package/dist/cli/testShardCli.d.ts +2 -0
- package/dist/cli/testShardCli.js +5 -0
- package/dist/expo/authTokenStorage.d.ts +76 -0
- package/dist/expo/authTokenStorage.js +160 -0
- package/dist/expo/expoModuleMocks.d.ts +24 -0
- package/dist/expo/expoModuleMocks.js +456 -0
- package/dist/expo/libraryModuleMocks.d.ts +17 -0
- package/dist/expo/libraryModuleMocks.js +347 -0
- package/dist/expo/mockHelpers.d.ts +39 -0
- package/dist/expo/mockHelpers.js +45 -0
- package/dist/expo/mockModuleAliases.d.ts +50 -0
- package/dist/expo/mockModuleAliases.js +174 -0
- package/dist/expo/reactNativeMock.d.ts +39 -0
- package/dist/expo/reactNativeMock.js +421 -0
- package/dist/expo/registerExpoTestEnvironment.d.ts +69 -0
- package/dist/expo/registerExpoTestEnvironment.js +163 -0
- package/dist/expo/renderWithTerreno.d.ts +47 -0
- package/dist/expo/renderWithTerreno.js +47 -0
- package/dist/http/authWithToken.d.ts +20 -0
- package/dist/http/authWithToken.js +30 -0
- package/dist/index.d.ts +14 -1
- package/dist/index.js +33 -1
- package/dist/logging/silenceLogs.js +0 -2
- package/dist/mocks/aiSdk.d.ts +77 -0
- package/dist/mocks/aiSdk.js +102 -0
- package/dist/mocks/cloudTasks.d.ts +91 -0
- package/dist/mocks/cloudTasks.js +197 -0
- package/dist/mocks/expoPush.d.ts +53 -0
- package/dist/mocks/expoPush.js +57 -0
- package/dist/mocks/gcs.d.ts +101 -0
- package/dist/mocks/gcs.js +162 -0
- package/dist/mocks/googleAuth.d.ts +69 -0
- package/dist/mocks/googleAuth.js +55 -0
- package/dist/mocks/mockGroup.d.ts +11 -0
- package/dist/mocks/mockGroup.js +21 -0
- package/dist/mocks/notifiers.d.ts +21 -0
- package/dist/mocks/notifiers.js +26 -0
- package/dist/mocks/sendgrid.d.ts +22 -0
- package/dist/mocks/sendgrid.js +32 -0
- package/dist/mocks/twilio.d.ts +72 -0
- package/dist/mocks/twilio.js +64 -0
- package/dist/preload/registerBackendPreload.js +4 -1
- package/dist/time/freezeTime.d.ts +16 -0
- package/dist/time/freezeTime.js +49 -0
- package/dist/transaction/testTransaction.d.ts +43 -6
- package/dist/transaction/testTransaction.js +371 -92
- package/dist/utils/waitUntil.d.ts +22 -0
- package/dist/utils/waitUntil.js +34 -0
- package/package.json +53 -8
- package/src/assertions/expectAPIError.ts +51 -0
- package/src/assertions/strictResponseProperties.ts +42 -0
- package/src/cache/mongoTestCache.ts +2 -2
- package/src/cli/collectFiles.ts +115 -0
- package/src/cli/testHygiene.ts +539 -0
- package/src/cli/testHygieneCli.ts +4 -0
- package/src/cli/testHygieneRules.ts +719 -0
- package/src/cli/testShard.ts +532 -0
- package/src/cli/testShardCli.ts +4 -0
- package/src/expo/authTokenStorage.ts +198 -0
- package/src/expo/expoModuleMocks.ts +503 -0
- package/src/expo/libraryModuleMocks.ts +411 -0
- package/src/expo/mockHelpers.ts +73 -0
- package/src/expo/mockModuleAliases.ts +252 -0
- package/src/expo/reactNativeMock.ts +534 -0
- package/src/expo/registerExpoTestEnvironment.ts +260 -0
- package/src/expo/renderWithTerreno.ts +79 -0
- package/src/http/authWithToken.ts +38 -0
- package/src/index.ts +82 -1
- package/src/logging/silenceLogs.ts +0 -2
- package/src/mocks/aiSdk.ts +199 -0
- package/src/mocks/cloudTasks.ts +323 -0
- package/src/mocks/expoPush.ts +134 -0
- package/src/mocks/gcs.ts +305 -0
- package/src/mocks/googleAuth.ts +136 -0
- package/src/mocks/mockGroup.ts +32 -0
- package/src/mocks/notifiers.ts +36 -0
- package/src/mocks/sendgrid.ts +57 -0
- package/src/mocks/twilio.ts +143 -0
- package/src/preload/registerBackendPreload.ts +4 -1
- package/src/time/freezeTime.ts +61 -0
- package/src/transaction/testTransaction.ts +456 -124
- package/src/utils/waitUntil.ts +47 -0
- package/biome.jsonc +0 -20
- package/bunfig.toml +0 -16
- package/src/cache/mongoTestCache.test.ts +0 -103
- package/src/env/setTerrenoTestEnv.test.ts +0 -26
- package/src/http/authAsUser.test.ts +0 -40
- package/src/http/getBaseServer.test.ts +0 -45
- package/src/logging/setupEnvironment.test.ts +0 -24
- package/src/logging/silenceLogs.test.ts +0 -52
- package/src/logging/testLogger.test.ts +0 -58
- package/src/mocks/sentryBun.test.ts +0 -230
- package/src/mongo/connection.test.ts +0 -43
- package/src/mongo/mongoServer.test.ts +0 -99
- package/src/preload/registerBackendPreload.test.ts +0 -10
- package/src/transaction/testTransaction.test.ts +0 -64
- package/src/utils/helpers.test.ts +0 -46
- package/tsconfig.json +0 -18
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import {mock} from "bun:test";
|
|
2
|
+
import {existsSync, readdirSync, statSync} from "node:fs";
|
|
3
|
+
import {isAbsolute, join, relative, resolve, sep} from "node:path";
|
|
4
|
+
|
|
5
|
+
import type {ModuleExports} from "./mockHelpers";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Path aliases the app resolves, e.g. `{"@hooks": "./hooks", "@store": "./store/index.ts"}`
|
|
9
|
+
* (the `paths` in `tsconfig.json`, without the `/*`). A value may name a directory or a file.
|
|
10
|
+
*/
|
|
11
|
+
export type ModuleAliasMap = Record<string, string>;
|
|
12
|
+
|
|
13
|
+
/** Options for `createModuleAliasMocker`. */
|
|
14
|
+
export interface ModuleAliasMockerOptions {
|
|
15
|
+
/** Alias to directory-or-file map. Relative targets resolve against `rootDir`. */
|
|
16
|
+
aliases: ModuleAliasMap;
|
|
17
|
+
/** Extensions tried when resolving an alias path, in order. Default `.ts`, `.tsx`, `.js`, `.jsx`. */
|
|
18
|
+
extensions?: readonly string[];
|
|
19
|
+
/** Files skipped when a whole directory is mocked. Default: tests, `.d.ts`, and `.web.*` variants. */
|
|
20
|
+
ignore?: RegExp;
|
|
21
|
+
/** Directory relative alias targets resolve against. Default `process.cwd()`. */
|
|
22
|
+
rootDir?: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Options for one `mockModule` call. */
|
|
26
|
+
export interface MockAliasedModuleOptions {
|
|
27
|
+
/** Allow override names the real module(s) do not export. Default `false` (throws on a typo). */
|
|
28
|
+
allowNewExports?: boolean;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** The mocker `createModuleAliasMocker` returns. */
|
|
32
|
+
export interface ModuleAliasMocker {
|
|
33
|
+
/** Every specifier Bun may key `file`'s module cache on: alias forms plus both path forms. */
|
|
34
|
+
getCacheKeys: (file: string) => string[];
|
|
35
|
+
/** The module files an alias specifier covers (one file, or every file under a directory). */
|
|
36
|
+
getModuleFiles: (specifier: string) => string[];
|
|
37
|
+
/** Real exports of a module file or alias specifier, captured once before any mock applied. */
|
|
38
|
+
getRealExports: (specifierOrFile: string) => ModuleExports;
|
|
39
|
+
/**
|
|
40
|
+
* Mocks `specifier` starting from its real exports, replacing only the names in `overrides`.
|
|
41
|
+
* A directory alias (`"@hooks"`) re-registers every file under it; each override applies to
|
|
42
|
+
* whichever files export that name. Registers under every cache key, so imports through the
|
|
43
|
+
* alias, a relative path, or the absolute path all see the mock.
|
|
44
|
+
*/
|
|
45
|
+
mockModule: (
|
|
46
|
+
specifier: string,
|
|
47
|
+
overrides: ModuleExports,
|
|
48
|
+
options?: MockAliasedModuleOptions
|
|
49
|
+
) => void;
|
|
50
|
+
/** Re-registers the real exports for `specifier`, or for every file mocked so far. */
|
|
51
|
+
restore: (specifier?: string) => void;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const DEFAULT_EXTENSIONS = [".ts", ".tsx", ".js", ".jsx"] as const;
|
|
55
|
+
|
|
56
|
+
const DEFAULT_IGNORE = /(?:\.test\.|\.spec\.|\.d\.ts$|\.web\.[jt]sx?$|(?:^|[\\/])__tests__[\\/])/u;
|
|
57
|
+
|
|
58
|
+
/** Real exports keyed by absolute file path, shared across mockers so a snapshot is taken once. */
|
|
59
|
+
const realExportsByFile = new Map<string, ModuleExports>();
|
|
60
|
+
|
|
61
|
+
const toPosix = (path: string): string => path.split(sep).join("/");
|
|
62
|
+
|
|
63
|
+
const isDirectory = (path: string): boolean => existsSync(path) && statSync(path).isDirectory();
|
|
64
|
+
|
|
65
|
+
const isFile = (path: string): boolean => existsSync(path) && statSync(path).isFile();
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Builds a mocker for an app's path aliases. Bun keys its module cache on the specifier it
|
|
69
|
+
* resolved, so `@hooks/useProfile`, `../hooks/useProfile`, and `/abs/hooks/useProfile.ts` can be
|
|
70
|
+
* separate entries. The mocker registers one factory under all of them, built from the module's
|
|
71
|
+
* real exports, so a test overrides a single hook without stubbing the rest of the module.
|
|
72
|
+
*
|
|
73
|
+
* Like any `mock.module` call, a mock stays registered for the rest of the Bun process. Call
|
|
74
|
+
* `mockModule` from `beforeEach` and `restore()` from `afterEach` / `afterAll`.
|
|
75
|
+
*/
|
|
76
|
+
export const createModuleAliasMocker = ({
|
|
77
|
+
aliases,
|
|
78
|
+
extensions = DEFAULT_EXTENSIONS,
|
|
79
|
+
ignore = DEFAULT_IGNORE,
|
|
80
|
+
rootDir = process.cwd(),
|
|
81
|
+
}: ModuleAliasMockerOptions): ModuleAliasMocker => {
|
|
82
|
+
const aliasTargets = Object.entries(aliases)
|
|
83
|
+
.map(([alias, target]): [string, string] => [
|
|
84
|
+
alias.replace(/\/$/u, ""),
|
|
85
|
+
isAbsolute(target) ? target : resolve(rootDir, target),
|
|
86
|
+
])
|
|
87
|
+
// Longest alias first so `@store/sdk` wins over `@store` when both are declared.
|
|
88
|
+
.sort(([a], [b]) => b.length - a.length);
|
|
89
|
+
const mockedFiles = new Set<string>();
|
|
90
|
+
|
|
91
|
+
const resolveFile = (basePath: string): string | undefined => {
|
|
92
|
+
if (isFile(basePath)) {
|
|
93
|
+
return basePath;
|
|
94
|
+
}
|
|
95
|
+
for (const extension of extensions) {
|
|
96
|
+
if (isFile(`${basePath}${extension}`)) {
|
|
97
|
+
return `${basePath}${extension}`;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
if (!isDirectory(basePath)) {
|
|
101
|
+
return undefined;
|
|
102
|
+
}
|
|
103
|
+
for (const extension of extensions) {
|
|
104
|
+
const indexFile = join(basePath, `index${extension}`);
|
|
105
|
+
if (isFile(indexFile)) {
|
|
106
|
+
return indexFile;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return undefined;
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
const isModuleFile = (file: string): boolean =>
|
|
113
|
+
extensions.some((extension) => file.endsWith(extension)) && !ignore.test(file);
|
|
114
|
+
|
|
115
|
+
const listModuleFiles = (directory: string): string[] => {
|
|
116
|
+
const files: string[] = [];
|
|
117
|
+
for (const entry of readdirSync(directory).sort()) {
|
|
118
|
+
if (entry.startsWith(".") || entry === "node_modules") {
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
const path = join(directory, entry);
|
|
122
|
+
if (isDirectory(path)) {
|
|
123
|
+
files.push(...listModuleFiles(path));
|
|
124
|
+
} else if (isModuleFile(path)) {
|
|
125
|
+
files.push(path);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return files;
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
/** Absolute path an alias specifier points at (file or directory), or undefined. */
|
|
132
|
+
const resolveAliasPath = (specifier: string): string | undefined => {
|
|
133
|
+
for (const [alias, target] of aliasTargets) {
|
|
134
|
+
if (specifier === alias) {
|
|
135
|
+
return target;
|
|
136
|
+
}
|
|
137
|
+
if (specifier.startsWith(`${alias}/`)) {
|
|
138
|
+
return join(target, specifier.slice(alias.length + 1));
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return undefined;
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
const getModuleFiles = (specifier: string): string[] => {
|
|
145
|
+
const aliasPath =
|
|
146
|
+
resolveAliasPath(specifier) ?? (isAbsolute(specifier) ? specifier : undefined);
|
|
147
|
+
if (!aliasPath) {
|
|
148
|
+
throw new Error(
|
|
149
|
+
`mockModuleAliases: "${specifier}" matches no alias in ${JSON.stringify(aliases)}`
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
// An exact directory alias covers every file below it, even when the directory has an index.
|
|
153
|
+
if (isDirectory(aliasPath) && (specifier in aliases || !resolveFile(aliasPath))) {
|
|
154
|
+
return listModuleFiles(aliasPath);
|
|
155
|
+
}
|
|
156
|
+
const file = resolveFile(aliasPath);
|
|
157
|
+
if (!file) {
|
|
158
|
+
throw new Error(
|
|
159
|
+
`mockModuleAliases: "${specifier}" resolves to ${aliasPath}, which does not exist`
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
return [file];
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
const getRealExports = (specifierOrFile: string): ModuleExports => {
|
|
166
|
+
const files = isFile(specifierOrFile) ? [specifierOrFile] : getModuleFiles(specifierOrFile);
|
|
167
|
+
const merged: ModuleExports = {};
|
|
168
|
+
for (const file of files) {
|
|
169
|
+
let real = realExportsByFile.get(file);
|
|
170
|
+
if (!real) {
|
|
171
|
+
real = {...(require(file) as ModuleExports)};
|
|
172
|
+
realExportsByFile.set(file, real);
|
|
173
|
+
}
|
|
174
|
+
Object.assign(merged, real);
|
|
175
|
+
}
|
|
176
|
+
return merged;
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
const getCacheKeys = (file: string): string[] => {
|
|
180
|
+
const withoutExtension = file.replace(/\.[cm]?[jt]sx?$/u, "");
|
|
181
|
+
const keys = new Set<string>([file, withoutExtension]);
|
|
182
|
+
for (const [alias, target] of aliasTargets) {
|
|
183
|
+
if (target === file) {
|
|
184
|
+
keys.add(alias);
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
const relativePath = toPosix(relative(target, withoutExtension));
|
|
188
|
+
if (relativePath.startsWith("..") || isAbsolute(relativePath) || !isDirectory(target)) {
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
keys.add(`${alias}/${relativePath}`);
|
|
192
|
+
if (relativePath === "index") {
|
|
193
|
+
keys.add(alias);
|
|
194
|
+
} else if (relativePath.endsWith("/index")) {
|
|
195
|
+
keys.add(`${alias}/${relativePath.slice(0, -"/index".length)}`);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return [...keys];
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
const registerFile = (file: string, exports: ModuleExports): void => {
|
|
202
|
+
const factory = (): ModuleExports => exports;
|
|
203
|
+
for (const key of getCacheKeys(file)) {
|
|
204
|
+
mock.module(key, factory);
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
const mockModule = (
|
|
209
|
+
specifier: string,
|
|
210
|
+
overrides: ModuleExports,
|
|
211
|
+
{allowNewExports = false}: MockAliasedModuleOptions = {}
|
|
212
|
+
): void => {
|
|
213
|
+
const files = getModuleFiles(specifier);
|
|
214
|
+
const realByFile = files.map((file) => getRealExports(file));
|
|
215
|
+
const unmatched = new Set(Object.keys(overrides));
|
|
216
|
+
for (const real of realByFile) {
|
|
217
|
+
for (const name of Object.keys(overrides)) {
|
|
218
|
+
if (name in real) {
|
|
219
|
+
unmatched.delete(name);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
if (unmatched.size > 0 && !allowNewExports) {
|
|
224
|
+
throw new Error(
|
|
225
|
+
`mockModuleAliases: ${[...unmatched].map((name) => `"${name}"`).join(", ")} ` +
|
|
226
|
+
`is not exported by "${specifier}". Check for a typo or a renamed export, or pass {allowNewExports: true}.`
|
|
227
|
+
);
|
|
228
|
+
}
|
|
229
|
+
files.forEach((file, index) => {
|
|
230
|
+
const real = realByFile[index] as ModuleExports;
|
|
231
|
+
const applied: ModuleExports = {...real};
|
|
232
|
+
for (const [name, value] of Object.entries(overrides)) {
|
|
233
|
+
// New names go on the first file so a single-file alias can still add exports.
|
|
234
|
+
if (name in real || (unmatched.has(name) && index === 0)) {
|
|
235
|
+
applied[name] = value;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
registerFile(file, applied);
|
|
239
|
+
mockedFiles.add(file);
|
|
240
|
+
});
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
const restore = (specifier?: string): void => {
|
|
244
|
+
const files = specifier ? getModuleFiles(specifier) : [...mockedFiles];
|
|
245
|
+
for (const file of files) {
|
|
246
|
+
registerFile(file, getRealExports(file));
|
|
247
|
+
mockedFiles.delete(file);
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
return {getCacheKeys, getModuleFiles, getRealExports, mockModule, restore};
|
|
252
|
+
};
|