@terreno/test 57.18.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.
Files changed (115) hide show
  1. package/CHANGELOG.md +1681 -0
  2. package/README.md +6 -0
  3. package/dist/assertions/expectAPIError.d.ts +18 -0
  4. package/dist/assertions/expectAPIError.js +33 -0
  5. package/dist/assertions/strictResponseProperties.d.ts +14 -0
  6. package/dist/assertions/strictResponseProperties.js +37 -0
  7. package/dist/cache/mongoTestCache.js +2 -2
  8. package/dist/cli/collectFiles.d.ts +38 -0
  9. package/dist/cli/collectFiles.js +73 -0
  10. package/dist/cli/testHygiene.d.ts +134 -0
  11. package/dist/cli/testHygiene.js +357 -0
  12. package/dist/cli/testHygieneCli.d.ts +2 -0
  13. package/dist/cli/testHygieneCli.js +5 -0
  14. package/dist/cli/testHygieneRules.d.ts +58 -0
  15. package/dist/cli/testHygieneRules.js +517 -0
  16. package/dist/cli/testShard.d.ts +155 -0
  17. package/dist/cli/testShard.js +377 -0
  18. package/dist/cli/testShardCli.d.ts +2 -0
  19. package/dist/cli/testShardCli.js +5 -0
  20. package/dist/expo/authTokenStorage.d.ts +76 -0
  21. package/dist/expo/authTokenStorage.js +160 -0
  22. package/dist/expo/expoModuleMocks.d.ts +24 -0
  23. package/dist/expo/expoModuleMocks.js +456 -0
  24. package/dist/expo/libraryModuleMocks.d.ts +17 -0
  25. package/dist/expo/libraryModuleMocks.js +347 -0
  26. package/dist/expo/mockHelpers.d.ts +39 -0
  27. package/dist/expo/mockHelpers.js +45 -0
  28. package/dist/expo/mockModuleAliases.d.ts +50 -0
  29. package/dist/expo/mockModuleAliases.js +174 -0
  30. package/dist/expo/reactNativeMock.d.ts +39 -0
  31. package/dist/expo/reactNativeMock.js +421 -0
  32. package/dist/expo/registerExpoTestEnvironment.d.ts +69 -0
  33. package/dist/expo/registerExpoTestEnvironment.js +163 -0
  34. package/dist/expo/renderWithTerreno.d.ts +47 -0
  35. package/dist/expo/renderWithTerreno.js +47 -0
  36. package/dist/http/authWithToken.d.ts +20 -0
  37. package/dist/http/authWithToken.js +30 -0
  38. package/dist/index.d.ts +14 -1
  39. package/dist/index.js +33 -1
  40. package/dist/logging/silenceLogs.js +0 -2
  41. package/dist/mocks/aiSdk.d.ts +77 -0
  42. package/dist/mocks/aiSdk.js +102 -0
  43. package/dist/mocks/cloudTasks.d.ts +91 -0
  44. package/dist/mocks/cloudTasks.js +197 -0
  45. package/dist/mocks/expoPush.d.ts +53 -0
  46. package/dist/mocks/expoPush.js +57 -0
  47. package/dist/mocks/gcs.d.ts +101 -0
  48. package/dist/mocks/gcs.js +162 -0
  49. package/dist/mocks/googleAuth.d.ts +69 -0
  50. package/dist/mocks/googleAuth.js +55 -0
  51. package/dist/mocks/mockGroup.d.ts +11 -0
  52. package/dist/mocks/mockGroup.js +21 -0
  53. package/dist/mocks/notifiers.d.ts +21 -0
  54. package/dist/mocks/notifiers.js +26 -0
  55. package/dist/mocks/sendgrid.d.ts +22 -0
  56. package/dist/mocks/sendgrid.js +32 -0
  57. package/dist/mocks/twilio.d.ts +72 -0
  58. package/dist/mocks/twilio.js +64 -0
  59. package/dist/preload/registerBackendPreload.js +4 -1
  60. package/dist/time/freezeTime.d.ts +16 -0
  61. package/dist/time/freezeTime.js +49 -0
  62. package/dist/transaction/testTransaction.d.ts +43 -6
  63. package/dist/transaction/testTransaction.js +371 -92
  64. package/dist/utils/waitUntil.d.ts +22 -0
  65. package/dist/utils/waitUntil.js +34 -0
  66. package/package.json +53 -8
  67. package/src/assertions/expectAPIError.ts +51 -0
  68. package/src/assertions/strictResponseProperties.ts +42 -0
  69. package/src/cache/mongoTestCache.ts +2 -2
  70. package/src/cli/collectFiles.ts +115 -0
  71. package/src/cli/testHygiene.ts +539 -0
  72. package/src/cli/testHygieneCli.ts +4 -0
  73. package/src/cli/testHygieneRules.ts +719 -0
  74. package/src/cli/testShard.ts +532 -0
  75. package/src/cli/testShardCli.ts +4 -0
  76. package/src/expo/authTokenStorage.ts +198 -0
  77. package/src/expo/expoModuleMocks.ts +503 -0
  78. package/src/expo/libraryModuleMocks.ts +411 -0
  79. package/src/expo/mockHelpers.ts +73 -0
  80. package/src/expo/mockModuleAliases.ts +252 -0
  81. package/src/expo/reactNativeMock.ts +534 -0
  82. package/src/expo/registerExpoTestEnvironment.ts +260 -0
  83. package/src/expo/renderWithTerreno.ts +79 -0
  84. package/src/http/authWithToken.ts +38 -0
  85. package/src/index.ts +82 -1
  86. package/src/logging/silenceLogs.ts +0 -2
  87. package/src/mocks/aiSdk.ts +199 -0
  88. package/src/mocks/cloudTasks.ts +323 -0
  89. package/src/mocks/expoPush.ts +134 -0
  90. package/src/mocks/gcs.ts +305 -0
  91. package/src/mocks/googleAuth.ts +136 -0
  92. package/src/mocks/mockGroup.ts +32 -0
  93. package/src/mocks/notifiers.ts +36 -0
  94. package/src/mocks/sendgrid.ts +57 -0
  95. package/src/mocks/twilio.ts +143 -0
  96. package/src/preload/registerBackendPreload.ts +4 -1
  97. package/src/time/freezeTime.ts +61 -0
  98. package/src/transaction/testTransaction.ts +456 -124
  99. package/src/utils/waitUntil.ts +47 -0
  100. package/biome.jsonc +0 -20
  101. package/bunfig.toml +0 -16
  102. package/src/cache/mongoTestCache.test.ts +0 -103
  103. package/src/env/setTerrenoTestEnv.test.ts +0 -26
  104. package/src/http/authAsUser.test.ts +0 -40
  105. package/src/http/getBaseServer.test.ts +0 -45
  106. package/src/logging/setupEnvironment.test.ts +0 -24
  107. package/src/logging/silenceLogs.test.ts +0 -52
  108. package/src/logging/testLogger.test.ts +0 -58
  109. package/src/mocks/sentryBun.test.ts +0 -230
  110. package/src/mongo/connection.test.ts +0 -43
  111. package/src/mongo/mongoServer.test.ts +0 -99
  112. package/src/preload/registerBackendPreload.test.ts +0 -10
  113. package/src/transaction/testTransaction.test.ts +0 -64
  114. package/src/utils/helpers.test.ts +0 -46
  115. package/tsconfig.json +0 -18
