You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When running a test using vitest and setting the "isolatedStorage" option to false, the test does not wait for background tasks queued using ctx.waitUntil(). This only seems to happen when using SELF (i.e. "await SELF.fetch(...)"), not when importing your worker module manually and calling fetch() from there. Awaiting the context using "await waitOnExecutionContext(ctx)" does not make a difference, the await completes immediately without waiting for the background task. Output generated by these background tasks will be associated with the wrong test, and I have also seen some crazy-looking worker errors pop up when I try to keep calling the worker with new requests (not in the minimal reproduction, but in our actual code I tried to test).
Expected behavior
Ideally, "await SELF.fetch(...)" should not return until the request is completely processed, including background work. Barring that, there should still be a way to await completion of background work.
Steps to reproduce
Clone the minimal reproduction repo linked below
Run "npm ci"
Run "npm run test"
Note that the "responds with Hello World! (unit style)" test completes in more than a second, while the five "integration style" tests complete almost immediately. The worker is queueing some background work that sleeps for a second, so any test that completes more quickly is wrong.
Also note that the "done long-running code" console outputs are not reported for the test that triggered them. (This depends on the exact timing.)
@csaboka thank you so much for reporting. I don't think this is a bug, but is instead a slight misunderstanding about the SELF.fetch call. This is actually making a fetch request to your worker, rather than calling the method directly (as it is in the unit test). Therefore both the env and the execution context that you have created is ignored.
@andyjessop : If this is the case, shouldn't the default example tests be updated to reflect this? The original project created by create-cloudflare passes an environment and context to fetch, but it shouldn't if they are ignored anyway. It should demonstrate the correct way of invoking the worker.
Which Cloudflare product(s) does this pertain to?
Workers Vitest Integration
What version(s) of the tool(s) are you using?
3.79.0 [Wrangler], 0.5.13 [vitest-pool-workers]
What version of Node are you using?
22.9.0
What operating system and version are you using?
Windows 11
Describe the Bug
Observed behavior
When running a test using vitest and setting the "isolatedStorage" option to false, the test does not wait for background tasks queued using ctx.waitUntil(). This only seems to happen when using SELF (i.e. "await SELF.fetch(...)"), not when importing your worker module manually and calling fetch() from there. Awaiting the context using "await waitOnExecutionContext(ctx)" does not make a difference, the await completes immediately without waiting for the background task. Output generated by these background tasks will be associated with the wrong test, and I have also seen some crazy-looking worker errors pop up when I try to keep calling the worker with new requests (not in the minimal reproduction, but in our actual code I tried to test).
Expected behavior
Ideally, "await SELF.fetch(...)" should not return until the request is completely processed, including background work. Barring that, there should still be a way to await completion of background work.
Steps to reproduce
Please provide a link to a minimal reproduction
https://github.com/csaboka/vitest-self-issue
Please provide any relevant error logs
No response
The text was updated successfully, but these errors were encountered: