From 556e520e402712bf6c098e9f405b2fd2feeefa2b Mon Sep 17 00:00:00 2001 From: isaacs Date: Thu, 14 Sep 2023 20:50:11 -0700 Subject: [PATCH] mock: actually be resilient against multiple instances Making the service key a global works only as long as there's exactly one instance of this module in the tree. Which, probably always going to be the case. But still. Better to actually not have that be an issue. --- src/mock/.tshy/commonjs.json | 3 ++- src/mock/src/service-key.ts | 20 +++----------------- 2 files changed, 5 insertions(+), 18 deletions(-) diff --git a/src/mock/.tshy/commonjs.json b/src/mock/.tshy/commonjs.json index 6060f0101..fb0752424 100644 --- a/src/mock/.tshy/commonjs.json +++ b/src/mock/.tshy/commonjs.json @@ -6,7 +6,8 @@ "../src/**/*.tsx" ], "exclude": [ - ".../src/**/*.mts" + ".../src/**/*.mts", + "../src/service-key.ts" ], "compilerOptions": { "outDir": "../.tshy-build-tmp/commonjs" diff --git a/src/mock/src/service-key.ts b/src/mock/src/service-key.ts index d352c3cf1..7f0581078 100644 --- a/src/mock/src/service-key.ts +++ b/src/mock/src/service-key.ts @@ -1,17 +1,3 @@ -import { randomBytes } from 'crypto' - -// Need this to be the same for a given thread between both -// CJS and ESM, since it might be loaded in the main thread via -// the import.mjs (esm), the globalPreload script (cjs), and of course the -// user's test program. -const kServiceKey = Symbol.for('@tapjs/mock.globalServiceKey') -const g = globalThis as typeof globalThis & { - [kServiceKey]?: string -} - -// literally the only way to cover both branches would be to break -// mocking entirely, so ignore this. -export const serviceKey = (g[kServiceKey] = - /* c8 ignore start */ - g[kServiceKey] ?? randomBytes(8).toString('hex')) -/* c8 ignore stop */ +//@ts-ignore +import serviceKeyCJS from '../commonjs/service-key.js' +export const { serviceKey } = serviceKeyCJS as { serviceKey: string }