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