diff --git a/mod.test.ts b/mod.test.ts index d142617..3d2610d 100644 --- a/mod.test.ts +++ b/mod.test.ts @@ -250,3 +250,27 @@ test("Three-Level Nesting with Mixed Operations", async (context) => { assertEquals(executionCount, 4); }); + +// Test timeout with zero value (should be treated as no timeout) +test("Test with timeout = 0 (no timeout)", async () => { + await delay(50); + assertEquals(1, 1); +}, { timeout: 0 }); + +// Test timeout with negative value (should be treated as no timeout) +test("Test with negative timeout (no timeout)", async () => { + await delay(50); + assertEquals(1, 1); +}, { timeout: -100 }); + +// Test that completes well within timeout +test("Test completes within generous timeout", async () => { + await delay(50); + assertEquals(1, 1); +}, { timeout: 5000 }); + +// Test longer execution with sufficient timeout (7 seconds with 10 second timeout) +test("7 second execution with 10 second timeout", async () => { + await delay(7000); + assertEquals(1, 1); +}, { timeout: 10000 }); diff --git a/shims/bun.ts b/shims/bun.ts index 581d87a..7191cdf 100644 --- a/shims/bun.ts +++ b/shims/bun.ts @@ -6,6 +6,19 @@ export async function wrappedTest( testFn: TestSubject, options: WrappedTestOptions, ): Promise { + // Build Bun test options - pass timeout to Bun's native test runner + // If timeout is 0 or not specified, Bun will use its default 5s timeout + // If we're using custom timeout logic, we need to set a high value for Bun + const bunOptions: { timeout?: number; skip?: boolean } = { + skip: options?.skip || false, + }; + + // If a timeout is specified and > 0, pass it to Bun's native timeout + // This makes Bun's timeout and our custom timeout work together + if (options.timeout && options.timeout > 0) { + bunOptions.timeout = options.timeout; + } + return await test(name, async () => { // Create wrapped context with step method const wrappedContext: TestContext = { @@ -87,7 +100,7 @@ export async function wrappedTest( }); let timeoutId: number = -1; // Store the timeout ID try { - if (options.timeout) { + if (options.timeout && options.timeout > 0) { const timeoutPromise = new Promise((_, reject) => { timeoutId = setTimeout(() => { reject(new Error("Test timed out")); @@ -98,13 +111,11 @@ export async function wrappedTest( // No timeout, just await testFn await options.waitForCallback ? callbackPromise : testFnPromise; } - } catch (error) { - throw error; } finally { - if (timeoutId) clearTimeout(timeoutId); + if (timeoutId !== -1) clearTimeout(timeoutId); // Make sure testFnPromise has completed await testFnPromise; if (options.waitForCallback) await callbackPromise; } - }); + }, bunOptions); } diff --git a/shims/deno.ts b/shims/deno.ts index 8828f4e..d988aae 100644 --- a/shims/deno.ts +++ b/shims/deno.ts @@ -114,7 +114,7 @@ export function wrappedTest(name: string, testFn: TestSubject, options: WrappedT }); let timeoutId: number = -1; // Store the timeout ID try { - if (options.timeout) { + if (options.timeout && options.timeout > 0) { const timeoutPromise = new Promise((_, reject) => { timeoutId = setTimeout(() => { reject(new Error("Test timed out")); @@ -124,10 +124,8 @@ export function wrappedTest(name: string, testFn: TestSubject, options: WrappedT } else { await options.waitForCallback ? callbackPromise : testFnPromise; } - } catch (error) { - throw error; } finally { - if (timeoutId) clearTimeout(timeoutId); + if (timeoutId !== -1) clearTimeout(timeoutId); await testFnPromise; if (options.waitForCallback) await callbackPromise; } diff --git a/shims/node.ts b/shims/node.ts index 92fab76..651f622 100644 --- a/shims/node.ts +++ b/shims/node.ts @@ -3,10 +3,17 @@ import type { ContextStepFunction, SimpleStepFunction, StepOptions, StepSubject, import type { TestSubject } from "../mod.ts"; function transformOptions(options?: WrappedTestOptions) { - return { + const nodeOptions: { skip: boolean; timeout?: number } = { skip: options?.skip || false, - timeout: options?.timeout, }; + + // Node.js requires timeout to be > 0 and <= 2147483647 + // Only set timeout if it's a valid positive number + if (options?.timeout && options.timeout > 0) { + nodeOptions.timeout = options.timeout; + } + + return nodeOptions; } // Helper function to create a fallback context for older Node versions