Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
2 changed files
with
5 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 } |