@terreno/test 0.31.0 → 56.0.0-beta.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.
@@ -246,6 +246,14 @@ var getReconnectUri = function (publishedUriEnvVar, baseDatabaseName) {
246
246
  }
247
247
  return (0, connection_1.buildDatabaseUri)({ databaseName: mongoose_1.default.connection.name || baseDatabaseName, uri: uri });
248
248
  };
249
+ /** Throws when the driver has no database handle, so callers treat it like a failed ping. */
250
+ var requireDb = function () {
251
+ var db = mongoose_1.default.connection.db;
252
+ if (!db) {
253
+ throw new Error("[mongoTestCache] mongoose connection has no database handle");
254
+ }
255
+ return db;
256
+ };
249
257
  var ensureConnectionReady = function (publishedUriEnvVar, baseDatabaseName) { return __awaiter(void 0, void 0, void 0, function () {
250
258
  var maxQuickAttempts, delayMs, attempt, state, _a, _b;
251
259
  return __generator(this, function (_c) {
@@ -262,7 +270,7 @@ var ensureConnectionReady = function (publishedUriEnvVar, baseDatabaseName) { re
262
270
  _c.label = 2;
263
271
  case 2:
264
272
  _c.trys.push([2, 4, , 5]);
265
- return [4 /*yield*/, mongoose_1.default.connection.db.admin().command({ ping: 1 })];
273
+ return [4 /*yield*/, requireDb().admin().command({ ping: 1 })];
266
274
  case 3:
267
275
  _c.sent();
268
276
  return [2 /*return*/];
@@ -286,7 +294,7 @@ var ensureConnectionReady = function (publishedUriEnvVar, baseDatabaseName) { re
286
294
  })];
287
295
  case 9:
288
296
  _c.sent();
289
- return [4 /*yield*/, mongoose_1.default.connection.db.admin().command({ ping: 1 })];
297
+ return [4 /*yield*/, requireDb().admin().command({ ping: 1 })];
290
298
  case 10:
291
299
  _c.sent();
292
300
  return [4 /*yield*/, (0, testTransaction_1.resetTestSessionAfterReconnect)()];
@@ -326,7 +334,7 @@ var loadTestDataIntoDb = function (cachedCollections) { return __awaiter(void 0,
326
334
  try {
327
335
  for (_a = __values(Object.keys(cachedCollections)), _b = _a.next(); !_b.done; _b = _a.next()) {
328
336
  collectionName = _b.value;
329
- clearPromises.push(mongoose_1.default.connection.db.collection(collectionName).deleteMany({}));
337
+ clearPromises.push(requireDb().collection(collectionName).deleteMany({}));
330
338
  }
331
339
  }
332
340
  catch (e_4_1) { e_4 = { error: e_4_1 }; }
@@ -510,8 +518,10 @@ var createMongoTestCache = function (options) {
510
518
  inflightSetupTestCache = null;
511
519
  });
512
520
  }
513
- return [4 /*yield*/, inflightSetupTestCache];
521
+ // Wrapped because the `finally` above clears the shared slot once it settles.
522
+ return [4 /*yield*/, Promise.resolve(inflightSetupTestCache)];
514
523
  case 1:
524
+ // Wrapped because the `finally` above clears the shared slot once it settles.
515
525
  _a.sent();
516
526
  _a.label = 2;
517
527
  case 2:
@@ -110,9 +110,11 @@ var createLogSilencer = function (options) {
110
110
  var _b;
111
111
  winston_1.default.clear();
112
112
  winston_1.default.add(silentTransport);
113
- var extraLoggers = __spreadArray(__spreadArray([], __read(((_b = options.additionalWinstonLoggers) !== null && _b !== void 0 ? _b : [])), false), [
114
- getApiWinstonLogger(),
115
- ], false).filter(Boolean);
113
+ // Only reach for @terreno/api when the caller has not supplied its logger. Requiring the
114
+ // package from inside api's own test run would load its built dist alongside the sources
115
+ // under test, registering every Mongoose model twice.
116
+ var provided = (_b = options.additionalWinstonLoggers) !== null && _b !== void 0 ? _b : [];
117
+ var extraLoggers = (provided.length > 0 ? provided : [getApiWinstonLogger()]).filter(Boolean);
116
118
  try {
117
119
  for (var extraLoggers_1 = __values(extraLoggers), extraLoggers_1_1 = extraLoggers_1.next(); !extraLoggers_1_1.done; extraLoggers_1_1 = extraLoggers_1.next()) {
118
120
  var extraLogger = extraLoggers_1_1.value;
@@ -1,5 +1,4 @@
1
- import type { Model } from "mongoose";
2
- type ModelQuery<T> = Partial<Record<keyof T, unknown>> & Record<string, unknown>;
1
+ import type { FilterQuery, Model } from "mongoose";
3
2
  export interface WaitForDocumentsOptions {
4
3
  timeoutMs?: number;
5
4
  intervalMs?: number;
@@ -9,7 +8,6 @@ export interface WaitForDocumentsOptions {
9
8
  * Polls until at least `count` documents match the query.
10
9
  * Useful when writes are asynchronous (audit logs, webhooks, etc.).
11
10
  */
12
- export declare const waitForDocuments: <T>(model: Model<T>, query: ModelQuery<T>, count?: number, { timeoutMs, intervalMs, sort }?: WaitForDocumentsOptions) => Promise<T[]>;
11
+ export declare const waitForDocuments: <T>(model: Model<T>, query: FilterQuery<T>, count?: number, { timeoutMs, intervalMs, sort }?: WaitForDocumentsOptions) => Promise<T[]>;
13
12
  /** Polls until a single document matches the query. */
14
- export declare const waitForDocument: <T>(model: Model<T>, query: ModelQuery<T>, options?: WaitForDocumentsOptions) => Promise<T>;
15
- export {};
13
+ export declare const waitForDocument: <T>(model: Model<T>, query: FilterQuery<T>, options?: WaitForDocumentsOptions) => Promise<T>;
package/package.json CHANGED
@@ -18,9 +18,9 @@
18
18
  "mongodb-memory-server": "^11.0.1",
19
19
  "mongodb-memory-server-core": "^11.0.1",
20
20
  "mongodb-memory-server-global": "^11.0.1",
21
- "mongoose": "9.7.4",
21
+ "mongoose": "8.23.0",
22
22
  "supertest": "^7.2.2",
23
- "typescript": "~5.9.2"
23
+ "typescript": "~6.0.3"
24
24
  },
25
25
  "homepage": "https://github.com/FlourishHealth/terreno#readme",
26
26
  "keywords": [
@@ -30,7 +30,7 @@
30
30
  "terreno",
31
31
  "testing"
32
32
  ],
33
- "license": "MIT",
33
+ "license": "Apache-2.0",
34
34
  "main": "dist/index.js",
35
35
  "exports": {
36
36
  ".": {
@@ -41,7 +41,7 @@
41
41
  },
42
42
  "name": "@terreno/test",
43
43
  "peerDependencies": {
44
- "mongoose": "^8.0.0 || ^9.0.0"
44
+ "mongoose": ">=8.0.0"
45
45
  },
46
46
  "publishConfig": {
47
47
  "access": "public"
@@ -61,5 +61,5 @@
61
61
  "test:ci": "bun test"
62
62
  },
63
63
  "types": "dist/index.d.ts",
64
- "version": "0.31.0"
64
+ "version": "56.0.0-beta.0"
65
65
  }
@@ -132,6 +132,15 @@ const getReconnectUri = (publishedUriEnvVar: string, baseDatabaseName: string):
132
132
  return buildDatabaseUri({databaseName: mongoose.connection.name || baseDatabaseName, uri});
133
133
  };
134
134
 
135
+ /** Throws when the driver has no database handle, so callers treat it like a failed ping. */
136
+ const requireDb = (): mongoose.mongo.Db => {
137
+ const {db} = mongoose.connection;
138
+ if (!db) {
139
+ throw new Error("[mongoTestCache] mongoose connection has no database handle");
140
+ }
141
+ return db;
142
+ };
143
+
135
144
  const ensureConnectionReady = async (
136
145
  publishedUriEnvVar: string,
137
146
  baseDatabaseName: string
@@ -143,7 +152,7 @@ const ensureConnectionReady = async (
143
152
  const state = mongoose.connection.readyState;
144
153
  if (state === 1) {
145
154
  try {
146
- await mongoose.connection.db!.admin().command({ping: 1});
155
+ await requireDb().admin().command({ping: 1});
147
156
  return;
148
157
  } catch {
149
158
  // stale connection
@@ -160,7 +169,7 @@ const ensureConnectionReady = async (
160
169
  serverSelectionTimeoutMS: 2000,
161
170
  socketTimeoutMS: 2000,
162
171
  });
163
- await mongoose.connection.db!.admin().command({ping: 1});
172
+ await requireDb().admin().command({ping: 1});
164
173
  await resetTestSessionAfterReconnect();
165
174
  return;
166
175
  } catch {
@@ -180,7 +189,7 @@ const ensureConnectionReady = async (
180
189
  const loadTestDataIntoDb = async (cachedCollections: Record<string, unknown[]>): Promise<void> => {
181
190
  const clearPromises: Array<Promise<unknown>> = [];
182
191
  for (const collectionName of Object.keys(cachedCollections)) {
183
- clearPromises.push(mongoose.connection.db!.collection(collectionName).deleteMany({}));
192
+ clearPromises.push(requireDb().collection(collectionName).deleteMany({}));
184
193
  }
185
194
  await Promise.all(clearPromises);
186
195
 
@@ -287,7 +296,8 @@ export const createMongoTestCache = (options: MongoTestCacheOptions): MongoTestC
287
296
  inflightSetupTestCache = null;
288
297
  });
289
298
  }
290
- await inflightSetupTestCache;
299
+ // Wrapped because the `finally` above clears the shared slot once it settles.
300
+ await Promise.resolve(inflightSetupTestCache);
291
301
  }
292
302
 
293
303
  if (!cachedCollectionsInMemory) {
@@ -82,10 +82,13 @@ export const createLogSilencer = (options: SilenceLogsOptions = {}): SilenceLogs
82
82
  winston.clear();
83
83
  winston.add(silentTransport);
84
84
 
85
- const extraLoggers = [
86
- ...(options.additionalWinstonLoggers ?? []),
87
- getApiWinstonLogger(),
88
- ].filter(Boolean) as winston.Logger[];
85
+ // Only reach for @terreno/api when the caller has not supplied its logger. Requiring the
86
+ // package from inside api's own test run would load its built dist alongside the sources
87
+ // under test, registering every Mongoose model twice.
88
+ const provided = options.additionalWinstonLoggers ?? [];
89
+ const extraLoggers = (provided.length > 0 ? provided : [getApiWinstonLogger()]).filter(
90
+ Boolean
91
+ ) as winston.Logger[];
89
92
 
90
93
  for (const extraLogger of extraLoggers) {
91
94
  extraLogger.clear();
@@ -1,6 +1,4 @@
1
- import type {Model} from "mongoose";
2
-
3
- type ModelQuery<T> = Partial<Record<keyof T, unknown>> & Record<string, unknown>;
1
+ import type {FilterQuery, Model} from "mongoose";
4
2
 
5
3
  export interface WaitForDocumentsOptions {
6
4
  timeoutMs?: number;
@@ -14,13 +12,13 @@ export interface WaitForDocumentsOptions {
14
12
  */
15
13
  export const waitForDocuments = async <T>(
16
14
  model: Model<T>,
17
- query: ModelQuery<T>,
15
+ query: FilterQuery<T>,
18
16
  count = 1,
19
17
  {timeoutMs = 5000, intervalMs = 100, sort}: WaitForDocumentsOptions = {}
20
18
  ): Promise<T[]> => {
21
19
  const start = Date.now();
22
20
  while (Date.now() - start < timeoutMs) {
23
- let q = model.find(query as never);
21
+ let q = model.find(query);
24
22
  if (sort) {
25
23
  q = q.sort(sort);
26
24
  }
@@ -31,7 +29,7 @@ export const waitForDocuments = async <T>(
31
29
  await new Promise((resolve) => setTimeout(resolve, intervalMs));
32
30
  }
33
31
 
34
- const found = await model.countDocuments(query as never);
32
+ const found = await model.countDocuments(query);
35
33
  throw new Error(
36
34
  `Timed out after ${timeoutMs}ms waiting for ${count} document(s) on ${model.modelName} matching ${JSON.stringify(query)} (found ${found})`
37
35
  );
@@ -40,7 +38,7 @@ export const waitForDocuments = async <T>(
40
38
  /** Polls until a single document matches the query. */
41
39
  export const waitForDocument = async <T>(
42
40
  model: Model<T>,
43
- query: ModelQuery<T>,
41
+ query: FilterQuery<T>,
44
42
  options: WaitForDocumentsOptions = {}
45
43
  ): Promise<T> => {
46
44
  const results = await waitForDocuments(model, query, 1, options);
package/tsconfig.json CHANGED
@@ -7,6 +7,7 @@
7
7
  "downlevelIteration": true,
8
8
  "esModuleInterop": true,
9
9
  "forceConsistentCasingInFileNames": true,
10
+ "ignoreDeprecations": "6.0",
10
11
  "jsx": "react",
11
12
  "lib": ["es2017", "dom", "es2017.object"],
12
13
  "module": "commonjs",
@@ -20,11 +21,13 @@
20
21
  "outDir": "./dist",
21
22
  "preserveConstEnums": true,
22
23
  "removeComments": false,
24
+ "rootDir": "./src",
23
25
  "skipLibCheck": true,
24
26
  "strict": true,
25
27
  "strictFunctionTypes": true,
26
28
  "strictNullChecks": true,
27
- "target": "es5"
29
+ "target": "es5",
30
+ "types": ["bun", "node"]
28
31
  },
29
32
  "exclude": ["node_modules", "src/mongo/ensureMongoBinary.ts"],
30
33
  "include": ["src/**/*.ts"]
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2026 Flourish Health, Inc.
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.