From 62687301e5646b93beb1eac6fa47fdeeba8a1cbb Mon Sep 17 00:00:00 2001 From: Michal Piechowiak Date: Thu, 26 Jan 2023 12:21:16 +0100 Subject: [PATCH] chore: Adjust jest environment conditionally for OS (#37452) Co-authored-by: LekoArts --- jest.environment.ts | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/jest.environment.ts b/jest.environment.ts index b82769d8d9dc6..88a5cea9ce777 100644 --- a/jest.environment.ts +++ b/jest.environment.ts @@ -1,4 +1,7 @@ const NodeEnvironment = require(`jest-environment-node`).TestEnvironment +const fsExtra = require(`fs-extra`) + +const isWindows = process.platform === `win32` class CustomEnvironment extends NodeEnvironment { constructor(config, context) { @@ -10,10 +13,23 @@ class CustomEnvironment extends NodeEnvironment { // this prevent dangling open handles that sometimes cause problems // particularly in windows tests (failures to move or delete a db file) if (this.global.__GATSBY_OPEN_ROOT_LMDBS) { - for (const rootDb of this.global.__GATSBY_OPEN_ROOT_LMDBS.values()) { - await rootDb.clearAsync() - await rootDb.close() + if (isWindows) { + for (const rootDb of this.global.__GATSBY_OPEN_ROOT_LMDBS.values()) { + await rootDb.clearAsync() + await rootDb.close() + } + } else { + for (const [ + dbPath, + rootDb, + ] of this.global.__GATSBY_OPEN_ROOT_LMDBS.entries()) { + if (rootDb.isOperational()) { + await rootDb.close() + } + await fsExtra.remove(dbPath) + } } + this.global.__GATSBY_OPEN_ROOT_LMDBS = undefined } await super.teardown()