@terreno/test 57.9.0 → 57.10.1
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/dist/index.d.ts +1 -1
- package/dist/index.js +2 -1
- package/dist/mongo/mongoServer.d.ts +6 -0
- package/dist/mongo/mongoServer.js +47 -9
- package/dist/preload/registerBackendPreload.js +22 -3
- package/package.json +1 -1
- package/src/index.ts +1 -0
- package/src/mongo/mongoServer.test.ts +26 -1
- package/src/mongo/mongoServer.ts +51 -8
- package/src/preload/registerBackendPreload.ts +24 -3
package/dist/index.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ export { createLogSilencer, registerLogSilencing, type SilenceLogsController, ty
|
|
|
6
6
|
export { registerSentryBunMock } from "./mocks/sentryBun";
|
|
7
7
|
export { buildDatabaseUri, DEFAULT_LOCAL_MONGO_URI, type EnsureTestMongooseConnectedOptions, ensureTestMongooseConnected, type SplitMongoUriResult, splitMongoUri, } from "./mongo/connection";
|
|
8
8
|
export { ensureMongoBinary } from "./mongo/ensureMongoBinary";
|
|
9
|
-
export { getMongoServerUri, initializeModels, type MemoryMongoHandle, type MongoServerOptions, restartMongoServer, startMongoServer, stopMongoServer, waitForDatabaseReady, } from "./mongo/mongoServer";
|
|
9
|
+
export { getMongoServerUri, initializeModels, type MemoryMongoHandle, type MongoServerOptions, resolveMongoLaunchTimeoutMs, restartMongoServer, startMongoServer, stopMongoServer, waitForDatabaseReady, } from "./mongo/mongoServer";
|
|
10
10
|
export { type BackendPreloadOptions, registerBackendPreload, registerSimpleMongoPreload, type SimpleMongoPreloadOptions, } from "./preload/registerBackendPreload";
|
|
11
11
|
export { abortTestTransaction, getTestSession, installTransactionPatches, resetTestSessionAfterReconnect, startTestTransaction, } from "./transaction/testTransaction";
|
|
12
12
|
export { ensureAllIndexes } from "./utils/ensureAllIndexes";
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.waitForDocuments = exports.waitForDocument = exports.timeout = exports.ensureAllIndexes = exports.startTestTransaction = exports.resetTestSessionAfterReconnect = exports.installTransactionPatches = exports.getTestSession = exports.abortTestTransaction = exports.registerSimpleMongoPreload = exports.registerBackendPreload = exports.waitForDatabaseReady = exports.stopMongoServer = exports.startMongoServer = exports.restartMongoServer = exports.initializeModels = exports.getMongoServerUri = exports.ensureMongoBinary = exports.splitMongoUri = exports.ensureTestMongooseConnected = exports.DEFAULT_LOCAL_MONGO_URI = exports.buildDatabaseUri = exports.registerSentryBunMock = exports.registerLogSilencing = exports.createLogSilencer = exports.getBaseServer = exports.authAsUser = exports.setTerrenoTestEnv = exports.createMongoTestCache = void 0;
|
|
3
|
+
exports.waitForDocuments = exports.waitForDocument = exports.timeout = exports.ensureAllIndexes = exports.startTestTransaction = exports.resetTestSessionAfterReconnect = exports.installTransactionPatches = exports.getTestSession = exports.abortTestTransaction = exports.registerSimpleMongoPreload = exports.registerBackendPreload = exports.waitForDatabaseReady = exports.stopMongoServer = exports.startMongoServer = exports.restartMongoServer = exports.resolveMongoLaunchTimeoutMs = exports.initializeModels = exports.getMongoServerUri = exports.ensureMongoBinary = exports.splitMongoUri = exports.ensureTestMongooseConnected = exports.DEFAULT_LOCAL_MONGO_URI = exports.buildDatabaseUri = exports.registerSentryBunMock = exports.registerLogSilencing = exports.createLogSilencer = exports.getBaseServer = exports.authAsUser = exports.setTerrenoTestEnv = exports.createMongoTestCache = void 0;
|
|
4
4
|
var mongoTestCache_1 = require("./cache/mongoTestCache");
|
|
5
5
|
Object.defineProperty(exports, "createMongoTestCache", { enumerable: true, get: function () { return mongoTestCache_1.createMongoTestCache; } });
|
|
6
6
|
var setTerrenoTestEnv_1 = require("./env/setTerrenoTestEnv");
|
|
@@ -24,6 +24,7 @@ Object.defineProperty(exports, "ensureMongoBinary", { enumerable: true, get: fun
|
|
|
24
24
|
var mongoServer_1 = require("./mongo/mongoServer");
|
|
25
25
|
Object.defineProperty(exports, "getMongoServerUri", { enumerable: true, get: function () { return mongoServer_1.getMongoServerUri; } });
|
|
26
26
|
Object.defineProperty(exports, "initializeModels", { enumerable: true, get: function () { return mongoServer_1.initializeModels; } });
|
|
27
|
+
Object.defineProperty(exports, "resolveMongoLaunchTimeoutMs", { enumerable: true, get: function () { return mongoServer_1.resolveMongoLaunchTimeoutMs; } });
|
|
27
28
|
Object.defineProperty(exports, "restartMongoServer", { enumerable: true, get: function () { return mongoServer_1.restartMongoServer; } });
|
|
28
29
|
Object.defineProperty(exports, "startMongoServer", { enumerable: true, get: function () { return mongoServer_1.startMongoServer; } });
|
|
29
30
|
Object.defineProperty(exports, "stopMongoServer", { enumerable: true, get: function () { return mongoServer_1.stopMongoServer; } });
|
|
@@ -16,6 +16,12 @@ export declare const initializeModels: () => Promise<{
|
|
|
16
16
|
modelCount: number;
|
|
17
17
|
modelInitMs: number;
|
|
18
18
|
}>;
|
|
19
|
+
/**
|
|
20
|
+
* How long one mongod may take to start. mongodb-memory-server defaults to 10s, which loaded
|
|
21
|
+
* CI hosts and sandboxed VMs exceed when starting a replica set.
|
|
22
|
+
* Override with `TERRENO_TEST_MONGO_LAUNCH_TIMEOUT_MS`.
|
|
23
|
+
*/
|
|
24
|
+
export declare const resolveMongoLaunchTimeoutMs: () => number;
|
|
19
25
|
/**
|
|
20
26
|
* Starts or reuses the MongoDB server for tests and connects mongoose once per worker.
|
|
21
27
|
*/
|
|
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.getMongoServerUri = exports.stopMongoServer = exports.restartMongoServer = exports.startMongoServer = exports.initializeModels = exports.waitForDatabaseReady = void 0;
|
|
6
|
+
exports.getMongoServerUri = exports.stopMongoServer = exports.restartMongoServer = exports.startMongoServer = exports.resolveMongoLaunchTimeoutMs = exports.initializeModels = exports.waitForDatabaseReady = void 0;
|
|
7
7
|
const luxon_1 = require("luxon");
|
|
8
8
|
const mongoose_1 = __importDefault(require("mongoose"));
|
|
9
9
|
const testLogger_1 = require("../logging/testLogger");
|
|
@@ -65,24 +65,62 @@ const shouldUseReplSet = (options) => {
|
|
|
65
65
|
}
|
|
66
66
|
return process.env.TERRENO_TEST_USE_REPLSET === "true";
|
|
67
67
|
};
|
|
68
|
-
const
|
|
68
|
+
const DEFAULT_LAUNCH_TIMEOUT_MS = 60000;
|
|
69
|
+
const MEMORY_SERVER_START_ATTEMPTS = 2;
|
|
70
|
+
/**
|
|
71
|
+
* How long one mongod may take to start. mongodb-memory-server defaults to 10s, which loaded
|
|
72
|
+
* CI hosts and sandboxed VMs exceed when starting a replica set.
|
|
73
|
+
* Override with `TERRENO_TEST_MONGO_LAUNCH_TIMEOUT_MS`.
|
|
74
|
+
*/
|
|
75
|
+
const resolveMongoLaunchTimeoutMs = () => {
|
|
76
|
+
const raw = process.env.TERRENO_TEST_MONGO_LAUNCH_TIMEOUT_MS?.trim();
|
|
77
|
+
const parsed = raw ? Number(raw) : Number.NaN;
|
|
78
|
+
// mongodb-memory-server ignores launch timeouts below one second
|
|
79
|
+
if (Number.isInteger(parsed) && parsed >= 1000) {
|
|
80
|
+
return parsed;
|
|
81
|
+
}
|
|
82
|
+
return DEFAULT_LAUNCH_TIMEOUT_MS;
|
|
83
|
+
};
|
|
84
|
+
exports.resolveMongoLaunchTimeoutMs = resolveMongoLaunchTimeoutMs;
|
|
85
|
+
const startMemoryServerOnce = async (useReplSet, launchTimeout) => {
|
|
69
86
|
if (useReplSet) {
|
|
70
87
|
const { MongoMemoryReplSet } = await import("mongodb-memory-server-global");
|
|
71
|
-
const replSet =
|
|
88
|
+
const replSet = new MongoMemoryReplSet({
|
|
89
|
+
instanceOpts: [{ launchTimeout }],
|
|
72
90
|
replSet: {
|
|
73
91
|
args: ["--wiredTigerCacheSizeGB", "0.25"],
|
|
74
92
|
count: 1,
|
|
75
93
|
storageEngine: "wiredTiger",
|
|
76
94
|
},
|
|
77
95
|
});
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
96
|
+
try {
|
|
97
|
+
await replSet.start();
|
|
98
|
+
await replSet.waitUntilRunning();
|
|
99
|
+
}
|
|
100
|
+
catch (error) {
|
|
101
|
+
await replSet.stop({ doCleanup: true, force: true }).catch(() => false);
|
|
102
|
+
throw error;
|
|
103
|
+
}
|
|
104
|
+
return replSet;
|
|
81
105
|
}
|
|
82
106
|
const { MongoMemoryServer } = await import("mongodb-memory-server");
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
107
|
+
return MongoMemoryServer.create({ instance: { launchTimeout } });
|
|
108
|
+
};
|
|
109
|
+
const startMemoryServer = async (useReplSet) => {
|
|
110
|
+
const launchTimeout = (0, exports.resolveMongoLaunchTimeoutMs)();
|
|
111
|
+
let lastError;
|
|
112
|
+
for (let attempt = 1; attempt <= MEMORY_SERVER_START_ATTEMPTS; attempt++) {
|
|
113
|
+
try {
|
|
114
|
+
const handle = await startMemoryServerOnce(useReplSet, launchTimeout);
|
|
115
|
+
memoryMongo = handle;
|
|
116
|
+
return handle.getUri();
|
|
117
|
+
}
|
|
118
|
+
catch (error) {
|
|
119
|
+
lastError = error;
|
|
120
|
+
testLogger_1.testLogger.warn(`[mongoServer] In-memory MongoDB failed to start (attempt ${attempt}/${MEMORY_SERVER_START_ATTEMPTS}): ${String(error)}`);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
throw lastError;
|
|
86
124
|
};
|
|
87
125
|
/**
|
|
88
126
|
* Starts or reuses the MongoDB server for tests and connects mongoose once per worker.
|
|
@@ -12,6 +12,7 @@ const testLogger_1 = require("../logging/testLogger");
|
|
|
12
12
|
const sentryBun_1 = require("../mocks/sentryBun");
|
|
13
13
|
const mongoServer_1 = require("../mongo/mongoServer");
|
|
14
14
|
const testTransaction_1 = require("../transaction/testTransaction");
|
|
15
|
+
const MONGO_STOP_TIMEOUT_MS = 30_000;
|
|
15
16
|
const shouldDisableDb = (options) => {
|
|
16
17
|
if (options.disableDb) {
|
|
17
18
|
return true;
|
|
@@ -19,6 +20,11 @@ const shouldDisableDb = (options) => {
|
|
|
19
20
|
return process.env.BUN_TEST_DISABLE_DB === "true";
|
|
20
21
|
};
|
|
21
22
|
let isServerStarted = false;
|
|
23
|
+
// Set when Mongo startup failed, so every later test fails at once instead of waiting on
|
|
24
|
+
// mongoose's command buffering.
|
|
25
|
+
let mongoStartupError;
|
|
26
|
+
/** Startup budget: two launch attempts plus connect and model init. */
|
|
27
|
+
const beforeAllTimeoutMs = () => (0, mongoServer_1.resolveMongoLaunchTimeoutMs)() * 2 + 30000;
|
|
22
28
|
/**
|
|
23
29
|
* Registers Bun test lifecycle hooks for Terreno backend packages.
|
|
24
30
|
* Call once from a package preload file (e.g. `api/src/tests/bunSetup.ts`).
|
|
@@ -44,9 +50,18 @@ const registerBackendPreload = (options = {}) => {
|
|
|
44
50
|
if (!shouldDisableDb(options)) {
|
|
45
51
|
(0, bun_test_1.beforeAll)(async () => {
|
|
46
52
|
if (connectMongoInBeforeAll) {
|
|
53
|
+
if (mongoStartupError !== undefined) {
|
|
54
|
+
throw mongoStartupError;
|
|
55
|
+
}
|
|
47
56
|
if (!isServerStarted) {
|
|
48
57
|
(0, setTerrenoTestEnv_1.setTerrenoTestEnv)(options.testEnv);
|
|
49
|
-
|
|
58
|
+
try {
|
|
59
|
+
await (0, mongoServer_1.startMongoServer)(options.mongo);
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
mongoStartupError = error;
|
|
63
|
+
throw error;
|
|
64
|
+
}
|
|
50
65
|
if (options.loadModels) {
|
|
51
66
|
await options.loadModels();
|
|
52
67
|
}
|
|
@@ -60,12 +75,13 @@ const registerBackendPreload = (options = {}) => {
|
|
|
60
75
|
}
|
|
61
76
|
}
|
|
62
77
|
await options.onBeforeAll?.();
|
|
63
|
-
},
|
|
78
|
+
}, beforeAllTimeoutMs());
|
|
64
79
|
if (connectMongoInBeforeAll) {
|
|
80
|
+
// Stopping an in-memory replica set under load can exceed Bun's 5s hook default
|
|
65
81
|
(0, bun_test_1.afterAll)(async () => {
|
|
66
82
|
await options.onAfterAll?.();
|
|
67
83
|
await (0, mongoServer_1.stopMongoServer)();
|
|
68
|
-
});
|
|
84
|
+
}, MONGO_STOP_TIMEOUT_MS);
|
|
69
85
|
}
|
|
70
86
|
else {
|
|
71
87
|
(0, bun_test_1.afterAll)(async () => {
|
|
@@ -74,6 +90,9 @@ const registerBackendPreload = (options = {}) => {
|
|
|
74
90
|
}
|
|
75
91
|
}
|
|
76
92
|
(0, bun_test_1.beforeEach)(async () => {
|
|
93
|
+
if (mongoStartupError !== undefined) {
|
|
94
|
+
throw new Error(`MongoDB test server failed to start: ${String(mongoStartupError)}`);
|
|
95
|
+
}
|
|
77
96
|
(0, setTerrenoTestEnv_1.setTerrenoTestEnv)(options.testEnv);
|
|
78
97
|
logSilencer?.reapply();
|
|
79
98
|
logSilencer?.clearLogs();
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -2,7 +2,7 @@ import {describe, expect, it} from "bun:test";
|
|
|
2
2
|
import mongoose from "mongoose";
|
|
3
3
|
|
|
4
4
|
import {ensureTestMongooseConnected} from "./connection";
|
|
5
|
-
import {initializeModels, waitForDatabaseReady} from "./mongoServer";
|
|
5
|
+
import {initializeModels, resolveMongoLaunchTimeoutMs, waitForDatabaseReady} from "./mongoServer";
|
|
6
6
|
|
|
7
7
|
describe("mongoServer", () => {
|
|
8
8
|
it("pings the connected database", async () => {
|
|
@@ -32,6 +32,31 @@ describe("mongoServer", () => {
|
|
|
32
32
|
});
|
|
33
33
|
});
|
|
34
34
|
|
|
35
|
+
describe("resolveMongoLaunchTimeoutMs", () => {
|
|
36
|
+
it("defaults to 60 seconds", () => {
|
|
37
|
+
Reflect.deleteProperty(process.env, "TERRENO_TEST_MONGO_LAUNCH_TIMEOUT_MS");
|
|
38
|
+
expect(resolveMongoLaunchTimeoutMs()).toBe(60000);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("reads a positive integer override", () => {
|
|
42
|
+
process.env.TERRENO_TEST_MONGO_LAUNCH_TIMEOUT_MS = "90000";
|
|
43
|
+
try {
|
|
44
|
+
expect(resolveMongoLaunchTimeoutMs()).toBe(90000);
|
|
45
|
+
} finally {
|
|
46
|
+
Reflect.deleteProperty(process.env, "TERRENO_TEST_MONGO_LAUNCH_TIMEOUT_MS");
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it.each(["0", "-5", "1.5", "999", "soon"])("ignores the invalid override %p", (value) => {
|
|
51
|
+
process.env.TERRENO_TEST_MONGO_LAUNCH_TIMEOUT_MS = value;
|
|
52
|
+
try {
|
|
53
|
+
expect(resolveMongoLaunchTimeoutMs()).toBe(60000);
|
|
54
|
+
} finally {
|
|
55
|
+
Reflect.deleteProperty(process.env, "TERRENO_TEST_MONGO_LAUNCH_TIMEOUT_MS");
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
35
60
|
describe("ensureTestMongooseConnected", () => {
|
|
36
61
|
it("returns immediately when mongoose is already connected", async () => {
|
|
37
62
|
expect(mongoose.connection.readyState).toBe(1);
|
package/src/mongo/mongoServer.ts
CHANGED
|
@@ -90,25 +90,68 @@ const shouldUseReplSet = (options: MongoServerOptions): boolean => {
|
|
|
90
90
|
return process.env.TERRENO_TEST_USE_REPLSET === "true";
|
|
91
91
|
};
|
|
92
92
|
|
|
93
|
-
const
|
|
93
|
+
const DEFAULT_LAUNCH_TIMEOUT_MS = 60000;
|
|
94
|
+
const MEMORY_SERVER_START_ATTEMPTS = 2;
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* How long one mongod may take to start. mongodb-memory-server defaults to 10s, which loaded
|
|
98
|
+
* CI hosts and sandboxed VMs exceed when starting a replica set.
|
|
99
|
+
* Override with `TERRENO_TEST_MONGO_LAUNCH_TIMEOUT_MS`.
|
|
100
|
+
*/
|
|
101
|
+
export const resolveMongoLaunchTimeoutMs = (): number => {
|
|
102
|
+
const raw = process.env.TERRENO_TEST_MONGO_LAUNCH_TIMEOUT_MS?.trim();
|
|
103
|
+
const parsed = raw ? Number(raw) : Number.NaN;
|
|
104
|
+
// mongodb-memory-server ignores launch timeouts below one second
|
|
105
|
+
if (Number.isInteger(parsed) && parsed >= 1000) {
|
|
106
|
+
return parsed;
|
|
107
|
+
}
|
|
108
|
+
return DEFAULT_LAUNCH_TIMEOUT_MS;
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
const startMemoryServerOnce = async (
|
|
112
|
+
useReplSet: boolean,
|
|
113
|
+
launchTimeout: number
|
|
114
|
+
): Promise<MemoryMongoHandle> => {
|
|
94
115
|
if (useReplSet) {
|
|
95
116
|
const {MongoMemoryReplSet} = await import("mongodb-memory-server-global");
|
|
96
|
-
const replSet =
|
|
117
|
+
const replSet = new MongoMemoryReplSet({
|
|
118
|
+
instanceOpts: [{launchTimeout}],
|
|
97
119
|
replSet: {
|
|
98
120
|
args: ["--wiredTigerCacheSizeGB", "0.25"],
|
|
99
121
|
count: 1,
|
|
100
122
|
storageEngine: "wiredTiger",
|
|
101
123
|
},
|
|
102
124
|
});
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
125
|
+
try {
|
|
126
|
+
await replSet.start();
|
|
127
|
+
await replSet.waitUntilRunning();
|
|
128
|
+
} catch (error: unknown) {
|
|
129
|
+
await replSet.stop({doCleanup: true, force: true}).catch(() => false);
|
|
130
|
+
throw error;
|
|
131
|
+
}
|
|
132
|
+
return replSet;
|
|
106
133
|
}
|
|
107
134
|
|
|
108
135
|
const {MongoMemoryServer} = await import("mongodb-memory-server");
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
136
|
+
return MongoMemoryServer.create({instance: {launchTimeout}});
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
const startMemoryServer = async (useReplSet: boolean): Promise<string> => {
|
|
140
|
+
const launchTimeout = resolveMongoLaunchTimeoutMs();
|
|
141
|
+
let lastError: unknown;
|
|
142
|
+
for (let attempt = 1; attempt <= MEMORY_SERVER_START_ATTEMPTS; attempt++) {
|
|
143
|
+
try {
|
|
144
|
+
const handle = await startMemoryServerOnce(useReplSet, launchTimeout);
|
|
145
|
+
memoryMongo = handle;
|
|
146
|
+
return handle.getUri();
|
|
147
|
+
} catch (error: unknown) {
|
|
148
|
+
lastError = error;
|
|
149
|
+
testLogger.warn(
|
|
150
|
+
`[mongoServer] In-memory MongoDB failed to start (attempt ${attempt}/${MEMORY_SERVER_START_ATTEMPTS}): ${String(error)}`
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
throw lastError;
|
|
112
155
|
};
|
|
113
156
|
|
|
114
157
|
/**
|
|
@@ -12,6 +12,7 @@ import {registerSentryBunMock} from "../mocks/sentryBun";
|
|
|
12
12
|
import {
|
|
13
13
|
initializeModels,
|
|
14
14
|
type MongoServerOptions,
|
|
15
|
+
resolveMongoLaunchTimeoutMs,
|
|
15
16
|
startMongoServer,
|
|
16
17
|
stopMongoServer,
|
|
17
18
|
} from "../mongo/mongoServer";
|
|
@@ -21,6 +22,8 @@ import {
|
|
|
21
22
|
startTestTransaction,
|
|
22
23
|
} from "../transaction/testTransaction";
|
|
23
24
|
|
|
25
|
+
const MONGO_STOP_TIMEOUT_MS = 30_000;
|
|
26
|
+
|
|
24
27
|
export interface BackendPreloadOptions {
|
|
25
28
|
disableDb?: boolean;
|
|
26
29
|
mongo?: MongoServerOptions;
|
|
@@ -48,6 +51,12 @@ const shouldDisableDb = (options: BackendPreloadOptions): boolean => {
|
|
|
48
51
|
};
|
|
49
52
|
|
|
50
53
|
let isServerStarted = false;
|
|
54
|
+
// Set when Mongo startup failed, so every later test fails at once instead of waiting on
|
|
55
|
+
// mongoose's command buffering.
|
|
56
|
+
let mongoStartupError: unknown;
|
|
57
|
+
|
|
58
|
+
/** Startup budget: two launch attempts plus connect and model init. */
|
|
59
|
+
const beforeAllTimeoutMs = (): number => resolveMongoLaunchTimeoutMs() * 2 + 30000;
|
|
51
60
|
|
|
52
61
|
/**
|
|
53
62
|
* Registers Bun test lifecycle hooks for Terreno backend packages.
|
|
@@ -77,9 +86,17 @@ export const registerBackendPreload = (options: BackendPreloadOptions = {}): voi
|
|
|
77
86
|
if (!shouldDisableDb(options)) {
|
|
78
87
|
beforeAll(async () => {
|
|
79
88
|
if (connectMongoInBeforeAll) {
|
|
89
|
+
if (mongoStartupError !== undefined) {
|
|
90
|
+
throw mongoStartupError;
|
|
91
|
+
}
|
|
80
92
|
if (!isServerStarted) {
|
|
81
93
|
setTerrenoTestEnv(options.testEnv);
|
|
82
|
-
|
|
94
|
+
try {
|
|
95
|
+
await startMongoServer(options.mongo);
|
|
96
|
+
} catch (error: unknown) {
|
|
97
|
+
mongoStartupError = error;
|
|
98
|
+
throw error;
|
|
99
|
+
}
|
|
83
100
|
if (options.loadModels) {
|
|
84
101
|
await options.loadModels();
|
|
85
102
|
}
|
|
@@ -92,13 +109,14 @@ export const registerBackendPreload = (options: BackendPreloadOptions = {}): voi
|
|
|
92
109
|
}
|
|
93
110
|
}
|
|
94
111
|
await options.onBeforeAll?.();
|
|
95
|
-
},
|
|
112
|
+
}, beforeAllTimeoutMs());
|
|
96
113
|
|
|
97
114
|
if (connectMongoInBeforeAll) {
|
|
115
|
+
// Stopping an in-memory replica set under load can exceed Bun's 5s hook default
|
|
98
116
|
afterAll(async () => {
|
|
99
117
|
await options.onAfterAll?.();
|
|
100
118
|
await stopMongoServer();
|
|
101
|
-
});
|
|
119
|
+
}, MONGO_STOP_TIMEOUT_MS);
|
|
102
120
|
} else {
|
|
103
121
|
afterAll(async () => {
|
|
104
122
|
await options.onAfterAll?.();
|
|
@@ -107,6 +125,9 @@ export const registerBackendPreload = (options: BackendPreloadOptions = {}): voi
|
|
|
107
125
|
}
|
|
108
126
|
|
|
109
127
|
beforeEach(async () => {
|
|
128
|
+
if (mongoStartupError !== undefined) {
|
|
129
|
+
throw new Error(`MongoDB test server failed to start: ${String(mongoStartupError)}`);
|
|
130
|
+
}
|
|
110
131
|
setTerrenoTestEnv(options.testEnv);
|
|
111
132
|
logSilencer?.reapply();
|
|
112
133
|
logSilencer?.clearLogs();
|