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
11+ // @ts -expect-error i think old browserify uses `process.browser`
1012 && typeof process . on === 'function' && process . browser !== true ;
1113var canExit = typeof process !== 'undefined' && process
1214 && typeof process . exit === 'function' ;
1315
16+ /** @typedef {import('.') } Tape */
17+ /** @typedef {ThisParameterType<Tape> } Harness */
18+ /** @typedef {import('.').HarnessConfig } HarnessConfig */
19+
1420module . exports = ( function ( ) {
1521 var wait = false ;
22+ /** @type {undefined | Harness } */
1623 var harness ;
1724
25+ /** @type {(opts?: HarnessConfig) => Harness } */
1826 function getHarness ( opts ) {
1927 // this override is here since tests fail via nyc if createHarness is moved upwards
2028 if ( ! harness ) {
@@ -24,6 +32,7 @@ module.exports = (function () {
2432 return harness ;
2533 }
2634
35+ /** @type {(this: Harness, ...args: Parameters<Tape>) => ReturnType<Tape> } */
2736 function lazyLoad ( ) {
2837 // eslint-disable-next-line no-invalid-this
2938 return getHarness ( ) . apply ( this , arguments ) ;
@@ -43,11 +52,13 @@ module.exports = (function () {
4352 return getHarness ( ) . only . apply ( this , arguments ) ;
4453 } ;
4554
55+ /** @type {import('.').CreateStream } */
4656 lazyLoad . createStream = function ( opts ) {
4757 var options = opts || { } ;
4858 if ( ! harness ) {
4959 var output = through ( ) ;
50- getHarness ( { stream : output , objectMode : options . objectMode } ) ;
60+ // eslint-disable-next-line no-extra-parens
61+ getHarness ( { stream : /** @type {import('stream').Writable } */ ( output ) , objectMode : options . objectMode } ) ;
5162 return output ;
5263 }
5364 return harness . createStream ( options ) ;
@@ -66,21 +77,23 @@ module.exports = (function () {
6677 return lazyLoad ;
6778} ( ) ) ;
6879
80+ /** @type {Tape['createHarness'] } */
6981function createHarness ( conf_ ) {
7082 var results = new Results ( { todoIsOK : ! ! ( process . env . TODO_IS_OK === '1' ) } ) ;
7183 if ( ! conf_ || conf_ . autoclose !== false ) {
7284 results . once ( 'done' , function ( ) { results . close ( ) ; } ) ;
7385 }
7486
87+ /** @type {(name: string, conf: import('.').TestOptions, cb: Test.Callback) => Test } */
7588 function test ( name , conf , cb ) {
7689 var t = new Test ( name , conf , cb ) ;
7790 test . _tests . push ( t ) ;
7891
7992 ( function inspectCode ( st ) {
80- st . on ( 'test' , function sub ( st_ ) {
93+ st . on ( 'test' , /** @type { (st: Test) => void } */ function sub ( st_ ) {
8194 inspectCode ( st_ ) ;
8295 } ) ;
83- st . on ( 'result' , function ( r ) {
96+ st . on ( 'result' , /** @type { (r: import('./lib/results').Result) => void } */ function ( r ) {
8497 if ( ! r . todo && ! r . ok && typeof r !== 'string' ) { test . _exitCode = 1 ; }
8598 } ) ;
8699 } ( t ) ) ;
@@ -90,21 +103,25 @@ function createHarness(conf_) {
90103 }
91104 test . _results = results ;
92105
93- test . _tests = [ ] ;
106+ /** @type { Test[] } */ test . _tests = [ ] ;
94107
108+ /** @type {import('.').CreateStream } */
95109 test . createStream = function ( opts ) {
96110 return results . createStream ( opts ) ;
97111 } ;
98112
113+ /** @type {import('.').HarnessEventHandler } */
99114 test . onFinish = function ( cb ) {
100115 results . on ( 'done' , cb ) ;
101116 } ;
102117
118+ /** @type {import('.').HarnessEventHandler } */
103119 test . onFailure = function ( cb ) {
104120 results . on ( 'fail' , cb ) ;
105121 } ;
106122
107123 var only = false ;
124+ /** @type {() => ReturnType<typeof test> } */
108125 test . only = function ( ) {
109126 if ( only ) { throw new Error ( 'there can only be one only test' ) ; }
110127 if ( conf_ && conf_ . noOnly ) { throw new Error ( '`only` tests are prohibited' ) ; }
@@ -117,9 +134,12 @@ function createHarness(conf_) {
117134
118135 test . close = function ( ) { results . close ( ) ; } ;
119136
137+ test . run = function ( ) { } ;
138+
120139 return test ;
121140}
122141
142+ /** @type {(conf: Omit<HarnessConfig, 'autoclose'>, wait?: boolean) => Harness } */
123143function createExitHarness ( config , wait ) {
124144 var noOnly = config . noOnly ;
125145 var objectMode = config . objectMode ;
@@ -140,6 +160,7 @@ function createExitHarness(config, wait) {
140160 var es = stream . pipe ( cStream || createDefaultStream ( ) ) ;
141161 if ( canEmitExit && es ) { // in node v0.4, `es` is `undefined`
142162 // TODO: use `err` arg?
163+ // @ts -expect-error
143164 // eslint-disable-next-line no-unused-vars
144165 es . on ( 'error' , function ( err ) { harness . _exitCode = 1 ; } ) ;
145166 }
@@ -180,6 +201,7 @@ function createExitHarness(config, wait) {
180201}
181202
182203module . exports . createHarness = createHarness ;
183- module . exports . Test = Test ;
184- module . exports . test = module . exports ; // tap compat
185- module . exports . test . skip = Test . skip ;
204+ var moduleExports = module . exports ; // this hack is needed because TS has a bug with seemingly circular exports
205+ moduleExports . Test = Test ;
206+ moduleExports . test = module . exports ; // tap compat
207+ moduleExports . skip = Test . skip ;
0 commit comments