Skip to content

Commit d4e633c

Browse files
committed
chore(testing): prevent accidentally starting shared test setup without installed cleanup hook
1 parent ac0399f commit d4e633c

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

packages/testing/src/integration-testing-hooks.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,6 @@ export async function downloadCurrentCryptSharedLibrary(
211211
* @export
212212
* @returns {MongodSetup} - Object with information about the started server.
213213
*/
214-
let sharedSetup: MongodSetup | null = null;
215214
export function startTestServer(
216215
id: string,
217216
args: Partial<MongoClusterOptions> = {}
@@ -230,6 +229,8 @@ export function startTestServer(
230229
return server;
231230
}
232231

232+
let installedGlobalAfterHook = false;
233+
let sharedSetup: MongodSetup | null = null;
233234
/**
234235
* Starts or reuse an existing shared local server managed by this process.
235236
*
@@ -242,11 +243,17 @@ export function startTestServer(
242243
* @returns {MongodSetup} - Object with information about the started server.
243244
*/
244245
export function startSharedTestServer(): MongodSetup {
246+
if (!installedGlobalAfterHook) {
247+
throw new Error(
248+
'Trying to start shared test server, but no global after hook was available at module load time'
249+
);
250+
}
251+
245252
if (process.env.MONGOSH_TEST_SERVER_URL) {
246253
return new MongodSetup(process.env.MONGOSH_TEST_SERVER_URL);
247254
}
248255

249-
const server = sharedSetup ?? (sharedSetup = new MongoRunnerSetup('shared'));
256+
const server = (sharedSetup ??= new MongoRunnerSetup('shared'));
250257

251258
before(async function () {
252259
this.timeout(120_000); // Include potential mongod download time.
@@ -258,12 +265,15 @@ export function startSharedTestServer(): MongodSetup {
258265
return server;
259266
}
260267

261-
global.after?.(async function () {
262-
if (sharedSetup !== null) {
263-
this.timeout(30_000);
264-
await sharedSetup.stop();
265-
}
266-
});
268+
if ('after' in globalThis) {
269+
installedGlobalAfterHook = true;
270+
after(async function () {
271+
if (sharedSetup !== null) {
272+
this.timeout(30_000);
273+
await sharedSetup.stop();
274+
}
275+
});
276+
}
267277

268278
// The same as startTestServer(), except that this starts multiple servers
269279
// in parallel in the same before() call.

0 commit comments

Comments
 (0)