@@ -32,12 +32,68 @@ import {
32
32
throws ,
33
33
} from 'node:assert' ;
34
34
35
- function OptionalFeatureUnsupportedError ( message ) {
36
- AssertionError . call ( this , message ) ;
35
+ type Options = {
36
+ expectedFailures ?: string [ ] ;
37
+ verbose ?: boolean ;
38
+ skippedTests ?: string [ ] ;
39
+ } ;
40
+
41
+ type TestRunnerFn = ( callback : TestFn | PromiseTestFn , message : string ) => void ;
42
+ type TestFn = ( ) => void ;
43
+ type PromiseTestFn = ( ) => Promise < void > ;
44
+ type ThrowingFn = ( ) => any ;
45
+
46
+ declare namespace globalThis {
47
+ var errors : Error [ ] ;
48
+ var testOptions : Options ;
49
+ var Window : typeof globalThis ;
50
+ var self : typeof globalThis ;
51
+ var GLOBAL : { isWindow ( ) : boolean } ;
52
+ function fetch ( url : string ) : Promise < { json ( ) : Promise < any > } > ;
53
+ function test ( func : TestFn , name : string ) : void ;
54
+ function done ( ) : undefined ;
55
+ function subsetTestByKey (
56
+ _key : any ,
57
+ testType : TestRunnerFn ,
58
+ testCallback : TestFn | PromiseTestFn ,
59
+ testMessage : string
60
+ ) : void ;
61
+ function promise_test (
62
+ func : PromiseTestFn ,
63
+ name : string ,
64
+ properties ?: any
65
+ ) : void ;
66
+ function assert_equals ( a : any , b : any , message ?: string ) : void ;
67
+ function assert_not_equals ( a : any , b : any , message ?: string ) : void ;
68
+ function assert_true ( val : any , message ?: string ) : void ;
69
+ function assert_false ( val : any , message ?: string ) : void ;
70
+ function assert_array_equals ( a : any , b : any , message ?: string ) : void ;
71
+ function assert_object_equals ( a : any , b : any , message ?: string ) : void ;
72
+ function assert_implements ( condition : any , description ?: string ) : void ;
73
+ function assert_implements_optional (
74
+ condition : any ,
75
+ description ?: string
76
+ ) : void ;
77
+ function assert_unreached ( description ?: string ) : void ;
78
+ function assert_throws_js (
79
+ constructor : any ,
80
+ func : ThrowingFn ,
81
+ description ?: string
82
+ ) : void ;
83
+ function assert_throws_exactly (
84
+ exception : any ,
85
+ fn : ThrowingFn ,
86
+ description ?: string
87
+ ) : void ;
88
+ function assert_throws_dom (
89
+ type : any ,
90
+ funcOrConstructor : any ,
91
+ descriptionOrFunc : any ,
92
+ maybeDescription : any
93
+ ) : void ;
37
94
}
38
- OptionalFeatureUnsupportedError . prototype = Object . create (
39
- AssertionError . prototype
40
- ) ;
95
+
96
+ class OptionalFeatureUnsupportedError extends AssertionError { }
41
97
42
98
globalThis . Window = Object . getPrototypeOf ( globalThis ) . constructor ;
43
99
@@ -67,13 +123,7 @@ globalThis.subsetTestByKey = (_key, testType, testCallback, testMessage) => {
67
123
return testType ( testCallback , testMessage ) ;
68
124
} ;
69
125
70
- globalThis . promise_test = async ( func , name , properties ) => {
71
- if ( typeof func !== 'function' ) {
72
- properties = name ;
73
- name = func ;
74
- func = null ;
75
- }
76
-
126
+ globalThis . promise_test = async ( func , name , _properties ) => {
77
127
if ( ! shouldRunTest ( name ) ) {
78
128
return ;
79
129
}
@@ -140,7 +190,7 @@ globalThis.assert_implements = (condition, description) => {
140
190
*/
141
191
globalThis . assert_implements_optional = ( condition , description ) => {
142
192
if ( ! condition ) {
143
- throw new OptionalFeatureUnsupportedError ( description ) ;
193
+ throw new OptionalFeatureUnsupportedError ( { message : description } ) ;
144
194
}
145
195
} ;
146
196
@@ -219,7 +269,7 @@ globalThis.assert_throws_exactly = (exception, fn, description) => {
219
269
*
220
270
*/
221
271
globalThis . assert_throws_dom = (
222
- type ,
272
+ _type ,
223
273
funcOrConstructor ,
224
274
descriptionOrFunc ,
225
275
maybeDescription
@@ -230,6 +280,7 @@ globalThis.assert_throws_dom = (
230
280
func = descriptionOrFunc ;
231
281
description = maybeDescription ;
232
282
} else {
283
+ // @ts -ignore
233
284
constructor = this . DOMException ;
234
285
func = funcOrConstructor ;
235
286
description = descriptionOrFunc ;
@@ -251,7 +302,7 @@ globalThis.assert_throws_dom = (
251
302
/**
252
303
* Create a synchronous test
253
304
*
254
- * @param {TestFunction } func - Test function. This is executed
305
+ * @param {TestFn } func - Test function. This is executed
255
306
* immediately. If it returns without error, the test status is
256
307
* set to ``PASS``. If it throws an :js:class:`AssertionError`, or
257
308
* any other exception, the test status is set to ``FAIL``
@@ -273,7 +324,7 @@ globalThis.test = (func, name) => {
273
324
274
325
globalThis . errors = [ ] ;
275
326
276
- function shouldRunTest ( message ) {
327
+ function shouldRunTest ( message : string ) {
277
328
if ( ( globalThis . testOptions . skippedTests ?? [ ] ) . includes ( message ) ) {
278
329
return false ;
279
330
}
@@ -285,12 +336,12 @@ function shouldRunTest(message) {
285
336
return true ;
286
337
}
287
338
288
- function prepare ( options ) {
339
+ function prepare ( options : Options ) {
289
340
globalThis . errors = [ ] ;
290
341
globalThis . testOptions = options ;
291
342
}
292
343
293
- function sanitizeMessage ( message ) {
344
+ function sanitizeMessage ( message : string ) {
294
345
// Test logs will be exported to XML, so we must escape any characters that
295
346
// are forbidden in an XML CDATA section, namely "[...] the surrogate blocks,
296
347
// FFFE, and FFFF".
@@ -308,7 +359,7 @@ function sanitizeMessage(message) {
308
359
) ;
309
360
}
310
361
311
- function validate ( testFileName , options ) {
362
+ function validate ( testFileName : string , options : Options ) {
312
363
const expectedFailures = new Set ( options . expectedFailures ?? [ ] ) ;
313
364
314
365
let failing = false ;
@@ -332,7 +383,7 @@ function validate(testFileName, options) {
332
383
}
333
384
}
334
385
335
- export function run ( file , options = { } ) {
386
+ export function run ( file : string , options : Options = { } ) {
336
387
return {
337
388
async test ( ) {
338
389
prepare ( options ) ;
0 commit comments