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
14- module . exports = ( function ( ) {
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('.').createHarness } CreateHarness */
23+ /** @typedef {import('./lib/results').Result } Result */
24+ /** @typedef {import('stream').Writable } WritableStream */
25+
26+ var tape = ( function ( ) {
1527 var wait = false ;
28+ /** @type {undefined | Harness } */
1629 var harness ;
1730
31+ /** @type {(opts?: HarnessConfig) => Harness } */
1832 function getHarness ( opts ) {
1933 // this override is here since tests fail via nyc if createHarness is moved upwards
2034 if ( ! harness ) {
@@ -24,6 +38,7 @@ module.exports = (function () {
2438 return harness ;
2539 }
2640
41+ /** @type {(this: Harness, ...args: Parameters<Tape>) => ReturnType<Tape> } */
2742 function lazyLoad ( ) {
2843 // eslint-disable-next-line no-invalid-this
2944 return getHarness ( ) . apply ( this , arguments ) ;
@@ -43,6 +58,7 @@ module.exports = (function () {
4358 return getHarness ( ) . only . apply ( this , arguments ) ;
4459 } ;
4560
61+ /** @type {CreateStream } */
4662 lazyLoad . createStream = function ( opts ) {
4763 var options = opts || { } ;
4864 if ( ! harness ) {
@@ -66,21 +82,23 @@ module.exports = (function () {
6682 return lazyLoad ;
6783} ( ) ) ;
6884
85+ /** @type {CreateHarness } */
6986function createHarness ( conf_ ) {
7087 var results = new Results ( { todoIsOK : ! ! ( process . env . TODO_IS_OK === '1' ) } ) ;
7188 if ( ! conf_ || conf_ . autoclose !== false ) {
7289 results . once ( 'done' , function ( ) { results . close ( ) ; } ) ;
7390 }
7491
92+ /** @type {(name: string, conf: TestOptions, cb: Test.Callback) => Test } */
7593 function test ( name , conf , cb ) {
7694 var t = new Test ( name , conf , cb ) ;
7795 test . _tests . push ( t ) ;
7896
7997 ( function inspectCode ( st ) {
80- st . on ( 'test' , function sub ( st_ ) {
98+ st . on ( 'test' , /** @type { (st: Test) => void } */ function sub ( st_ ) {
8199 inspectCode ( st_ ) ;
82100 } ) ;
83- st . on ( 'result' , function ( r ) {
101+ st . on ( 'result' , /** @type { (r: Result) => void } */ function ( r ) {
84102 if ( ! r . todo && ! r . ok && typeof r !== 'string' ) { test . _exitCode = 1 ; }
85103 } ) ;
86104 } ( t ) ) ;
@@ -90,21 +108,25 @@ function createHarness(conf_) {
90108 }
91109 test . _results = results ;
92110
93- test . _tests = [ ] ;
111+ /** @type { Test[] } */ test . _tests = [ ] ;
94112
113+ /** @type {CreateStream } */
95114 test . createStream = function ( opts ) {
96115 return results . createStream ( opts ) ;
97116 } ;
98117
118+ /** @type {HarnessEventHandler } */
99119 test . onFinish = function ( cb ) {
100120 results . on ( 'done' , cb ) ;
101121 } ;
102122
123+ /** @type {HarnessEventHandler } */
103124 test . onFailure = function ( cb ) {
104125 results . on ( 'fail' , cb ) ;
105126 } ;
106127
107128 var only = false ;
129+ /** @type {() => Test } */
108130 test . only = function ( ) {
109131 if ( only ) { throw new Error ( 'there can only be one only test' ) ; }
110132 if ( conf_ && conf_ . noOnly ) { throw new Error ( '`only` tests are prohibited' ) ; }
@@ -117,9 +139,11 @@ function createHarness(conf_) {
117139
118140 test . close = function ( ) { results . close ( ) ; } ;
119141
142+ // @ts -expect-error TODO FIXME: why is `test` not assignable to `Harness`???
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 ;
@@ -137,11 +161,12 @@ function createExitHarness(config, wait) {
137161 if ( running ) { return ; }
138162 running = true ;
139163 var stream = harness . createStream ( { objectMode : objectMode } ) ;
140- var es = stream . pipe ( cStream || createDefaultStream ( ) ) ;
164+ // eslint-disable-next-line no-extra-parens
165+ var es = stream . pipe ( /** @type {WritableStream } */ ( cStream || createDefaultStream ( ) ) ) ;
141166 if ( canEmitExit && es ) { // in node v0.4, `es` is `undefined`
142167 // TODO: use `err` arg?
143168 // eslint-disable-next-line no-unused-vars
144- es . on ( 'error' , function ( err ) { harness . _exitCode = 1 ; } ) ;
169+ es . on ( 'error' , function ( _ ) { harness . _exitCode = 1 ; } ) ;
145170 }
146171 stream . on ( 'end' , function ( ) { ended = true ; } ) ;
147172 }
@@ -179,7 +204,9 @@ function createExitHarness(config, wait) {
179204 return harness ;
180205}
181206
207+ module . exports = tape ;
208+
182209module . exports . createHarness = createHarness ;
183210module . exports . Test = Test ;
184- module . exports . test = module . exports ; // tap compat
185- module . exports . test . skip = Test . skip ;
211+ module . exports . test = tape ; // tap compat
212+ module . exports . skip = Test . skip ;
0 commit comments