11'use strict' ;
22
33var defined = require ( 'defined' ) ;
4+ var through = require ( '@ljharb/through' ) ;
5+
46var createDefaultStream = require ( './lib/default_stream' ) ;
57var Test = require ( './lib/test' ) ;
68var Results = require ( './lib/results' ) ;
7- var through = require ( '@ljharb/through' ) ;
89
910var canEmitExit = typeof process !== 'undefined' && process
10- && typeof process . on === 'function' && process . browser !== true ;
11+ // eslint-disable-next-line no-extra-parens
12+ && typeof process . on === 'function' && /** @type {{ browser?: boolean } } */ ( process ) . browser !== true ;
1113var canExit = typeof process !== 'undefined' && process
1214 && typeof process . exit === 'function' ;
1315
16+ /** @typedef {import('.') } Tape */
17+ /** @typedef {import('.').Harness } Harness */
18+ /** @typedef {import('.').HarnessConfig } HarnessConfig */
19+ /** @typedef {import('.').HarnessCallSignatures } HarnessCallSignatures */
20+ /** @typedef {import('.').TestOptions } TestOptions */
21+ /** @typedef {import('.').HarnessEventHandler } HarnessEventHandler */
22+ /** @typedef {import('.').CreateStream } CreateStream */
23+ /** @typedef {import('.').createHarness } CreateHarness */
24+ /** @typedef {import('./lib/results').Result } Result */
25+ /** @typedef {import('stream').Writable } WritableStream */
26+
1427module . exports = ( function ( ) {
1528 var wait = false ;
29+ /** @type {undefined | Harness } */
1630 var harness ;
1731
32+ /** @type {(opts?: HarnessConfig) => Harness } */
1833 function getHarness ( opts ) {
1934 // this override is here since tests fail via nyc if createHarness is moved upwards
2035 if ( ! harness ) {
@@ -24,6 +39,7 @@ module.exports = (function () {
2439 return harness ;
2540 }
2641
42+ /** @type {(this: Harness, ...args: Parameters<Tape>) => ReturnType<Tape> } */
2743 function lazyLoad ( ) {
2844 // eslint-disable-next-line no-invalid-this
2945 return getHarness ( ) . apply ( this , arguments ) ;
@@ -43,6 +59,7 @@ module.exports = (function () {
4359 return getHarness ( ) . only . apply ( this , arguments ) ;
4460 } ;
4561
62+ /** @type {CreateStream } */
4663 lazyLoad . createStream = function ( opts ) {
4764 var options = opts || { } ;
4865 if ( ! harness ) {
@@ -66,21 +83,23 @@ module.exports = (function () {
6683 return lazyLoad ;
6784} ( ) ) ;
6885
86+ /** @type {CreateHarness } */
6987function createHarness ( conf_ ) {
7088 var results = new Results ( { todoIsOK : ! ! ( process . env . TODO_IS_OK === '1' ) } ) ;
7189 if ( ! conf_ || conf_ . autoclose !== false ) {
7290 results . once ( 'done' , function ( ) { results . close ( ) ; } ) ;
7391 }
7492
93+ /** @type {(name: string, conf: TestOptions, cb: Test.Callback) => Test } */
7594 function test ( name , conf , cb ) {
7695 var t = new Test ( name , conf , cb ) ;
7796 test . _tests . push ( t ) ;
7897
7998 ( function inspectCode ( st ) {
80- st . on ( 'test' , function sub ( st_ ) {
99+ st . on ( 'test' , /** @type { (st: Test) => void } */ function sub ( st_ ) {
81100 inspectCode ( st_ ) ;
82101 } ) ;
83- st . on ( 'result' , function ( r ) {
102+ st . on ( 'result' , /** @type { (r: Result) => void } */ function ( r ) {
84103 if ( ! r . todo && ! r . ok && typeof r !== 'string' ) { test . _exitCode = 1 ; }
85104 } ) ;
86105 } ( t ) ) ;
@@ -90,21 +109,25 @@ function createHarness(conf_) {
90109 }
91110 test . _results = results ;
92111
93- test . _tests = [ ] ;
112+ /** @type { Test[] } */ test . _tests = [ ] ;
94113
114+ /** @type {CreateStream } */
95115 test . createStream = function ( opts ) {
96116 return results . createStream ( opts ) ;
97117 } ;
98118
119+ /** @type {HarnessEventHandler } */
99120 test . onFinish = function ( cb ) {
100121 results . on ( 'done' , cb ) ;
101122 } ;
102123
124+ /** @type {HarnessEventHandler } */
103125 test . onFailure = function ( cb ) {
104126 results . on ( 'fail' , cb ) ;
105127 } ;
106128
107129 var only = false ;
130+ /** @type {() => Test } */
108131 test . only = function ( ) {
109132 if ( only ) { throw new Error ( 'there can only be one only test' ) ; }
110133 if ( conf_ && conf_ . noOnly ) { throw new Error ( '`only` tests are prohibited' ) ; }
@@ -117,9 +140,11 @@ function createHarness(conf_) {
117140
118141 test . close = function ( ) { results . close ( ) ; } ;
119142
143+ // @ts -expect-error TODO FIXME: why is `test` not assignable to `Harness`???
120144 return test ;
121145}
122146
147+ /** @type {(conf: Omit<HarnessConfig, 'autoclose'>, wait?: boolean) => Harness } */
123148function createExitHarness ( config , wait ) {
124149 var noOnly = config . noOnly ;
125150 var objectMode = config . objectMode ;
@@ -137,11 +162,12 @@ function createExitHarness(config, wait) {
137162 if ( running ) { return ; }
138163 running = true ;
139164 var stream = harness . createStream ( { objectMode : objectMode } ) ;
140- var es = stream . pipe ( cStream || createDefaultStream ( ) ) ;
165+ // eslint-disable-next-line no-extra-parens
166+ var es = stream . pipe ( /** @type {WritableStream } */ ( cStream || createDefaultStream ( ) ) ) ;
141167 if ( canEmitExit && es ) { // in node v0.4, `es` is `undefined`
142168 // TODO: use `err` arg?
143169 // eslint-disable-next-line no-unused-vars
144- es . on ( 'error' , function ( err ) { harness . _exitCode = 1 ; } ) ;
170+ es . on ( 'error' , function ( _ ) { harness . _exitCode = 1 ; } ) ;
145171 }
146172 stream . on ( 'end' , function ( ) { ended = true ; } ) ;
147173 }
@@ -180,6 +206,7 @@ function createExitHarness(config, wait) {
180206}
181207
182208module . exports . createHarness = createHarness ;
183- module . exports . Test = Test ;
184- module . exports . test = module . exports ; // tap compat
185- module . exports . test . skip = Test . skip ;
209+ var moduleExports = module . exports ; // this hack is needed because TS has a bug with seemingly circular exports
210+ moduleExports . Test = Test ;
211+ moduleExports . test = module . exports ; // tap compat
212+ moduleExports . skip = Test . skip ;
0 commit comments