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+ /** @typedef {import('.').TestCase } TestCase */
27+
1428module . exports = ( function ( ) {
1529 var wait = false ;
30+ /** @type {undefined | Harness } */
1631 var harness ;
1732
33+ /** @type {(opts?: HarnessConfig) => Harness } */
1834 function getHarness ( opts ) {
1935 // this override is here since tests fail via nyc if createHarness is moved upwards
2036 if ( ! harness ) {
@@ -24,6 +40,7 @@ module.exports = (function () {
2440 return harness ;
2541 }
2642
43+ /** @type {(this: Harness, ...args: Parameters<Tape>) => ReturnType<Tape> } */
2744 function lazyLoad ( ) {
2845 // eslint-disable-next-line no-invalid-this
2946 return getHarness ( ) . apply ( this , arguments ) ;
@@ -43,6 +60,7 @@ module.exports = (function () {
4360 return getHarness ( ) . only . apply ( this , arguments ) ;
4461 } ;
4562
63+ /** @type {CreateStream } */
4664 lazyLoad . createStream = function ( opts ) {
4765 var options = opts || { } ;
4866 if ( ! harness ) {
@@ -66,21 +84,23 @@ module.exports = (function () {
6684 return lazyLoad ;
6785} ( ) ) ;
6886
87+ /** @type {CreateHarness } */
6988function createHarness ( conf_ ) {
7089 var results = new Results ( { todoIsOK : ! ! ( process . env . TODO_IS_OK === '1' ) } ) ;
7190 if ( ! conf_ || conf_ . autoclose !== false ) {
7291 results . once ( 'done' , function ( ) { results . close ( ) ; } ) ;
7392 }
7493
75- function test ( name , conf , cb ) {
94+ /** @type {HarnessCallSignatures } */
95+ function test ( /** @type {string } */ name , /** @type {TestOptions } */ conf , /** @type {TestCase } */ cb ) {
7696 var t = new Test ( name , conf , cb ) ;
7797 test . _tests . push ( t ) ;
7898
7999 ( function inspectCode ( st ) {
80- st . on ( 'test' , function sub ( st_ ) {
100+ st . on ( 'test' , /** @type { (st: Test) => void } */ function sub ( st_ ) {
81101 inspectCode ( st_ ) ;
82102 } ) ;
83- st . on ( 'result' , function ( r ) {
103+ st . on ( 'result' , /** @type { (r: Result) => void } */ function ( r ) {
84104 if ( ! r . todo && ! r . ok && typeof r !== 'string' ) { test . _exitCode = 1 ; }
85105 } ) ;
86106 } ( t ) ) ;
@@ -90,21 +110,25 @@ function createHarness(conf_) {
90110 }
91111 test . _results = results ;
92112
93- test . _tests = [ ] ;
113+ /** @type { Test[] } */ test . _tests = [ ] ;
94114
115+ /** @type {CreateStream } */
95116 test . createStream = function ( opts ) {
96117 return results . createStream ( opts ) ;
97118 } ;
98119
120+ /** @type {HarnessEventHandler } */
99121 test . onFinish = function ( cb ) {
100122 results . on ( 'done' , cb ) ;
101123 } ;
102124
125+ /** @type {HarnessEventHandler } */
103126 test . onFailure = function ( cb ) {
104127 results . on ( 'fail' , cb ) ;
105128 } ;
106129
107130 var only = false ;
131+ /** @type {() => Test } */
108132 test . only = function ( ) {
109133 if ( only ) { throw new Error ( 'there can only be one only test' ) ; }
110134 if ( conf_ && conf_ . noOnly ) { throw new Error ( '`only` tests are prohibited' ) ; }
@@ -120,6 +144,7 @@ function createHarness(conf_) {
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,9 +162,11 @@ 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?
169+ // @ts -expect-error unused var
143170 // eslint-disable-next-line no-unused-vars
144171 es . on ( 'error' , function ( err ) { harness . _exitCode = 1 ; } ) ;
145172 }
@@ -180,6 +207,7 @@ function createExitHarness(config, wait) {
180207}
181208
182209module . exports . createHarness = createHarness ;
183- module . exports . Test = Test ;
184- module . exports . test = module . exports ; // tap compat
185- module . exports . test . skip = Test . skip ;
210+ var moduleExports = module . exports ; // this hack is needed because TS has a bug with seemingly circular exports
211+ moduleExports . Test = Test ;
212+ moduleExports . test = module . exports ; // tap compat
213+ moduleExports . skip = Test . skip ;
0 commit comments