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 {import('.').Harness } Harness */
18+ /** @typedef {import('.').HarnessConfig } HarnessConfig */
19+ /** @typedef {import('.').TestOptions } TestOptions */
20+ /** @typedef {import('.').HarnessEventHandler } HarnessEventHandler */
21+ /** @typedef {import('.').CreateStream } CreateStream */
22+ /** @typedef {import('./lib/results').Result } Result */
23+
1424module . exports = ( function ( ) {
1525 var wait = false ;
26+ /** @type {undefined | Harness } */
1627 var harness ;
1728
29+ /** @type {(opts?: HarnessConfig) => Harness } */
1830 function getHarness ( opts ) {
1931 // this override is here since tests fail via nyc if createHarness is moved upwards
2032 if ( ! harness ) {
@@ -24,6 +36,7 @@ module.exports = (function () {
2436 return harness ;
2537 }
2638
39+ /** @type {(this: Harness, ...args: Parameters<Tape>) => ReturnType<Tape> } */
2740 function lazyLoad ( ) {
2841 // eslint-disable-next-line no-invalid-this
2942 return getHarness ( ) . apply ( this , arguments ) ;
@@ -43,11 +56,13 @@ module.exports = (function () {
4356 return getHarness ( ) . only . apply ( this , arguments ) ;
4457 } ;
4558
59+ /** @type {CreateStream } */
4660 lazyLoad . createStream = function ( opts ) {
4761 var options = opts || { } ;
4862 if ( ! harness ) {
4963 var output = through ( ) ;
50- getHarness ( { stream : output , objectMode : options . objectMode } ) ;
64+ // eslint-disable-next-line no-extra-parens
65+ getHarness ( { stream : /** @type {import('stream').Writable } */ ( output ) , objectMode : options . objectMode } ) ;
5166 return output ;
5267 }
5368 return harness . createStream ( options ) ;
@@ -66,21 +81,23 @@ module.exports = (function () {
6681 return lazyLoad ;
6782} ( ) ) ;
6883
84+ /** @type {Tape['createHarness'] } */
6985function createHarness ( conf_ ) {
7086 var results = new Results ( { todoIsOK : ! ! ( process . env . TODO_IS_OK === '1' ) } ) ;
7187 if ( ! conf_ || conf_ . autoclose !== false ) {
7288 results . once ( 'done' , function ( ) { results . close ( ) ; } ) ;
7389 }
7490
91+ /** @type {(name: string, conf: TestOptions, cb: Test.Callback) => Test } */
7592 function test ( name , conf , cb ) {
7693 var t = new Test ( name , conf , cb ) ;
7794 test . _tests . push ( t ) ;
7895
7996 ( function inspectCode ( st ) {
80- st . on ( 'test' , function sub ( st_ ) {
97+ st . on ( 'test' , /** @type { (st: Test) => void } */ function sub ( st_ ) {
8198 inspectCode ( st_ ) ;
8299 } ) ;
83- st . on ( 'result' , function ( r ) {
100+ st . on ( 'result' , /** @type { (r: Result) => void } */ function ( r ) {
84101 if ( ! r . todo && ! r . ok && typeof r !== 'string' ) { test . _exitCode = 1 ; }
85102 } ) ;
86103 } ( t ) ) ;
@@ -90,21 +107,25 @@ function createHarness(conf_) {
90107 }
91108 test . _results = results ;
92109
93- test . _tests = [ ] ;
110+ /** @type { Test[] } */ test . _tests = [ ] ;
94111
112+ /** @type {CreateStream } */
95113 test . createStream = function ( opts ) {
96114 return results . createStream ( opts ) ;
97115 } ;
98116
117+ /** @type {HarnessEventHandler } */
99118 test . onFinish = function ( cb ) {
100119 results . on ( 'done' , cb ) ;
101120 } ;
102121
122+ /** @type {HarnessEventHandler } */
103123 test . onFailure = function ( cb ) {
104124 results . on ( 'fail' , cb ) ;
105125 } ;
106126
107127 var only = false ;
128+ /** @type {() => ReturnType<typeof test> } */
108129 test . only = function ( ) {
109130 if ( only ) { throw new Error ( 'there can only be one only test' ) ; }
110131 if ( conf_ && conf_ . noOnly ) { throw new Error ( '`only` tests are prohibited' ) ; }
@@ -117,9 +138,12 @@ function createHarness(conf_) {
117138
118139 test . close = function ( ) { results . close ( ) ; } ;
119140
141+ test . run = function ( ) { } ;
142+
120143 return test ;
121144}
122145
146+ /** @type {(conf: Omit<HarnessConfig, 'autoclose'>, wait?: boolean) => Harness } */
123147function createExitHarness ( config , wait ) {
124148 var noOnly = config . noOnly ;
125149 var objectMode = config . objectMode ;
@@ -140,6 +164,7 @@ function createExitHarness(config, wait) {
140164 var es = stream . pipe ( cStream || createDefaultStream ( ) ) ;
141165 if ( canEmitExit && es ) { // in node v0.4, `es` is `undefined`
142166 // TODO: use `err` arg?
167+ // @ts -expect-error
143168 // eslint-disable-next-line no-unused-vars
144169 es . on ( 'error' , function ( err ) { harness . _exitCode = 1 ; } ) ;
145170 }
@@ -180,6 +205,7 @@ function createExitHarness(config, wait) {
180205}
181206
182207module . exports . createHarness = createHarness ;
183- module . exports . Test = Test ;
184- module . exports . test = module . exports ; // tap compat
185- module . exports . test . skip = Test . skip ;
208+ var moduleExports = module . exports ; // this hack is needed because TS has a bug with seemingly circular exports
209+ moduleExports . Test = Test ;
210+ moduleExports . test = module . exports ; // tap compat
211+ moduleExports . skip = Test . skip ;
0 commit comments