Skip to content

[vitest-pool-workers] onAfterTryTask calls ensurePoppedActiveTryStorage before afterEach hooks are run #11690

@KianNH

Description

@KianNH

What versions & operating system are you using?

@cloudflare/vitest-pool-workers: 0.11.0
miniflare: 4.20250917.0
workerd: 1.20250917.0
vitest: 3.2.4

Please provide a link to a minimal reproduction

tbd

Describe the Bug

vitest-pool-workers uses the onAfterTryTask hook when asserting ensurePoppedActiveTryStorage:

async onAfterTryTask(test: Test, options: TryOptions) {
if (DEBUG) {
__console.log(`${_(6)}onAfterTryTask: ${test.name}`, options);
await scheduler.wait(100);
}
// Pop storage for this try, asserting that we haven't done so already.
// `onAfterTryTask()` is never called multiple times for the same try,
// `onBeforeTryTask()` will only be called with a new try after this,
// and `onAfterRunTask()` will only be called after all tries.
assert(await this.ensurePoppedActiveTryStorage(test));
return super.onAfterTryTask(test);
}

onAfterTryTask hook is run before afterEach or beforeEach clean-up hooks are run:

https://github.com/vitest-dev/vitest/blob/c666d149a4516761bae92ca56ce1336d2fd352c3/packages/runner/src/run.ts#L334-L365

This means that a successful run of a test which uses afterEach to clean-up state, i.e running Durable Object alarms and then calling deleteAlarm / deleteAll, will fail the ensurePoppedActiveTryStorage assertion.

As a workaround, we have to call what we'd put in afterEach within the test callback itself for each test.

Patching VPW to call onAfterRunTask instead of onAfterTryTask makes these tests run successfully but the existing VPW code calls out why that won't work:

// and `onAfterRunTask()` will only be called after all tries.

onAfterRetryTask was introduced in Vitest 4 & may be an option once VPW supports Vitest 4?

  /**
   * Called after the retry resolution happend. Unlike `onAfterTryTask`, the test now has a new state.
   * All `after` hooks were also called by this point.
   */
  onAfterRetryTask?: (test: Test, options: { retry: number; repeats: number }) => unknown

Please provide any relevant error logs

onAfterTryTask: test name { retry: 0, repeats: 0 }

================================================================================
Failed to pop isolated storage stack frame in test/v1.file.spec.ts's test "test name".
In particular, we were unable to pop Durable Objects storage.
This usually means your Worker tried to access storage outside of a test, or some resources have not been disposed of properly.
Ensure you "await" all Promises that read or write to these services, and make sure you use the "using" keyword when passing data across JSRPC.
See https://developers.cloudflare.com/workers/testing/vitest-integration/known-issues/#isolated-storage for more details.

- AssertionError [ERR_ASSERTION]: Expected .sqlite, got /tmp/miniflare-520ee179eadf78ab64a6a45bc3ef5135/do/vitest-pool-workers-runner--CLASS/13117469cae122afdae3c5920695f1325f0ba2d4461e49389d6255f9985e1aba.sqlite-shm
================================================================================

stdout | test/v1.file.spec.ts > test name
afterEach called

// ... Durable Object alarm uses storage ...

onAfterRunTask: test name

 FAIL  test/v1.file.spec.ts > test name
AssertionError: Isolated storage failed. There should be additional logs above.

- Expected
+ Received

- 204
+ 500

Metadata

Metadata

Labels

vitestRelating to the Workers Vitest integration

Type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions