-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathapp.test.ts
More file actions
132 lines (121 loc) · 3.84 KB
/
app.test.ts
File metadata and controls
132 lines (121 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* eslint-disable @typescript-eslint/no-explicit-any */
// import { describe, it, expect } from "@jest/globals";
import * as app from "../src/app";
import { Registry } from "../src/server/registry";
// Use the same HttpMethods type as in app.ts
const httpMethod = "get";
// Minimal valid mock Config
const mockConfig = {
openApiPath: "_",
basePath: ".",
port: 1234,
alwaysFakeOptionals: false,
generate: { routes: false, types: false },
proxyPaths: new Map(),
proxyUrl: "",
startAdminApi: false,
startRepl: false,
startServer: false,
watch: { routes: false, types: false },
routePrefix: "",
};
// Minimal valid mock MockRequest
const mockRequest = {
method: httpMethod,
rawPath: "/foo",
body: undefined,
headers: {},
path: "/foo",
query: {},
req: {},
};
class MockModuleLoader {
private registry: any;
constructor(basePath: string, registry: Registry) {
this.registry = registry;
}
async load() {
// Register a mock route in the registry for GET /foo
this.registry.add("/foo", {
get: async () => ({ ok: true }),
});
}
async watch() {}
async stopWatching() {}
}
describe("handleMswRequest", () => {
it("returns 404 if no handler exists", async () => {
const result = await (app as any).handleMswRequest(mockRequest);
expect(result).toEqual({
error: "No handler found for get /foo",
status: 404,
});
});
it("calls the correct handler if present", async () => {
await (app as any).createMswHandlers(
{
...mockConfig,
openApiPath: "test/fixtures/openapi-example.yaml",
},
MockModuleLoader,
);
const result = await (app as any).handleMswRequest(mockRequest);
expect(result).toBeDefined();
expect(result.ok).toBe(true);
});
});
describe("counterfact", () => {
it("returns a startRepl function", async () => {
const result = await (app as any).counterfact(mockConfig);
expect(typeof result.startRepl).toBe("function");
});
it("returns contextRegistry, registry, koaApp, koaMiddleware, and start", async () => {
const result = await (app as any).counterfact(mockConfig);
expect(result.contextRegistry).toBeDefined();
expect(result.registry).toBeDefined();
expect(result.koaApp).toBeDefined();
expect(result.koaMiddleware).toBeDefined();
expect(typeof result.start).toBe("function");
});
it("does not start the REPL automatically", async () => {
// If start() still auto-started the REPL, it would call repl.start() which binds
// to stdin; testing the `startRepl` property being a separate callable is the
// architectural contract. We also verify start() returns a stop() function (not
// a replServer), confirming the REPL is no longer embedded in the return value.
const { start, startRepl } = await (app as any).counterfact({
...mockConfig,
startRepl: true,
});
const result = await start({ ...mockConfig, startRepl: true });
expect(typeof startRepl).toBe("function");
expect(typeof result.stop).toBe("function");
expect((result as any).replServer).toBeUndefined();
await result.stop();
});
});
describe("createMswHandlers", () => {
it("throws if openApiDocument is undefined", async () => {
await expect(
(app as any).createMswHandlers(
{
...mockConfig,
openApiPath: "nonexistent.yaml",
},
MockModuleLoader,
),
).rejects.toThrow();
});
it("returns handlers for valid openApiDocument", async () => {
const handlers = await (app as any).createMswHandlers(
{
...mockConfig,
openApiPath: "test/fixtures/openapi-example.yaml",
},
MockModuleLoader,
);
expect(Array.isArray(handlers)).toBe(true);
expect(handlers.length).toBeGreaterThan(0);
expect(handlers[0]).toHaveProperty("method");
expect(handlers[0]).toHaveProperty("path");
});
});