From 79c117c66469983389afada0beb5502a3c501bd1 Mon Sep 17 00:00:00 2001 From: Alessio Gravili Date: Thu, 19 Sep 2024 21:15:20 -0400 Subject: [PATCH] chore: fix lingering jest and next-server processes in monorepo (#8320) --- test/dev.ts | 12 +++++++++--- test/helpers/stopMemoryDB.ts | 8 ++++++++ test/jest.config.js | 2 ++ 3 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 test/helpers/stopMemoryDB.ts diff --git a/test/dev.ts b/test/dev.ts index d00d440f01..550fa96ee2 100644 --- a/test/dev.ts +++ b/test/dev.ts @@ -50,8 +50,14 @@ if (args.o) { await open(`http://localhost:3000${adminRoute}`) } -// @ts-expect-error -await nextDev({ port: process.env.PORT || 3000, dirname: rootDir }, 'default', rootDir) +const port = process.env.PORT ? Number(process.env.PORT) : 3000 + +await nextDev({ port }, 'default', rootDir) // fetch the admin url to force a render -void fetch(`http://localhost:${process.env.PORT || 3000}${adminRoute}`) +void fetch(`http://localhost:${port}${adminRoute}`) + +// This ensures that the next-server process is killed when this process is killed and doesn't linger around. +process.on('SIGINT', function () { + process.exit(0) +}) diff --git a/test/helpers/stopMemoryDB.ts b/test/helpers/stopMemoryDB.ts new file mode 100644 index 0000000000..aaaf136f61 --- /dev/null +++ b/test/helpers/stopMemoryDB.ts @@ -0,0 +1,8 @@ +// eslint-disable-next-line no-restricted-exports +export default async () => { + if (global._mongoMemoryServer) { + console.log('Stopping memorydb...') + await global._mongoMemoryServer.stop() + console.log('Stopped memorydb') + } +} diff --git a/test/jest.config.js b/test/jest.config.js index d5fb0b87a3..40c5eed18d 100644 --- a/test/jest.config.js +++ b/test/jest.config.js @@ -12,6 +12,8 @@ const customJestConfig = { setupFilesAfterEnv: ['/jest.setup.js'], globalSetup: path.resolve(dirname, './helpers/startMemoryDB.ts'), + globalTeardown: path.resolve(dirname, './helpers/stopMemoryDB.ts'), + moduleNameMapper: { '\\.(css|scss)$': '/helpers/mocks/emptyModule.js', '\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$':