Skip to content

Commit 5bea87b

Browse files
step2yeungtrentmwillis
authored andcommitted
Core: reset testTimeout on assert.timeout if config.timeout has already been set
1 parent d7f1e27 commit 5bea87b

File tree

4 files changed

+50
-10
lines changed

4 files changed

+50
-10
lines changed

docs/assert/timeout.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ Sets the length of time to wait for async operations before failing the test.
1919

2020
`assert.timeout()` sets the length of time, in milliseconds, to wait for async operations in the current test. This is equivalent to setting `config.testTimeout` on a per-test basis. The timeout length only applies when performing async operations.
2121

22+
If `assert.timeout()` is called after a timeout has already been set, the old timeout will be cleared and the new duration will be used for the new timer.
23+
2224
If `0` is passed, then the test will be assumed to be completely synchronous. If a non-numeric value is passed as an argument, the function will throw an error.
2325

2426
### Examples

src/assert.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import dump from "./dump";
22
import equiv from "./equiv";
3-
import { internalStop } from "./test";
3+
import { internalStop, resetTestTimeout } from "./test";
44
import Logger from "./logger";
55

66
import config from "./core/config";
77
import { objectType, objectValues } from "./core/utilities";
88
import { sourceFromStacktrace } from "./core/stacktrace";
9+
import { clearTimeout } from "./globals";
910

1011
class Assert {
1112
constructor( testContext ) {
@@ -20,6 +21,15 @@ class Assert {
2021
}
2122

2223
this.test.timeout = duration;
24+
25+
// If a timeout has been set, clear it and reset with the new duration
26+
if ( config.timeout ) {
27+
clearTimeout( config.timeout );
28+
29+
if ( config.timeoutHandler && this.test.timeout > 0 ) {
30+
resetTestTimeout( this.test.timeout );
31+
}
32+
}
2333
}
2434

2535
// Documents a "step", which is a string value, in a test as a passing assertion

src/test.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,12 @@ export function only( testName, callback ) {
734734
newTest.queue();
735735
}
736736

737+
// Resets config.timeout with a new timeout duration.
738+
export function resetTestTimeout( timeoutDuration ) {
739+
clearTimeout( config.timeout );
740+
config.timeout = setTimeout( config.timeoutHandler( timeoutDuration ), timeoutDuration );
741+
}
742+
737743
// Put a hold on processing and return a function that will release it.
738744
export function internalStop( test ) {
739745
let released = false;
@@ -752,16 +758,21 @@ export function internalStop( test ) {
752758

753759
if ( typeof timeoutDuration === "number" && timeoutDuration > 0 ) {
754760
clearTimeout( config.timeout );
755-
config.timeout = setTimeout( function() {
756-
pushFailure(
757-
`Test took longer than ${timeoutDuration}ms; test timed out.`,
758-
sourceFromStacktrace( 2 )
759-
);
760-
released = true;
761-
internalRecover( test );
762-
}, timeoutDuration );
761+
config.timeoutHandler = function( timeout ) {
762+
return function() {
763+
pushFailure(
764+
`Test took longer than ${timeout}ms; test timed out.`,
765+
sourceFromStacktrace( 2 )
766+
);
767+
released = true;
768+
internalRecover( test );
769+
};
770+
};
771+
config.timeout = setTimeout(
772+
config.timeoutHandler( timeoutDuration ),
773+
timeoutDuration
774+
);
763775
}
764-
765776
}
766777

767778
return function resume() {

test/main/assert/timeout.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,23 @@ QUnit.module( "assert.timeout", function() {
4545
}, /You must pass a number as the duration to assert.timeout/ );
4646
} );
4747

48+
QUnit.test( "reset a timeout if an existing timeout has been set", function( assert ) {
49+
assert.timeout( 50 );
50+
assert.expect( 1 );
51+
52+
setTimeout( function() {
53+
assert.timeout( 10 );
54+
var originalPushFailure = QUnit.config.current.pushFailure;
55+
QUnit.config.current.pushFailure = function pushFailureStub( message ) {
56+
QUnit.config.current.pushFailure = originalPushFailure;
57+
58+
assert.equal( message, "Test took longer than 10ms; test timed out." );
59+
done();
60+
};
61+
} );
62+
var done = assert.async();
63+
} );
64+
4865
QUnit.module( "a value of zero", function() {
4966
function stubPushFailure( assert ) {
5067
var originalPushFailure = QUnit.config.current.pushFailure;

0 commit comments

Comments
 (0)