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
11
+ // @ts -expect-error i think old browserify uses `process.browser`
10
12
&& typeof process . on === 'function' && 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 {ThisParameterType<Tape> } Harness */
18
+ /** @typedef {import('.').HarnessConfig } HarnessConfig */
19
+
14
20
module . exports = ( function ( ) {
15
21
var wait = false ;
22
+ /** @type {undefined | Harness } */
16
23
var harness ;
17
24
25
+ /** @type {(opts?: HarnessConfig) => Harness } */
18
26
function getHarness ( opts ) {
19
27
// this override is here since tests fail via nyc if createHarness is moved upwards
20
28
if ( ! harness ) {
@@ -24,6 +32,7 @@ module.exports = (function () {
24
32
return harness ;
25
33
}
26
34
35
+ /** @type {(this: Harness, ...args: Parameters<Tape>) => ReturnType<Tape> } */
27
36
function lazyLoad ( ) {
28
37
// eslint-disable-next-line no-invalid-this
29
38
return getHarness ( ) . apply ( this , arguments ) ;
@@ -43,11 +52,13 @@ module.exports = (function () {
43
52
return getHarness ( ) . only . apply ( this , arguments ) ;
44
53
} ;
45
54
55
+ /** @type {import('.').CreateStream } */
46
56
lazyLoad . createStream = function ( opts ) {
47
57
var options = opts || { } ;
48
58
if ( ! harness ) {
49
59
var output = through ( ) ;
50
- getHarness ( { stream : output , objectMode : options . objectMode } ) ;
60
+ // eslint-disable-next-line no-extra-parens
61
+ getHarness ( { stream : /** @type {import('stream').Writable } */ ( output ) , objectMode : options . objectMode } ) ;
51
62
return output ;
52
63
}
53
64
return harness . createStream ( options ) ;
@@ -66,21 +77,23 @@ module.exports = (function () {
66
77
return lazyLoad ;
67
78
} ( ) ) ;
68
79
80
+ /** @type {Tape['createHarness'] } */
69
81
function createHarness ( conf_ ) {
70
82
var results = new Results ( { todoIsOK : ! ! ( process . env . TODO_IS_OK === '1' ) } ) ;
71
83
if ( ! conf_ || conf_ . autoclose !== false ) {
72
84
results . once ( 'done' , function ( ) { results . close ( ) ; } ) ;
73
85
}
74
86
87
+ /** @type {(name: string, conf: import('.').TestOptions, cb: Test.Callback) => Test } */
75
88
function test ( name , conf , cb ) {
76
89
var t = new Test ( name , conf , cb ) ;
77
90
test . _tests . push ( t ) ;
78
91
79
92
( function inspectCode ( st ) {
80
- st . on ( 'test' , function sub ( st_ ) {
93
+ st . on ( 'test' , /** @type { (st: Test) => void } */ function sub ( st_ ) {
81
94
inspectCode ( st_ ) ;
82
95
} ) ;
83
- st . on ( 'result' , function ( r ) {
96
+ st . on ( 'result' , /** @type { (r: import('./lib/results').Result) => void } */ function ( r ) {
84
97
if ( ! r . todo && ! r . ok && typeof r !== 'string' ) { test . _exitCode = 1 ; }
85
98
} ) ;
86
99
} ( t ) ) ;
@@ -90,21 +103,25 @@ function createHarness(conf_) {
90
103
}
91
104
test . _results = results ;
92
105
93
- test . _tests = [ ] ;
106
+ /** @type { Test[] } */ test . _tests = [ ] ;
94
107
108
+ /** @type {import('.').CreateStream } */
95
109
test . createStream = function ( opts ) {
96
110
return results . createStream ( opts ) ;
97
111
} ;
98
112
113
+ /** @type {import('.').HarnessEventHandler } */
99
114
test . onFinish = function ( cb ) {
100
115
results . on ( 'done' , cb ) ;
101
116
} ;
102
117
118
+ /** @type {import('.').HarnessEventHandler } */
103
119
test . onFailure = function ( cb ) {
104
120
results . on ( 'fail' , cb ) ;
105
121
} ;
106
122
107
123
var only = false ;
124
+ /** @type {() => ReturnType<typeof test> } */
108
125
test . only = function ( ) {
109
126
if ( only ) { throw new Error ( 'there can only be one only test' ) ; }
110
127
if ( conf_ && conf_ . noOnly ) { throw new Error ( '`only` tests are prohibited' ) ; }
@@ -117,9 +134,12 @@ function createHarness(conf_) {
117
134
118
135
test . close = function ( ) { results . close ( ) ; } ;
119
136
137
+ test . run = function ( ) { } ;
138
+
120
139
return test ;
121
140
}
122
141
142
+ /** @type {(conf: Omit<HarnessConfig, 'autoclose'>, wait?: boolean) => Harness } */
123
143
function createExitHarness ( config , wait ) {
124
144
var noOnly = config . noOnly ;
125
145
var objectMode = config . objectMode ;
@@ -140,6 +160,7 @@ function createExitHarness(config, wait) {
140
160
var es = stream . pipe ( cStream || createDefaultStream ( ) ) ;
141
161
if ( canEmitExit && es ) { // in node v0.4, `es` is `undefined`
142
162
// TODO: use `err` arg?
163
+ // @ts -expect-error
143
164
// eslint-disable-next-line no-unused-vars
144
165
es . on ( 'error' , function ( err ) { harness . _exitCode = 1 ; } ) ;
145
166
}
@@ -180,6 +201,7 @@ function createExitHarness(config, wait) {
180
201
}
181
202
182
203
module . exports . createHarness = createHarness ;
183
- module . exports . Test = Test ;
184
- module . exports . test = module . exports ; // tap compat
185
- module . exports . test . skip = Test . skip ;
204
+ var moduleExports = module . exports ; // this hack is needed because TS has a bug with seemingly circular exports
205
+ moduleExports . Test = Test ;
206
+ moduleExports . test = module . exports ; // tap compat
207
+ moduleExports . skip = Test . skip ;
0 commit comments