Skip to content

Commit

Permalink
Ref: adjusting express mock for bridge implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinTail committed Oct 15, 2024
1 parent 422a583 commit 2a091b4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
2 changes: 1 addition & 1 deletion coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 26 additions & 17 deletions tests/express-mock.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Mock } from "vitest";

Check failure on line 1 in tests/express-mock.ts

View workflow job for this annotation

GitHub Actions / build (18.18.0)

'Mock' is defined but never used

Check failure on line 1 in tests/express-mock.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'Mock' is defined but never used

Check failure on line 1 in tests/express-mock.ts

View workflow job for this annotation

GitHub Actions / build (20.9.0)

'Mock' is defined but never used

Check failure on line 1 in tests/express-mock.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

'Mock' is defined but never used

Check failure on line 1 in tests/express-mock.ts

View workflow job for this annotation

GitHub Actions / build (22.0.0)

'Mock' is defined but never used

Check failure on line 1 in tests/express-mock.ts

View workflow job for this annotation

GitHub Actions / build (22.x)

'Mock' is defined but never used
import { makeRequestMock, makeResponseMock } from "../src/testing";

const expressJsonMock = vi.fn();
const expressRawMock = vi.fn();
Expand All @@ -11,27 +12,34 @@ vi.mock("express-fileupload", () => ({ default: fileUploadMock }));
const staticHandler = vi.fn();
const staticMock = vi.fn(() => staticHandler);

let appMock: Record<
"disable" | "use" | "get" | "post" | "put" | "patch" | "delete" | "options",
Mock
>;

const expressMock = () => {
appMock = {
disable: vi.fn(() => appMock),
use: vi.fn(() => appMock),
get: vi.fn(),
post: vi.fn(),
put: vi.fn(),
patch: vi.fn(),
delete: vi.fn(),
options: vi.fn(),
};
return appMock;
const appMock = {
disable: vi.fn(() => appMock),
use: vi.fn(() => appMock),
get: vi.fn(),
post: vi.fn(),
put: vi.fn(),
patch: vi.fn(),
delete: vi.fn(),
options: vi.fn(),
init: vi.fn(),
};

const resetAppMock = () => {
for (const key in appMock) {
const prop = appMock[key as keyof typeof appMock];
if (prop && "mockClear" in prop && typeof prop.mockClear === "function") {
prop.mockClear();
}
}
};

const expressMock = () => appMock;
expressMock.json = () => expressJsonMock;
expressMock.raw = () => expressRawMock;
expressMock.static = staticMock;
expressMock.application = appMock;
expressMock.request = makeRequestMock();
expressMock.response = makeResponseMock();

vi.mock("express", () => ({ default: expressMock }));

Expand All @@ -44,4 +52,5 @@ export {
expressRawMock,
staticMock,
staticHandler,
resetAppMock,
};
5 changes: 5 additions & 0 deletions tests/unit/server.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
expressJsonMock,
expressMock,
expressRawMock,
resetAppMock,
} from "../express-mock";
import {
createHttpsServerSpy,
Expand All @@ -31,6 +32,10 @@ describe("Server", () => {
vi.restoreAllMocks();
});

afterEach(() => {
resetAppMock();
});

test("Express is mocked", () => {
expect(expressMock).toBeTruthy();
});
Expand Down

0 comments on commit 2a091b4

Please sign in to comment.