-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Long task with synchronous pieces does not time out #2920
Comments
If it helps, here's a repo that I just threw together. Vite/vitest versions are absolute latest https://github.com/bensaufley/vitest-timeout-example |
Found that putting the synchronous task inside the promise, with setImmediate, produces a timeout: it("times out", async () => {
const start = Date.now();
await new Promise<void>((resolve) => {
while (Date.now() - start < 5_000) {}
setImmediate(resolve);
});
}, 150); I'm 100% certain there are system-level reasons for why there are distinctions here, but from a usage standpoint, the distinction between |
Hi there! Is there any update on this issue? I came across a similar issue: an infinite loop inside a synchronous function, and the |
This is just how JavaScript engine works, I don't what else to update here. |
The originally reported issue is not that the task isn't canceled (which I agree is just the nature of JS); it's that when the task completes, it isn't marked as timed out, despite taking much longer than the specified time. |
As I said, setTimeout cannot be called while the test is running synchronously, it „messes“ up the timers. To JS it did not took 5 seconds. This is the nature of js. Test time is not checked after the test has finished, the fail is scheduled before test starts with setTimeout to timeout early. |
And this is the core of the issue. I explained all of this above:
If we're considering timeout to only mean "canceled before execution could finish" then timeout may not be the best way to frame that.
This is just incorrect. JS knows what time it took. Vite even reports that time in the output. That's where the heart of this issue is. |
Then what about the assertion itself? I used The code was some thing like this: // This is just a sample script. Paste your real code (javascript or HTML) here.
it('title', () => {
const obj = someFunction();
except(object).toEqual(
except.objectContaining({
sample1: [],
sample2: [],
sample3: {
foo: undefined,
bar: undefined,
title: undefined,
},
})
});
});
|
That doesn't seem related to this discussion. |
Describe the bug
Basically I've got a synchronous function that I want to test is reasonably fast with a large amount of data. I understand the issue of not being able to interrupt the process for the synchronous task, and saw for example another issue mentioning the problem with an infinite loop—but this function does eventually resolve. If I've set a timeout for 150ms and it takes 5 seconds to resolve, I would expect the test to fail as timed out, but it doesn't, no matter how long it takes.
I suppose this may be "behaving as expected" as timeout is only intended to interrupt asynchronous tasks if it can and does nothing if it can't interrupt, but it's definitely unintuitive to me that a test with a 150ms timeout that resolves in 5 seconds is considered to have passed.
For my particular use case I did a
const start = Date.now(); … expect(Date.now() - start).toBeLessThan(150);
and I'm sure that will probably be the response here, but I think it's unintuitive that the distinction between synchronous and asynchronous code (even synchronous code within a promise) will change whether the same behavior is considered to have timed out.Reproduction
Times out as I'd expect, because (as far as I understand, and I admit I'm a little fuzzy)
setTimeout
does not monopolize ticks and allows the test process to intervene:Does not time out, and does not fail once completed:
Even this, which occurred to me while writing this issue, does not result in a timeout:
System Info
Used Package Manager
npm
Validations
The text was updated successfully, but these errors were encountered: