Skip to content

Commit

Permalink
make the root tap object a true global singleton
Browse files Browse the repository at this point in the history
fix: #1005
  • Loading branch information
isaacs committed Mar 8, 2024
1 parent 0230e40 commit 2d16686
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/core/dist/commonjs/tap.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions src/core/dist/commonjs/tap.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/core/dist/commonjs/tap.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/core/dist/esm/tap.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions src/core/dist/esm/tap.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/core/dist/esm/tap.js.map

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions src/core/src/tap.ts
Expand Up @@ -25,7 +25,7 @@ import { TestBase } from './test-base.js'

const stdout = proc?.stdout

const privSym = Symbol('private constructor')
const privSym = Symbol.for('TAP private constructor')
type PrivateTAPCtor = {
[privSym]: true
}
Expand Down Expand Up @@ -122,7 +122,7 @@ class TAP extends Test {
// plugins get applied right here:
super(options)

instance = this
instance = g[privSym] = this
this.on('idle', () => maybeAutoend())
this.on('complete', (results: FinalResults) =>
this.#oncomplete(results)
Expand Down Expand Up @@ -483,6 +483,10 @@ const ignoreEPIPE = () => {
}
}

const g = globalThis as typeof globalThis & {
[privSym]: TAP
}

/**
* The exported function instantiates a {@link @tapjs/core!tap.TAP} object if
* we don't already have one, or return the one that was previously
Expand All @@ -492,5 +496,6 @@ const ignoreEPIPE = () => {
* but they are ignored if the instance was already created.
*/
export const tap = (opts?: TestOpts) =>
instance || new TAP(privateTAPCtor, opts)
instance ?? g[privSym] ?? new TAP(privateTAPCtor, opts)

export type { TAP }
4 changes: 4 additions & 0 deletions src/core/test/tap.ts
Expand Up @@ -4,6 +4,9 @@ import stripAnsi from 'strip-ansi-cjs'
import { fileURLToPath } from 'url'
import { env } from '../dist/esm/proc.js'
import { tap } from '../dist/esm/tap.js'
import { createRequire } from 'node:module'
const req = createRequire(import.meta.url)
const { tap: cjsTap } = req('../dist/commonjs/tap.js')

const __filename = fileURLToPath(import.meta.url)

Expand All @@ -23,6 +26,7 @@ type Result = {
const main = () => {
const t = tap({ some: 'options' })
t.equal(t.options.some, 'options')
t.equal(t, cjsTap())
const clean = (s: string) =>
stripAnsi(s)
.replace(/# time=[0-9.]+m?s\n/g, '# time={TIME}\n')
Expand Down

0 comments on commit 2d16686

Please sign in to comment.