diff --git a/test/js/node/watch/fs.watch-async-context.test.ts b/test/js/node/watch/fs.watch-async-context.test.ts index 6fca1e2406..99f03989d0 100644 --- a/test/js/node/watch/fs.watch-async-context.test.ts +++ b/test/js/node/watch/fs.watch-async-context.test.ts @@ -1,19 +1,19 @@ -import { test, expect } from "bun:test"; import { AsyncLocalStorage } from "async_hooks"; +import { expect, test } from "bun:test"; import fs from "fs"; -import path from "path"; import { tmpdir } from "os"; +import path from "path"; test("fs.watch preserves AsyncLocalStorage context", async () => { const asyncLocalStorage = new AsyncLocalStorage(); const testFile = path.join(tmpdir(), "test-watch-async-context.txt"); - + // Create test file fs.writeFileSync(testFile, "initial content"); - + try { const contextValue = { userId: "user123", requestId: "req456" }; - + const promise = new Promise((resolve, reject) => { asyncLocalStorage.run(contextValue, () => { const watcher = fs.watch(testFile, (eventType, filename) => { @@ -23,7 +23,7 @@ test("fs.watch preserves AsyncLocalStorage context", async () => { expect(context).toEqual(contextValue); expect(context.userId).toBe("user123"); expect(context.requestId).toBe("req456"); - + watcher.close(); resolve(); } catch (error) { @@ -31,14 +31,14 @@ test("fs.watch preserves AsyncLocalStorage context", async () => { reject(error); } }); - + // Trigger the watcher by modifying the file setTimeout(() => { fs.writeFileSync(testFile, "modified content"); }, 10); }); }); - + await promise; } finally { // Clean up @@ -51,10 +51,10 @@ test("fs.watch preserves AsyncLocalStorage context", async () => { test("fs.watch without AsyncLocalStorage context", async () => { const asyncLocalStorage = new AsyncLocalStorage(); const testFile = path.join(tmpdir(), "test-watch-no-context.txt"); - + // Create test file fs.writeFileSync(testFile, "initial content"); - + try { const promise = new Promise((resolve, reject) => { // Set up watcher outside of AsyncLocalStorage context @@ -63,7 +63,7 @@ test("fs.watch without AsyncLocalStorage context", async () => { // Should have no context const context = asyncLocalStorage.getStore(); expect(context).toBeUndefined(); - + watcher.close(); resolve(); } catch (error) { @@ -71,13 +71,13 @@ test("fs.watch without AsyncLocalStorage context", async () => { reject(error); } }); - + // Trigger the watcher by modifying the file setTimeout(() => { fs.writeFileSync(testFile, "modified content"); }, 10); }); - + await promise; } finally { // Clean up @@ -90,14 +90,14 @@ test("fs.watch without AsyncLocalStorage context", async () => { test("fs.watch nested AsyncLocalStorage context", async () => { const asyncLocalStorage = new AsyncLocalStorage(); const testFile = path.join(tmpdir(), "test-watch-nested-context.txt"); - + // Create test file fs.writeFileSync(testFile, "initial content"); - + try { const outerContext = { level: "outer", value: 1 }; const innerContext = { level: "inner", value: 2 }; - + const promise = new Promise((resolve, reject) => { asyncLocalStorage.run(outerContext, () => { asyncLocalStorage.run(innerContext, () => { @@ -108,7 +108,7 @@ test("fs.watch nested AsyncLocalStorage context", async () => { expect(context).toEqual(innerContext); expect(context.level).toBe("inner"); expect(context.value).toBe(2); - + watcher.close(); resolve(); } catch (error) { @@ -116,7 +116,7 @@ test("fs.watch nested AsyncLocalStorage context", async () => { reject(error); } }); - + // Trigger the watcher by modifying the file setTimeout(() => { fs.writeFileSync(testFile, "modified content"); @@ -124,7 +124,7 @@ test("fs.watch nested AsyncLocalStorage context", async () => { }); }); }); - + await promise; } finally { // Clean up @@ -132,4 +132,4 @@ test("fs.watch nested AsyncLocalStorage context", async () => { fs.unlinkSync(testFile); } catch {} } -}); \ No newline at end of file +});