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