@@ -0,0 +1,143 @@
1
+ import {type Mock, mock} from "bun:test";
2
+
3
+ import {createMockGroup} from "./mockGroup";
4
+
5
+ export interface TwilioMessageCreateParams {
6
+ body?: string;
7
+ from?: string;
8
+ messagingServiceSid?: string;
9
+ statusCallback?: string;
10
+ to: string;
11
+ [key: string]: unknown;
12
+ }
13
+
14
+ export interface TwilioMockMessage {
15
+ body?: string;
16
+ from?: string;
17
+ sid: string;
18
+ status: string;
19
+ to: string;
20
+ }
21
+
22
+ export interface TwilioMockVerification {
23
+ channel: string;
24
+ sid: string;
25
+ status: string;
26
+ to: string;
27
+ }
28
+
29
+ export interface TwilioMockVerificationCheck {
30
+ status: string;
31
+ to: string;
32
+ valid: boolean;
33
+ }
34
+
35
+ export interface TwilioMockVerifyService {
36
+ verificationChecks: {
37
+ create: TwilioMock["verificationChecksCreate"];
38
+ };
39
+ verifications: {
40
+ create: TwilioMock["verificationsCreate"];
41
+ };
42
+ }
43
+
44
+ export interface TwilioMockClient {
45
+ messages: {create: TwilioMock["messagesCreate"]};
46
+ verify: {v2: {services: TwilioMock["verifyServices"]}};
47
+ }
48
+
49
+ export interface TwilioMock {
50
+ /** `twilio(accountSid, authToken)`; also `new Twilio(...)`. Returns the shared `client`. */
51
+ twilio: Mock<(accountSid?: string, authToken?: string) => TwilioMockClient>;
52
+ client: TwilioMockClient;
53
+ /** `client.messages.create` — resolves `{sid: "SMmock…", status: "queued"}`. */
54
+ messagesCreate: Mock<(params: TwilioMessageCreateParams) => Promise<TwilioMockMessage>>;
55
+ /** `client.verify.v2.services(serviceSid)`. */
56
+ verifyServices: Mock<(serviceSid: string) => TwilioMockVerifyService>;
57
+ /** `services(sid).verifications.create` — resolves `status: "pending"`. */
58
+ verificationsCreate: Mock<
59
+ (params: {channel: string; to: string}) => Promise<TwilioMockVerification>
60
+ >;
61
+ /** `services(sid).verificationChecks.create` — approves every code by default. */
62
+ verificationChecksCreate: Mock<
63
+ (params: {code: string; to: string}) => Promise<TwilioMockVerificationCheck>
64
+ >;
65
+ /** Clears calls, overrides, and the sid counter. */
66
+ reset: () => void;
67
+ }
68
+
69
+ const padSid = (prefix: string, count: number): string =>
70
+ `${prefix}mock${String(count).padStart(28, "0")}`;
71
+
72
+ /**
73
+ * Registers `mock.module("twilio", ...)`. The module is the callable factory, so both
74
+ * `require("twilio")(sid, token)` and `import twilio from "twilio"` work. `twilio` does not need
75
+ * to be installed.
76
+ */
77
+ export const registerTwilioMock = (): TwilioMock => {
78
+ const group = createMockGroup();
79
+ let sidCount = 0;
80
+ const nextSid = (prefix: string): string => {
81
+ sidCount += 1;
82
+ return padSid(prefix, sidCount);
83
+ };
84
+
85
+ const messagesCreate = group.fn(
86
+ async (params: TwilioMessageCreateParams): Promise<TwilioMockMessage> => ({
87
+ body: params.body,
88
+ from: params.from,
89
+ sid: nextSid("SM"),
90
+ status: "queued",
91
+ to: params.to,
92
+ })
93
+ );
94
+ const verificationsCreate = group.fn(
95
+ async (params: {channel: string; to: string}): Promise<TwilioMockVerification> => ({
96
+ channel: params.channel,
97
+ sid: nextSid("VE"),
98
+ status: "pending",
99
+ to: params.to,
100
+ })
101
+ );
102
+ const verificationChecksCreate = group.fn(
103
+ async (params: {code: string; to: string}): Promise<TwilioMockVerificationCheck> => ({
104
+ status: "approved",
105
+ to: params.to,
106
+ valid: true,
107
+ })
108
+ );
109
+ const service: TwilioMockVerifyService = {
110
+ verificationChecks: {create: verificationChecksCreate},
111
+ verifications: {create: verificationsCreate},
112
+ };
113
+ const verifyServices = group.fn((_serviceSid: string): TwilioMockVerifyService => service);
114
+ const client: TwilioMockClient = {
115
+ messages: {create: messagesCreate},
116
+ verify: {v2: {services: verifyServices}},
117
+ };
118
+ const twilio = group.fn((_accountSid?: string, _authToken?: string): TwilioMockClient => client);
119
+
120
+ const reset = (): void => {
121
+ group.reset();
122
+ sidCount = 0;
123
+ };
124
+
125
+ const twilioModule = Object.assign(
126
+ (accountSid?: string, authToken?: string): TwilioMockClient => twilio(accountSid, authToken),
127
+ {Twilio: twilio}
128
+ );
129
+ // `__esModule` makes Bun's `require` return the callable itself rather than a namespace.
130
+ mock.module("twilio", () =>
131
+ Object.assign(twilioModule, {__esModule: true, default: twilioModule})
132
+ );
133
+
134
+ return {
135
+ client,
136
+ messagesCreate,
137
+ reset,
138
+ twilio,
139
+ verificationChecksCreate,
140
+ verificationsCreate,
141
+ verifyServices,
142
+ };
143
+ };
@@ -181,7 +181,10 @@ export const registerSimpleMongoPreload = (options: SimpleMongoPreloadOptions =
181
181
  let uri = process.env.TERRENO_TEST_MONGODB_URI?.trim();
182
182
  if (!uri && process.env.TERRENO_TEST_USE_MEMORY_MONGO === "true") {
183
183
  const {MongoMemoryServer} = await import("mongodb-memory-server");
184
- memoryMongo = await MongoMemoryServer.create();
184
+ // A cold mongod start can exceed mongodb-memory-server's 10s default on CI machines.
185
+ memoryMongo = await MongoMemoryServer.create({
186
+ instance: {launchTimeout: resolveMongoLaunchTimeoutMs()},
187
+ });
185
188
  uri = memoryMongo.getUri();
186
189
  }
187
190
  const connectUri = uri ?? defaultLocalMongoUri;
@@ -0,0 +1,61 @@
1
+ import {afterEach, setSystemTime} from "bun:test";
2
+ import {DateTime} from "luxon";
3
+
4
+ export type FreezeTimeInput = string | DateTime;
5
+
6
+ export interface FreezeTimeOptions {
7
+ /** Restore the previous clock after the current test. Defaults to true. */
8
+ restoreAfterEach?: boolean;
9
+ }
10
+
11
+ // Instants frozen by calls that have not been restored, oldest first. Restoring re-freezes to the
12
+ // newest remaining entry, so nested freezes unwind in any order.
13
+ const activeFreezes: Array<{millis: number}> = [];
14
+
15
+ const toFrozenDateTime = (input: FreezeTimeInput): DateTime => {
16
+ const dateTime = typeof input === "string" ? DateTime.fromISO(input, {setZone: true}) : input;
17
+ if (!dateTime.isValid) {
18
+ const invalidInput = typeof input === "string" ? input : String(input.invalidExplanation);
19
+ throw new Error(`freezeTime: invalid date/time "${invalidInput}" (${dateTime.invalidReason})`);
20
+ }
21
+ return dateTime;
22
+ };
23
+
24
+ /**
25
+ * Freezes the clock at `input` for `Date.now()`, `new Date()`, and Luxon's `DateTime.now()`.
26
+ *
27
+ * Uses Bun's `setSystemTime` and also replaces `Date.now`, so it still wins when a preload has
28
+ * swapped `Date.now` for its own mock. By default it restores the previous clock after the current
29
+ * test (call it in `beforeEach` or inside a test). Returns a restore function for un-freezing
30
+ * earlier; calling it twice is a no-op. Do not mix with direct `setSystemTime` calls in the same
31
+ * test: restoring the last freeze resets the system clock to real time.
32
+ */
33
+ export const freezeTime = (
34
+ input: FreezeTimeInput,
35
+ {restoreAfterEach = true}: FreezeTimeOptions = {}
36
+ ): (() => void) => {
37
+ const millis = toFrozenDateTime(input).toMillis();
38
+ const entry = {millis};
39
+ const previousDateNow = Date.now;
40
+
41
+ activeFreezes.push(entry);
42
+ setSystemTime(new Date(millis));
43
+ Date.now = (): number => millis;
44
+
45
+ let isRestored = false;
46
+ const restore = (): void => {
47
+ if (isRestored) {
48
+ return;
49
+ }
50
+ isRestored = true;
51
+ Date.now = previousDateNow;
52
+ activeFreezes.splice(activeFreezes.indexOf(entry), 1);
53
+ const newest = activeFreezes.at(-1);
54
+ setSystemTime(newest ? new Date(newest.millis) : undefined);
55
+ };
56
+
57
+ if (restoreAfterEach) {
58
+ afterEach(restore);
59
+ }
60
+ return restore;
61
+ };