Skip to content

Cloudflare Vitest Pool Workers fails to handle 302 redirect responses from Durable Objects with "Expected callback for X" error #9907

@shikharbsar

Description

@shikharbsar

What versions & operating system are you using?

Node.js: v22.13.1, Wrangler: 4.20.0, @cloudflare/vitest-pool-workers: 0.8.38

Please provide a link to a minimal reproduction

No response

Describe the Bug

Environment:

  • @cloudflare/vitest-pool-workers version: (check your package.json)
  • vitest version: 3.1.2
  • Node.js version: (your version)

Issue Summary:
The Cloudflare Vitest test framework fails to properly handle 302 redirect responses returned by Durable Objects, causing tests to fail with cryptic "Expected callback for X" errors (where X is a number like 30, 34, 38, 42).

Steps to Reproduce:

  1. Create a Durable Object handler that returns a 302 redirect response
  2. Write a test that calls the handler through runInDurableObject()
  3. The test fails with "AssertionError: Expected callback for X" even though the handler works correctly

Expected Behavior:
Tests should be able to handle 302 redirect responses from Durable Objects without throwing framework-level errors.

Actual Behavior:
Tests fail with "AssertionError: Expected callback for X" errors that appear to be internal framework issues, not user code issues.

Code Example:

// This handler works in production but fails in tests
const result = await runInDurableObject(object, async (instance) => {
    const formData = new FormData();
    formData.append('abc', btoa(JSON.stringify({ abc })));
    formData.append('url', 'https://example.com');
    
    const request = new Request("https://example.com/auth", {
        method: 'POST',
        body: formData
    });
    
    // This returns a 302 redirect, which causes the test framework to fail
    return worker.fetch(request, env, ctx);
});

Evidence the Handler Works:
Console output shows the handler correctly generates redirect URLs:

redirectUrl https://test.thoughtspot.cloud/callosum/v1/saml/login?targetURLPath=https%3A%2F%2Fexample.com%2Fcallback%3FinstanceUrl%3Dhttps%253A%252F%252Ftest.thoughtspot.cloud%26oauthReqInfo%3DeyJjbGllbnRJZCI6InRlc3QtY2xpZW50Iiwic2NvcGUiOiJyZWFkIiwicmVkaXJlY3RVcmkiOiJodHRwczovL2V4YW1wbGUuY29tL2NhbGxiYWNrIn0%253D

AssertionError: Expected callback for 42
Expected: true
Received: false

❯ new AssertionError node-internal:internal_assertionerror:427:15
❯ assert node-internal:internal_assert:49:15
❯ maybeHandleRunRequest Users/path/node_modules/@cloudflare/vitest-pool-workers/dist/worker/lib/cloudflare/test-internal.mjs:196:5
❯ DurableObject.Wrapper.fetch Users/path/node_modules/@cloudflare/vitest-pool-workers/dist/worker/lib/cloudflare/test-internal.mjs:530:28

Workaround:
Currently skipping tests that involve 302 redirects from Durable Objects and testing the underlying logic separately:

it.skip("should handle redirect responses", async () => {
    // Skipped due to framework bug with 302 responses
});

it("should construct redirect URLs correctly", async () => {
    // Test the URL construction logic directly
    const redirectUrl = new URL('path', instanceUrl);
    // ... verify URL construction without involving Durable Object
});

Impact:
This prevents comprehensive testing of OAuth flows and other redirect-based functionality in Durable Objects, requiring developers to skip important integration tests.

Additional Context:
The issue appears to be in the framework's internal callback handling mechanism when processing 302 responses from Durable Objects. The error occurs at the framework level, not in user code, suggesting an issue with how the test framework wraps or processes Durable Object responses.

Please provide any relevant error logs

No response

Metadata

Metadata

Assignees

No one assigned

    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