File tree 11 files changed +109
-19
lines changed
11 files changed +109
-19
lines changed Original file line number Diff line number Diff line change @@ -14,11 +14,22 @@ npx ava --timeout=2m # 2 minutes
14
14
npx ava --timeout=100 # 100 milliseconds
15
15
```
16
16
17
- Timeouts can also be set individually for each test. These timeouts are reset each time an assertion is made.
17
+ ### ` t.timeout(ms, message?) `
18
+
19
+ Timeouts can also be set individually for each test These timeouts are reset each time an assertion is made. The test fails if it takes more than ` ms ` for an assertion to be made or the test to complete.
18
20
19
21
``` js
20
22
test (' foo' , t => {
21
23
t .timeout (100 ); // 100 milliseconds
22
24
// Write your assertions here
23
25
});
24
26
```
27
+
28
+ An optional message string can be provided. This can be useful if your test depends on some other setup that may not have been completed:
29
+
30
+ ``` js
31
+ test (' foo' , t => {
32
+ t .timeout (100 , ' make sure database has started' ); // 100 milliseconds
33
+ // Write your assertions here
34
+ });
35
+ ```
Original file line number Diff line number Diff line change @@ -353,7 +353,7 @@ export interface TimeoutFn {
353
353
* Set a timeout for the test, in milliseconds. The test will fail if the timeout is exceeded.
354
354
* The timeout is reset each time an assertion is made.
355
355
*/
356
- ( ms : number ) : void ;
356
+ ( ms : number , message ?: string ) : void ;
357
357
}
358
358
359
359
export interface TeardownFn {
Original file line number Diff line number Diff line change @@ -64,6 +64,21 @@ class AssertionError extends Error {
64
64
}
65
65
exports . AssertionError = AssertionError ;
66
66
67
+ function checkAssertionMessage ( assertion , message ) {
68
+ if ( typeof message === 'undefined' || typeof message === 'string' ) {
69
+ return true ;
70
+ }
71
+
72
+ return new AssertionError ( {
73
+ assertion,
74
+ improperUsage : true ,
75
+ message : 'The assertion message must be a string' ,
76
+ values : [ formatWithLabel ( 'Called with:' , message ) ]
77
+ } ) ;
78
+ }
79
+
80
+ exports . checkAssertionMessage = checkAssertionMessage ;
81
+
67
82
function getErrorWithLongStackTrace ( ) {
68
83
const limitBefore = Error . stackTraceLimit ;
69
84
Error . stackTraceLimit = Infinity ;
@@ -268,22 +283,16 @@ class Assertions {
268
283
} ) ;
269
284
270
285
const checkMessage = ( assertion , message , powerAssert = false ) => {
271
- if ( typeof message === 'undefined' || typeof message === 'string' ) {
272
- return true ;
286
+ const result = checkAssertionMessage ( assertion , message ) ;
287
+ if ( result === true ) {
288
+ return this . true ;
273
289
}
274
290
275
- const error = new AssertionError ( {
276
- assertion,
277
- improperUsage : true ,
278
- message : 'The assertion message must be a string' ,
279
- values : [ formatWithLabel ( 'Called with:' , message ) ]
280
- } ) ;
281
-
282
291
if ( powerAssert ) {
283
- throw error ;
292
+ throw result ;
284
293
}
285
294
286
- fail ( error ) ;
295
+ fail ( result ) ;
287
296
return false ;
288
297
} ;
289
298
Original file line number Diff line number Diff line change @@ -65,8 +65,8 @@ class ExecutionContext extends assert.Assertions {
65
65
66
66
this . plan . skip = ( ) => { } ;
67
67
68
- this . timeout = ms => {
69
- test . timeout ( ms ) ;
68
+ this . timeout = ( ms , message ) => {
69
+ test . timeout ( ms , message ) ;
70
70
} ;
71
71
72
72
this . teardown = callback => {
@@ -431,15 +431,22 @@ class Test {
431
431
this . planError = planError ;
432
432
}
433
433
434
- timeout ( ms ) {
434
+ timeout ( ms , message ) {
435
+ const result = assert . checkAssertionMessage ( 'timeout' , message ) ;
436
+ if ( result !== true ) {
437
+ this . saveFirstError ( result ) ;
438
+ // Allow the timeout to be set even when the message is invalid.
439
+ message = '' ;
440
+ }
441
+
435
442
if ( this . finishing ) {
436
443
return ;
437
444
}
438
445
439
446
this . clearTimeout ( ) ;
440
447
this . timeoutMs = ms ;
441
448
this . timeoutTimer = nowAndTimers . setTimeout ( ( ) => {
442
- this . saveFirstError ( new Error ( 'Test timeout exceeded' ) ) ;
449
+ this . saveFirstError ( new Error ( message || 'Test timeout exceeded' ) ) ;
443
450
444
451
if ( this . finishDueToTimeout ) {
445
452
this . finishDueToTimeout ( ) ;
Original file line number Diff line number Diff line change @@ -19,11 +19,15 @@ exports.fixture = async (...args) => {
19
19
serialization
20
20
} ) ;
21
21
22
+ const errors = new WeakMap ( ) ;
22
23
const stats = {
23
24
failed : [ ] ,
24
25
skipped : [ ] ,
25
26
unsavedSnapshots : [ ] ,
26
- passed : [ ]
27
+ passed : [ ] ,
28
+ getError ( statObject ) {
29
+ return errors . get ( statObject ) ;
30
+ }
27
31
} ;
28
32
29
33
running . on ( 'message' , message => {
@@ -55,7 +59,9 @@ exports.fixture = async (...args) => {
55
59
56
60
case 'test-failed' : {
57
61
const { title, testFile} = message ;
58
- stats . failed . push ( { title, file : normalizePath ( cwd , testFile ) } ) ;
62
+ const statObject = { title, file : normalizePath ( cwd , testFile ) } ;
63
+ errors . set ( statObject , message . err ) ;
64
+ stats . failed . push ( statObject ) ;
59
65
break ;
60
66
}
61
67
Original file line number Diff line number Diff line change
1
+ const test = require ( 'ava' ) ;
2
+
3
+ test ( 'timeout with custom message' , async t => {
4
+ t . timeout ( 10 , 'time budget exceeded' ) ; // eslint-disable-line ava/assertion-arguments
5
+ await new Promise ( ( ) => { } ) ;
6
+ } ) ;
Original file line number Diff line number Diff line change
1
+ const test = require ( 'ava' ) ;
2
+
3
+ test ( 'timeout with invalid message' , t => {
4
+ t . timeout ( 10 , 20 ) ; // eslint-disable-line ava/assertion-arguments
5
+ } ) ;
6
+
Original file line number Diff line number Diff line change
1
+ {
2
+ "ava" : {
3
+ "files" : [
4
+ " *.js"
5
+ ]
6
+ },
7
+ "dependencies" : {
8
+ "ava" : " file:../../.."
9
+ }
10
+ }
Original file line number Diff line number Diff line change
1
+ # Snapshot report for ` test/test-timeouts/test.js `
2
+
3
+ The actual snapshot is saved in ` test.js.snap ` .
4
+
5
+ Generated by [ AVA] ( https://avajs.dev ) .
6
+
7
+ ## timeout messages must be strings
8
+
9
+ > error message
10
+
11
+ 'The assertion message must be a string'
12
+
13
+ > formatted values
14
+
15
+ [
16
+ {
17
+ formatted: '20',
18
+ label: 'Called with:',
19
+ },
20
+ ]
Original file line number Diff line number Diff line change
1
+ const test = require ( '@ava/test' ) ;
2
+ const exec = require ( '../helpers/exec' ) ;
3
+
4
+ test ( 'timeout message can be specified' , async t => {
5
+ const result = await t . throwsAsync ( exec . fixture ( 'custom-message.js' ) ) ;
6
+ const error = result . stats . getError ( result . stats . failed [ 0 ] ) ;
7
+ t . is ( error . message , 'time budget exceeded' ) ;
8
+ } ) ;
9
+
10
+ test ( 'timeout messages must be strings' , async t => {
11
+ const result = await t . throwsAsync ( exec . fixture ( 'invalid-message.js' ) ) ;
12
+ const error = result . stats . getError ( result . stats . failed [ 0 ] ) ;
13
+ t . snapshot ( error . message , 'error message' ) ;
14
+ t . snapshot ( error . values , 'formatted values' ) ;
15
+ } ) ;
You can’t perform that action at this time.
0 commit comments