AVA中--timeout与t.timeout的区别及使用方式咨询
Let’s break down your two questions about AVA’s timeout behavior clearly:
1. Do you need to use both command-line --timeout and t.timeout()?
No, you don’t have to use both—but they serve distinct purposes, so combining them can be practical in some scenarios:
- The command-line
--timeoutsets a global default timeout for all your tests. Think of this as a safety net: it prevents hanging tests from running indefinitely if you forget to configure timeouts for individual cases. t.timeout(ms)is a per-test override that takes priority over the global setting. Use this when a specific test needs more (or less) time than the default—like a test that interacts with a slow external API, or a fast unit test that doesn’t need the full global timeout window.
So, if all your tests have similar timing needs, stick to either the global command-line setting or t.timeout() in each test. If you want a universal fallback but need to adjust for edge-case tests, using both makes sense.
2. If t.timeout(1000) is set, do two consecutive t.is() assertions need to complete within 1 second?
Not quite—let’s clarify with AVA’s documented behavior:
The timer for
t.timeout()resets after each assertion.
Here’s what that actually means:
- Every time you run an assertion (like
t.is()), the timeout timer resets to the full 1000ms. - The test will only fail if 1 full second passes without any new assertions or the test finishing.
For example:
test('timeout behavior demo', t => { t.timeout(1000); t.is(1, 1); // Timer resets to 1000ms here // Waiting 800ms before the next assertion is totally fine setTimeout(() => { t.is(2, 2); // Timer resets again to 1000ms // As long as we finish the test or run another assertion within 1 second of this, no timeout occurs }, 800); });
The only way the test times out is if, after an assertion, you wait more than 1000ms without running another assertion or ending the test. It’s not about the total time between two assertions—it’s about idle time with no test activity after the last assertion.
内容的提问来源于stack exchange,提问作者Daniel Stephens

