1
1
'use strict' ;
2
2
3
3
var defined = require ( 'defined' ) ;
4
+ var through = require ( '@ljharb/through' ) ;
5
+
4
6
var createDefaultStream = require ( './lib/default_stream' ) ;
5
7
var Test = require ( './lib/test' ) ;
6
8
var Results = require ( './lib/results' ) ;
7
- var through = require ( '@ljharb/through' ) ;
8
9
9
10
var 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 ;
11
13
var canExit = typeof process !== 'undefined' && process
12
14
&& typeof process . exit === 'function' ;
13
15
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
+
14
28
module . exports = ( function ( ) {
15
29
var wait = false ;
30
+ /** @type {undefined | Harness } */
16
31
var harness ;
17
32
33
+ /** @type {(opts?: HarnessConfig) => Harness } */
18
34
function getHarness ( opts ) {
19
35
// this override is here since tests fail via nyc if createHarness is moved upwards
20
36
if ( ! harness ) {
@@ -24,6 +40,7 @@ module.exports = (function () {
24
40
return harness ;
25
41
}
26
42
43
+ /** @type {(this: Harness, ...args: Parameters<Tape>) => ReturnType<Tape> } */
27
44
function lazyLoad ( ) {
28
45
// eslint-disable-next-line no-invalid-this
29
46
return getHarness ( ) . apply ( this , arguments ) ;
@@ -43,6 +60,7 @@ module.exports = (function () {
43
60
return getHarness ( ) . only . apply ( this , arguments ) ;
44
61
} ;
45
62
63
+ /** @type {CreateStream } */
46
64
lazyLoad . createStream = function ( opts ) {
47
65
var options = opts || { } ;
48
66
if ( ! harness ) {
@@ -66,21 +84,23 @@ module.exports = (function () {
66
84
return lazyLoad ;
67
85
} ( ) ) ;
68
86
87
+ /** @type {CreateHarness } */
69
88
function createHarness ( conf_ ) {
70
89
var results = new Results ( { todoIsOK : ! ! ( process . env . TODO_IS_OK === '1' ) } ) ;
71
90
if ( ! conf_ || conf_ . autoclose !== false ) {
72
91
results . once ( 'done' , function ( ) { results . close ( ) ; } ) ;
73
92
}
74
93
94
+ /** @type {(name: string, conf: TestOptions, cb: TestCase) => Test } */
75
95
function test ( name , conf , cb ) {
76
96
var t = new Test ( name , conf , cb ) ;
77
97
test . _tests . push ( t ) ;
78
98
79
99
( function inspectCode ( st ) {
80
- st . on ( 'test' , function sub ( st_ ) {
100
+ st . on ( 'test' , /** @type { (st: Test) => void } */ function sub ( st_ ) {
81
101
inspectCode ( st_ ) ;
82
102
} ) ;
83
- st . on ( 'result' , function ( r ) {
103
+ st . on ( 'result' , /** @type { (r: Result) => void } */ function ( r ) {
84
104
if ( ! r . todo && ! r . ok && typeof r !== 'string' ) { test . _exitCode = 1 ; }
85
105
} ) ;
86
106
} ( t ) ) ;
@@ -90,21 +110,25 @@ function createHarness(conf_) {
90
110
}
91
111
test . _results = results ;
92
112
93
- test . _tests = [ ] ;
113
+ /** @type { Test[] } */ test . _tests = [ ] ;
94
114
115
+ /** @type {CreateStream } */
95
116
test . createStream = function ( opts ) {
96
117
return results . createStream ( opts ) ;
97
118
} ;
98
119
120
+ /** @type {HarnessEventHandler } */
99
121
test . onFinish = function ( cb ) {
100
122
results . on ( 'done' , cb ) ;
101
123
} ;
102
124
125
+ /** @type {HarnessEventHandler } */
103
126
test . onFailure = function ( cb ) {
104
127
results . on ( 'fail' , cb ) ;
105
128
} ;
106
129
107
130
var only = false ;
131
+ /** @type {() => Test } */
108
132
test . only = function ( ) {
109
133
if ( only ) { throw new Error ( 'there can only be one only test' ) ; }
110
134
if ( conf_ && conf_ . noOnly ) { throw new Error ( '`only` tests are prohibited' ) ; }
@@ -117,9 +141,11 @@ function createHarness(conf_) {
117
141
118
142
test . close = function ( ) { results . close ( ) ; } ;
119
143
144
+ // @ts -expect-error TODO FIXME: why is `test` not assignable to `Harness`???
120
145
return test ;
121
146
}
122
147
148
+ /** @type {(conf: Omit<HarnessConfig, 'autoclose'>, wait?: boolean) => Harness } */
123
149
function createExitHarness ( config , wait ) {
124
150
var noOnly = config . noOnly ;
125
151
var objectMode = config . objectMode ;
@@ -137,11 +163,12 @@ function createExitHarness(config, wait) {
137
163
if ( running ) { return ; }
138
164
running = true ;
139
165
var stream = harness . createStream ( { objectMode : objectMode } ) ;
140
- var es = stream . pipe ( cStream || createDefaultStream ( ) ) ;
166
+ // eslint-disable-next-line no-extra-parens
167
+ var es = stream . pipe ( /** @type {WritableStream } */ ( cStream || createDefaultStream ( ) ) ) ;
141
168
if ( canEmitExit && es ) { // in node v0.4, `es` is `undefined`
142
169
// TODO: use `err` arg?
143
170
// eslint-disable-next-line no-unused-vars
144
- es . on ( 'error' , function ( err ) { harness . _exitCode = 1 ; } ) ;
171
+ es . on ( 'error' , function ( _ ) { harness . _exitCode = 1 ; } ) ;
145
172
}
146
173
stream . on ( 'end' , function ( ) { ended = true ; } ) ;
147
174
}
@@ -180,6 +207,7 @@ function createExitHarness(config, wait) {
180
207
}
181
208
182
209
module . 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