Skip to content

Commit 10e2e8a

Browse files
committed
Add t.timeout.clear() to restore default behavior
1 parent 5a9a627 commit 10e2e8a

File tree

7 files changed

+43
-18
lines changed

7 files changed

+43
-18
lines changed

docs/02-execution-context.md

+2
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,5 @@ You cannot use `t.teardown()` in hooks either.
4545
## `t.timeout(ms)`
4646

4747
Set a timeout for the test, in milliseconds. The test will fail if this timeout is exceeded. The timeout is reset each time an assertion is made.
48+
49+
Use `t.timeout.clear()` to clear the timeout and restore the default behavior.

lib/test.js

+6-12
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ class ExecutionContext extends Assertions {
7979
test.timeout(ms, message);
8080
};
8181

82+
this.timeout.clear = () => {
83+
test.clearTimeout();
84+
};
85+
8286
this.teardown = callback => {
8387
test.addTeardown(callback);
8488
};
@@ -289,7 +293,6 @@ export default class Test {
289293
this.pendingAttemptCount = 0;
290294
this.planCount = null;
291295
this.startedAt = 0;
292-
this.timeoutMs = 0;
293296
this.timeoutTimer = null;
294297
}
295298

@@ -418,7 +421,6 @@ export default class Test {
418421
}
419422

420423
this.clearTimeout();
421-
this.timeoutMs = ms;
422424
this.timeoutTimer = nowAndTimers.setCappedTimeout(() => {
423425
this.saveFirstError(new Error(message ?? 'Test timeout exceeded'));
424426

@@ -427,19 +429,11 @@ export default class Test {
427429
}
428430
}, ms);
429431

430-
this.notifyTimeoutUpdate(this.timeoutMs);
432+
this.notifyTimeoutUpdate(ms);
431433
}
432434

433435
refreshTimeout() {
434-
if (!this.timeoutTimer) {
435-
return;
436-
}
437-
438-
if (this.timeoutTimer.refresh) {
439-
this.timeoutTimer.refresh();
440-
} else {
441-
this.timeout(this.timeoutMs);
442-
}
436+
this.timeoutTimer?.refresh();
443437
}
444438

445439
clearTimeout() {

test-tap/test.js

+12
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,18 @@ test('timeout is refreshed on assert', {skip: ciInfo.isCI}, t => ava(async a =>
589589
t.equal(result.passed, true);
590590
}));
591591

592+
test('timeout can be cleared', {skip: ciInfo.isCI}, t => ava(async a => {
593+
a.timeout(100);
594+
a.plan(2);
595+
await Promise.all([
596+
delay(50).then(() => a.pass()),
597+
delay(100).then(() => a.timeout.clear()),
598+
delay(350).then(() => a.pass()),
599+
]);
600+
}).run().then(result => {
601+
t.equal(result.passed, true);
602+
}));
603+
592604
test('teardown passing test', t => {
593605
const teardown = sinon.spy();
594606
return ava(a => {

test-types/import-in-cts/timeout.cts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import test from '../../entrypoints/main.cjs';
2+
3+
test('test', t => {
4+
t.timeout(100);
5+
t.timeout.clear();
6+
});

test-types/module/timeout.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import test from '../../entrypoints/main.mjs';
2+
3+
test('test', t => {
4+
t.timeout(100);
5+
t.timeout.clear();
6+
});

test/watch-mode/helpers/watch.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export const withFixture = fixture => async (t, task) => {
8888
idlePromise = new Promise(() => {});
8989
assertingIdle = false;
9090
// TODO: When testing using AVA 6, enable for better managed timeouts.
91-
// t.timeout(0);
91+
// t.timeout.clear();
9292
if (failedIdleAssertion) {
9393
failedIdleAssertion = false;
9494
t.fail('Watcher performed a test run while it should have been idle');

types/test-fn.d.cts

+10-5
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,16 @@ export type PlanFn = {
3939
skip(count: number): void;
4040
};
4141

42-
/**
43-
* Set a timeout for the test, in milliseconds. The test will fail if the timeout is exceeded.
44-
* The timeout is reset each time an assertion is made.
45-
*/
46-
export type TimeoutFn = (ms: number, message?: string) => void;
42+
export type TimeoutFn = {
43+
/**
44+
* Set a timeout for the test, in milliseconds. The test will fail if the timeout is exceeded.
45+
* The timeout is reset each time an assertion is made.
46+
*/
47+
(ms: number, message?: string): void;
48+
49+
/** Clear the timeout and restore the default behavior. */
50+
clear(): void;
51+
}
4752

4853
/** Declare a function to be run after the test has ended. */
4954
export type TeardownFn = (fn: (() => Promise<void>) | (() => void)) => void;

0 commit comments

Comments
 (0)