Skip to content

Commit

Permalink
Add t.timeout.clear() to restore default behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
novemberborn committed Jul 2, 2023
1 parent 018d64f commit 2895dcf
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 18 deletions.
2 changes: 2 additions & 0 deletions docs/02-execution-context.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,5 @@ You cannot use `t.teardown()` in hooks either.
## `t.timeout(ms)`

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.

Use `t.timeout.clear()` to clear the timeout and restore the default behavior.
18 changes: 6 additions & 12 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ class ExecutionContext extends Assertions {
test.timeout(ms, message);
};

this.timeout.clear = () => {
test.clearTimeout();
};

this.teardown = callback => {
test.addTeardown(callback);
};
Expand Down Expand Up @@ -289,7 +293,6 @@ export default class Test {
this.pendingAttemptCount = 0;
this.planCount = null;
this.startedAt = 0;
this.timeoutMs = 0;
this.timeoutTimer = null;
}

Expand Down Expand Up @@ -418,7 +421,6 @@ export default class Test {
}

this.clearTimeout();
this.timeoutMs = ms;
this.timeoutTimer = nowAndTimers.setCappedTimeout(() => {
this.saveFirstError(new Error(message ?? 'Test timeout exceeded'));

Expand All @@ -427,19 +429,11 @@ export default class Test {
}
}, ms);

this.notifyTimeoutUpdate(this.timeoutMs);
this.notifyTimeoutUpdate(ms);
}

refreshTimeout() {
if (!this.timeoutTimer) {
return;
}

if (this.timeoutTimer.refresh) {
this.timeoutTimer.refresh();
} else {
this.timeout(this.timeoutMs);
}
this.timeoutTimer?.refresh();
}

clearTimeout() {
Expand Down
12 changes: 12 additions & 0 deletions test-tap/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,18 @@ test('timeout is refreshed on assert', {skip: ciInfo.isCI}, t => ava(async a =>
t.equal(result.passed, true);
}));

test('timeout can be cleared', {skip: ciInfo.isCI}, t => ava(async a => {
a.timeout(100);
a.plan(2);
await Promise.all([
delay(50).then(() => a.pass()),
delay(100).then(() => a.timeout.clear()),
delay(350).then(() => a.pass()),
]);
}).run().then(result => {
t.equal(result.passed, true);
}));

test('teardown passing test', t => {
const teardown = sinon.spy();
return ava(a => {
Expand Down
6 changes: 6 additions & 0 deletions test-types/import-in-cts/timeout.cts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import test from 'ava';

test('test', t => {
t.timeout(100);
t.timeout.clear();
});
6 changes: 6 additions & 0 deletions test-types/module/timeout.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import test from 'ava';

test('test', t => {
t.timeout(100);
t.timeout.clear();
});
2 changes: 1 addition & 1 deletion test/watch-mode/helpers/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export const withFixture = fixture => async (t, task) => {
idlePromise = new Promise(() => {});
assertingIdle = false;
// TODO: When testing using AVA 6, enable for better managed timeouts.
// t.timeout(0);
// t.timeout.clear();
if (failedIdleAssertion) {
failedIdleAssertion = false;
t.fail('Watcher performed a test run while it should have been idle');
Expand Down
15 changes: 10 additions & 5 deletions types/test-fn.d.cts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,16 @@ export type PlanFn = {
skip(count: number): void;
};

/**
* Set a timeout for the test, in milliseconds. The test will fail if the timeout is exceeded.
* The timeout is reset each time an assertion is made.
*/
export type TimeoutFn = (ms: number, message?: string) => void;
export type TimeoutFn = {
/**
* Set a timeout for the test, in milliseconds. The test will fail if the timeout is exceeded.
* The timeout is reset each time an assertion is made.
*/
(ms: number, message?: string): void;

/** Clear the timeout and restore the default behavior. */
clear(): void;
}

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

0 comments on commit 2895dcf

Please sign in to comment.