Files
outline/server/middlewares/timeout.test.ts
T
Tom Moor 091346dfe8 chore: Migrate to vitest (#12272)
* wip

* Remove obsolete snapshots

* simplify

* chore(test): Convert mocks to TypeScript and tighten fetch mock types

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* Remove unneccessary patches

* Migrate to msw instead of custom fetch mock

* Address PR review comments

- Split chained vi.useFakeTimers().setSystemTime() into separate calls.
- Switch test setup to dynamic imports so EventEmitter.defaultMaxListeners
  assignment runs before module init (static imports were hoisted above it).
- Drop redundant NODE_ENV guard in monkeyPatchSequelizeErrorsForJest; its
  sole caller already gates on env.isTest.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-06 21:10:51 -04:00

106 lines
2.6 KiB
TypeScript

import type { Socket } from "net";
import timeout from "./timeout";
describe("Timeout middleware", () => {
it("should set request timeout and restore it after request", async () => {
const originalTimeout = 10000;
const newTimeout = 1800000; // 30 minutes
const setTimeout = vi.fn();
const mockSocket = {
timeout: originalTimeout,
setTimeout,
} as unknown as Socket;
const ctx = {
req: {
socket: mockSocket,
},
};
const next = vi.fn();
const middleware = timeout(newTimeout);
await middleware(
// @ts-expect-error mock context
ctx,
next
);
// Should have set the new timeout
expect(setTimeout).toHaveBeenCalledWith(newTimeout);
// Should have called next
expect(next).toHaveBeenCalled();
// Should have restored the original timeout
expect(setTimeout).toHaveBeenCalledWith(originalTimeout);
});
it("should restore original timeout even if next throws", async () => {
const originalTimeout = 10000;
const newTimeout = 1800000; // 30 minutes
const setTimeout = vi.fn();
const mockSocket = {
timeout: originalTimeout,
setTimeout,
} as unknown as Socket;
const ctx = {
req: {
socket: mockSocket,
},
};
const error = new Error("Test error");
const next = vi.fn().mockRejectedValue(error);
const middleware = timeout(newTimeout);
await expect(
middleware(
// @ts-expect-error mock context
ctx,
next
)
).rejects.toThrow("Test error");
// Should have set the new timeout
expect(setTimeout).toHaveBeenCalledWith(newTimeout);
// Should have called next
expect(next).toHaveBeenCalled();
// Should have restored the original timeout even after error
expect(setTimeout).toHaveBeenCalledWith(originalTimeout);
});
it("should handle undefined original timeout", async () => {
const newTimeout = 1800000; // 30 minutes
const setTimeout = vi.fn();
const mockSocket = {
timeout: undefined,
setTimeout,
} as unknown as Socket;
const ctx = {
req: {
socket: mockSocket,
},
};
const next = vi.fn();
const middleware = timeout(newTimeout);
await middleware(
// @ts-expect-error mock context
ctx,
next
);
// Should have set the new timeout
expect(setTimeout).toHaveBeenCalledWith(newTimeout);
// Should have called next
expect(next).toHaveBeenCalled();
// Should have restored timeout to 0 when original was undefined
expect(setTimeout).toHaveBeenCalledWith(0);
});
});