@terreno/test 57.3.0 → 57.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bunfig.toml +13 -0
- package/dist/cache/mongoTestCache.test.d.ts +1 -0
- package/dist/cache/mongoTestCache.test.js +182 -0
- package/dist/http/getBaseServer.test.js +23 -8
- package/dist/logging/silenceLogs.test.d.ts +1 -0
- package/dist/logging/silenceLogs.test.js +51 -0
- package/dist/mocks/sentryBun.test.d.ts +1 -0
- package/dist/mocks/sentryBun.test.js +207 -0
- package/dist/mongo/connection.test.js +12 -0
- package/dist/mongo/mongoServer.test.d.ts +1 -0
- package/dist/mongo/mongoServer.test.js +196 -0
- package/dist/preload/defaultBackendPreload.js +3 -1
- package/dist/preload/registerBackendPreload.test.d.ts +1 -0
- package/dist/preload/registerBackendPreload.test.js +10 -0
- package/dist/transaction/testTransaction.test.d.ts +1 -0
- package/dist/transaction/testTransaction.test.js +142 -0
- package/dist/utils/helpers.test.d.ts +1 -0
- package/dist/utils/helpers.test.js +113 -0
- package/package.json +10 -10
- package/src/cache/mongoTestCache.test.ts +103 -0
- package/src/http/getBaseServer.test.ts +20 -5
- package/src/logging/silenceLogs.test.ts +52 -0
- package/src/mocks/sentryBun.test.ts +230 -0
- package/src/mongo/connection.test.ts +14 -0
- package/src/mongo/mongoServer.test.ts +74 -0
- package/src/preload/defaultBackendPreload.ts +5 -2
- package/src/preload/registerBackendPreload.test.ts +10 -0
- package/src/transaction/testTransaction.test.ts +64 -0
- package/src/utils/helpers.test.ts +46 -0
|
@@ -5,17 +5,20 @@ import {getBaseServer} from "./getBaseServer";
|
|
|
5
5
|
describe("getBaseServer", () => {
|
|
6
6
|
it("creates an express app with json parsing enabled", async () => {
|
|
7
7
|
const app = getBaseServer();
|
|
8
|
-
app.get("/ping", (
|
|
9
|
-
res.json({ok: true});
|
|
8
|
+
app.get("/ping", (req, res) => {
|
|
9
|
+
res.json({ok: true, query: req.query});
|
|
10
10
|
});
|
|
11
11
|
|
|
12
12
|
const server = app.listen(0);
|
|
13
13
|
const address = server.address();
|
|
14
14
|
const port = typeof address === "object" && address ? address.port : 0;
|
|
15
15
|
|
|
16
|
-
const
|
|
17
|
-
expect(
|
|
18
|
-
await
|
|
16
|
+
const pingResponse = await fetch(`http://127.0.0.1:${port}/ping?foo=bar&arr=1&arr=2`);
|
|
17
|
+
expect(pingResponse.status).toBe(200);
|
|
18
|
+
await pingResponse.json();
|
|
19
|
+
|
|
20
|
+
const optionsResponse = await fetch(`http://127.0.0.1:${port}/ping`, {method: "OPTIONS"});
|
|
21
|
+
expect(optionsResponse.status).toBe(200);
|
|
19
22
|
|
|
20
23
|
await new Promise<void>((resolve, reject) => {
|
|
21
24
|
server.close((error) => {
|
|
@@ -27,4 +30,16 @@ describe("getBaseServer", () => {
|
|
|
27
30
|
});
|
|
28
31
|
});
|
|
29
32
|
});
|
|
33
|
+
|
|
34
|
+
it("skips CORS and applies an OpenAPI compat patch", () => {
|
|
35
|
+
let patched = false;
|
|
36
|
+
const app = getBaseServer({
|
|
37
|
+
enableCors: false,
|
|
38
|
+
patchOpenApiCompat: () => {
|
|
39
|
+
patched = true;
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
expect(patched).toBe(true);
|
|
43
|
+
expect(app).toBeDefined();
|
|
44
|
+
});
|
|
30
45
|
});
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import {describe, expect, it} from "bun:test";
|
|
2
|
+
import winston from "winston";
|
|
3
|
+
|
|
4
|
+
import {createLogSilencer, registerLogSilencing} from "./silenceLogs";
|
|
5
|
+
|
|
6
|
+
describe("createLogSilencer", () => {
|
|
7
|
+
it("captures console output and restores originals", () => {
|
|
8
|
+
// biome-ignore lint/suspicious/noConsole: this test asserts console capture
|
|
9
|
+
const originalLog = console.log;
|
|
10
|
+
const silencer = createLogSilencer();
|
|
11
|
+
// biome-ignore lint/suspicious/noConsole: this test asserts console capture
|
|
12
|
+
console.log("captured-line");
|
|
13
|
+
expect(silencer.getLogs().some((line) => line.includes("captured-line"))).toBe(true);
|
|
14
|
+
silencer.clearLogs();
|
|
15
|
+
expect(silencer.getLogs()).toEqual([]);
|
|
16
|
+
silencer.restore();
|
|
17
|
+
// biome-ignore lint/suspicious/noConsole: this test asserts console capture
|
|
18
|
+
expect(console.log).toBe(originalLog);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("forwards captured winston lines when showAllLogs is true", () => {
|
|
22
|
+
const silencer = createLogSilencer({showAllLogs: true});
|
|
23
|
+
winston.info("visible-info");
|
|
24
|
+
expect(silencer.getLogs().length).toBeGreaterThan(0);
|
|
25
|
+
silencer.restore();
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("silences an extra winston logger", () => {
|
|
29
|
+
const extra = winston.createLogger({
|
|
30
|
+
transports: [new winston.transports.Console()],
|
|
31
|
+
});
|
|
32
|
+
const silencer = createLogSilencer({additionalWinstonLoggers: [extra]});
|
|
33
|
+
extra.info("extra-logger");
|
|
34
|
+
expect(silencer.getLogs().some((line) => line.includes("extra-logger"))).toBe(true);
|
|
35
|
+
silencer.restore();
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
describe("registerLogSilencing", () => {
|
|
40
|
+
let beforeCount = 0;
|
|
41
|
+
const silencer = registerLogSilencing({
|
|
42
|
+
onBeforeEach: () => {
|
|
43
|
+
beforeCount += 1;
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("registers lifecycle hooks without throwing", () => {
|
|
48
|
+
expect(typeof silencer.reapply).toBe("function");
|
|
49
|
+
expect(beforeCount).toBeGreaterThanOrEqual(0);
|
|
50
|
+
silencer.restore();
|
|
51
|
+
});
|
|
52
|
+
});
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import {describe, expect, it} from "bun:test";
|
|
2
|
+
|
|
3
|
+
import {registerSentryBunMock} from "./sentryBun";
|
|
4
|
+
|
|
5
|
+
interface MockFn {
|
|
6
|
+
(...args: unknown[]): unknown;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
interface MockClient {
|
|
10
|
+
captureException: MockFn;
|
|
11
|
+
captureMessage: MockFn;
|
|
12
|
+
close: () => Promise<unknown>;
|
|
13
|
+
flush: () => Promise<unknown>;
|
|
14
|
+
getOptions: MockFn;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface MockHub {
|
|
18
|
+
addBreadcrumb: MockFn;
|
|
19
|
+
captureException: MockFn;
|
|
20
|
+
captureMessage: MockFn;
|
|
21
|
+
configureScope: MockFn;
|
|
22
|
+
getClient: () => MockClient;
|
|
23
|
+
getScope: MockFn;
|
|
24
|
+
popScope: MockFn;
|
|
25
|
+
pushScope: MockFn;
|
|
26
|
+
setContext: MockFn;
|
|
27
|
+
setTag: MockFn;
|
|
28
|
+
setTags: MockFn;
|
|
29
|
+
setUser: MockFn;
|
|
30
|
+
withScope: MockFn;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
interface MockScope {
|
|
34
|
+
addBreadcrumb: MockFn;
|
|
35
|
+
clear: MockFn;
|
|
36
|
+
getSpan: MockFn;
|
|
37
|
+
setContext: MockFn;
|
|
38
|
+
setFingerprint: MockFn;
|
|
39
|
+
setLevel: MockFn;
|
|
40
|
+
setSpan: MockFn;
|
|
41
|
+
setTag: MockFn;
|
|
42
|
+
setTags: MockFn;
|
|
43
|
+
setTransactionName: MockFn;
|
|
44
|
+
setUser: MockFn;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
interface MockSpan {
|
|
48
|
+
finish: MockFn;
|
|
49
|
+
setData: MockFn;
|
|
50
|
+
setStatus: MockFn;
|
|
51
|
+
setTag: MockFn;
|
|
52
|
+
startChild: () => MockSpan;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
interface MockTransaction {
|
|
56
|
+
finish: MockFn;
|
|
57
|
+
setData: MockFn;
|
|
58
|
+
setName: MockFn;
|
|
59
|
+
setStatus: MockFn;
|
|
60
|
+
setTag: MockFn;
|
|
61
|
+
startChild: () => MockSpan;
|
|
62
|
+
toTraceparent: () => string;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
interface MockSentryModule {
|
|
66
|
+
addBreadcrumb: MockFn;
|
|
67
|
+
captureException: MockFn;
|
|
68
|
+
captureMessage: MockFn;
|
|
69
|
+
clearScope: MockFn;
|
|
70
|
+
close: () => Promise<unknown>;
|
|
71
|
+
configureScope: MockFn;
|
|
72
|
+
default: MockSentryModule;
|
|
73
|
+
flush: () => Promise<unknown>;
|
|
74
|
+
getClient: () => MockClient;
|
|
75
|
+
getCurrentHub: () => MockHub;
|
|
76
|
+
getCurrentScope: () => MockScope;
|
|
77
|
+
Handlers: {
|
|
78
|
+
errorHandler: () => MockFn;
|
|
79
|
+
requestHandler: () => MockFn;
|
|
80
|
+
tracingHandler: () => MockFn;
|
|
81
|
+
};
|
|
82
|
+
init: MockFn;
|
|
83
|
+
isInitialized: () => boolean;
|
|
84
|
+
logger: {
|
|
85
|
+
debug: MockFn;
|
|
86
|
+
error: MockFn;
|
|
87
|
+
fatal: MockFn;
|
|
88
|
+
info: MockFn;
|
|
89
|
+
trace: MockFn;
|
|
90
|
+
warn: MockFn;
|
|
91
|
+
};
|
|
92
|
+
popScope: MockFn;
|
|
93
|
+
pushScope: MockFn;
|
|
94
|
+
setContext: MockFn;
|
|
95
|
+
setFingerprint: MockFn;
|
|
96
|
+
setLevel: MockFn;
|
|
97
|
+
setTag: MockFn;
|
|
98
|
+
setTags: MockFn;
|
|
99
|
+
setUser: MockFn;
|
|
100
|
+
setupExpressErrorHandler: MockFn;
|
|
101
|
+
Severity: {
|
|
102
|
+
Error: string;
|
|
103
|
+
Warning: string;
|
|
104
|
+
};
|
|
105
|
+
startTransaction: (opts: {name: string}) => MockTransaction;
|
|
106
|
+
withScope: (callback: (scope: MockScope) => void) => void;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
describe("registerSentryBunMock", () => {
|
|
110
|
+
it("registers a mock @sentry/bun module and exercises mock surfaces", async () => {
|
|
111
|
+
registerSentryBunMock();
|
|
112
|
+
const sentry = (await import("@sentry/bun")) as unknown as MockSentryModule;
|
|
113
|
+
expect(typeof sentry.captureException).toBe("function");
|
|
114
|
+
expect(typeof sentry.captureMessage).toBe("function");
|
|
115
|
+
expect(typeof sentry.flush).toBe("function");
|
|
116
|
+
|
|
117
|
+
sentry.captureException(new Error("boom"));
|
|
118
|
+
sentry.captureMessage("hello");
|
|
119
|
+
sentry.init({});
|
|
120
|
+
sentry.addBreadcrumb({});
|
|
121
|
+
sentry.clearScope();
|
|
122
|
+
sentry.configureScope(() => undefined);
|
|
123
|
+
sentry.setContext("ctx", {});
|
|
124
|
+
sentry.setFingerprint(["f"]);
|
|
125
|
+
sentry.setLevel("error");
|
|
126
|
+
sentry.setTag("k", "v");
|
|
127
|
+
sentry.setTags({a: "b"});
|
|
128
|
+
sentry.setUser({id: "1"});
|
|
129
|
+
sentry.setupExpressErrorHandler();
|
|
130
|
+
sentry.pushScope();
|
|
131
|
+
sentry.popScope();
|
|
132
|
+
expect(sentry.isInitialized()).toBe(true);
|
|
133
|
+
expect(sentry.Severity.Error).toBe("error");
|
|
134
|
+
|
|
135
|
+
const client = sentry.getClient();
|
|
136
|
+
client.captureException(new Error("c"));
|
|
137
|
+
client.captureMessage("m");
|
|
138
|
+
await client.flush();
|
|
139
|
+
await client.close();
|
|
140
|
+
client.getOptions();
|
|
141
|
+
|
|
142
|
+
const hub = sentry.getCurrentHub();
|
|
143
|
+
hub.addBreadcrumb({});
|
|
144
|
+
hub.captureException(new Error("h"));
|
|
145
|
+
hub.captureMessage("hm");
|
|
146
|
+
hub.configureScope(() => undefined);
|
|
147
|
+
hub.getClient();
|
|
148
|
+
hub.getScope();
|
|
149
|
+
hub.popScope();
|
|
150
|
+
hub.pushScope();
|
|
151
|
+
hub.setContext("c", {});
|
|
152
|
+
hub.setTag("t", "v");
|
|
153
|
+
hub.setTags({t: "v"});
|
|
154
|
+
hub.setUser({id: "2"});
|
|
155
|
+
hub.withScope(() => undefined);
|
|
156
|
+
|
|
157
|
+
const scope = sentry.getCurrentScope();
|
|
158
|
+
scope.addBreadcrumb({});
|
|
159
|
+
scope.clear();
|
|
160
|
+
scope.getSpan();
|
|
161
|
+
scope.setContext("c", {});
|
|
162
|
+
scope.setFingerprint(["f"]);
|
|
163
|
+
scope.setLevel("info");
|
|
164
|
+
scope.setSpan({});
|
|
165
|
+
scope.setTag("t", "v");
|
|
166
|
+
scope.setTags({t: "v"});
|
|
167
|
+
scope.setTransactionName("tx");
|
|
168
|
+
scope.setUser({id: "3"});
|
|
169
|
+
|
|
170
|
+
const tx = sentry.startTransaction({name: "t"});
|
|
171
|
+
tx.setData("k", "v");
|
|
172
|
+
tx.setName("n");
|
|
173
|
+
tx.setStatus("ok");
|
|
174
|
+
tx.setTag("t", "v");
|
|
175
|
+
const child = tx.startChild();
|
|
176
|
+
child.setData("k", "v");
|
|
177
|
+
child.setStatus("ok");
|
|
178
|
+
child.setTag("t", "v");
|
|
179
|
+
child.startChild();
|
|
180
|
+
child.finish();
|
|
181
|
+
expect(tx.toTraceparent()).toBe("mock-trace-parent");
|
|
182
|
+
tx.finish();
|
|
183
|
+
|
|
184
|
+
sentry.logger.debug("d");
|
|
185
|
+
sentry.logger.error("e");
|
|
186
|
+
sentry.logger.fatal("f");
|
|
187
|
+
sentry.logger.info("i");
|
|
188
|
+
sentry.logger.trace("t");
|
|
189
|
+
sentry.logger.warn("w");
|
|
190
|
+
|
|
191
|
+
const next = (): void => undefined;
|
|
192
|
+
sentry.Handlers.errorHandler()(new Error("e"), {}, {}, next);
|
|
193
|
+
sentry.Handlers.requestHandler()({}, {}, next);
|
|
194
|
+
sentry.Handlers.tracingHandler()({}, {}, next);
|
|
195
|
+
|
|
196
|
+
sentry.withScope((innerScope: {setTag: (key: string, value: string) => void}) => {
|
|
197
|
+
innerScope.setTag("k", "v");
|
|
198
|
+
});
|
|
199
|
+
await sentry.flush();
|
|
200
|
+
await sentry.close();
|
|
201
|
+
|
|
202
|
+
sentry.default.captureException(new Error("d"));
|
|
203
|
+
sentry.default.captureMessage("dm");
|
|
204
|
+
sentry.default.init({});
|
|
205
|
+
sentry.default.addBreadcrumb({});
|
|
206
|
+
sentry.default.clearScope();
|
|
207
|
+
sentry.default.configureScope(() => undefined);
|
|
208
|
+
sentry.default.setContext("c", {});
|
|
209
|
+
sentry.default.setFingerprint(["f"]);
|
|
210
|
+
sentry.default.setLevel("error");
|
|
211
|
+
sentry.default.setTag("t", "v");
|
|
212
|
+
sentry.default.setTags({t: "v"});
|
|
213
|
+
sentry.default.setUser({id: "4"});
|
|
214
|
+
sentry.default.setupExpressErrorHandler();
|
|
215
|
+
sentry.default.pushScope();
|
|
216
|
+
sentry.default.popScope();
|
|
217
|
+
expect(sentry.default.isInitialized()).toBe(true);
|
|
218
|
+
sentry.default.logger.info("di");
|
|
219
|
+
sentry.default.startTransaction({name: "dt"}).finish();
|
|
220
|
+
await sentry.default.flush();
|
|
221
|
+
await sentry.default.close();
|
|
222
|
+
sentry.default.withScope(() => undefined);
|
|
223
|
+
sentry.default.Handlers.errorHandler()(new Error("de"), {}, {}, next);
|
|
224
|
+
sentry.default.Handlers.tracingHandler()({}, {}, next);
|
|
225
|
+
sentry.default.getClient();
|
|
226
|
+
sentry.default.getCurrentHub();
|
|
227
|
+
sentry.default.getCurrentScope();
|
|
228
|
+
expect(sentry.default.Severity.Warning).toBe("warning");
|
|
229
|
+
});
|
|
230
|
+
});
|
|
@@ -16,6 +16,20 @@ describe("mongo connection helpers", () => {
|
|
|
16
16
|
});
|
|
17
17
|
expect(uri).toBe("mongodb://127.0.0.1/terrenoTest_base?retryWrites=true");
|
|
18
18
|
});
|
|
19
|
+
|
|
20
|
+
it("buildDatabaseUri appends database name without query params", () => {
|
|
21
|
+
const uri = buildDatabaseUri({
|
|
22
|
+
databaseName: "terrenoTest_base",
|
|
23
|
+
uri: "mongodb://127.0.0.1",
|
|
24
|
+
});
|
|
25
|
+
expect(uri).toBe("mongodb://127.0.0.1/terrenoTest_base");
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("splitMongoUri keeps a URI that has no database path", () => {
|
|
29
|
+
const result = splitMongoUri("mongodb://127.0.0.1");
|
|
30
|
+
expect(result.baseUri).toBe("mongodb://127.0.0.1");
|
|
31
|
+
expect(result.uriOptions).toBe("");
|
|
32
|
+
});
|
|
19
33
|
});
|
|
20
34
|
|
|
21
35
|
describe("setTerrenoTestEnv", () => {
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import {describe, expect, it} from "bun:test";
|
|
2
|
+
import mongoose from "mongoose";
|
|
3
|
+
|
|
4
|
+
import {ensureTestMongooseConnected} from "./connection";
|
|
5
|
+
import {initializeModels, waitForDatabaseReady} from "./mongoServer";
|
|
6
|
+
|
|
7
|
+
describe("mongoServer", () => {
|
|
8
|
+
it("pings the connected database", async () => {
|
|
9
|
+
await waitForDatabaseReady(3, 10);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it("throws when mongoose has no database handle", async () => {
|
|
13
|
+
const original = Object.getOwnPropertyDescriptor(mongoose.connection, "db");
|
|
14
|
+
Object.defineProperty(mongoose.connection, "db", {
|
|
15
|
+
configurable: true,
|
|
16
|
+
get: () => undefined,
|
|
17
|
+
});
|
|
18
|
+
try {
|
|
19
|
+
await expect(waitForDatabaseReady(1, 1)).rejects.toThrow(/undefined/);
|
|
20
|
+
} finally {
|
|
21
|
+
if (original) {
|
|
22
|
+
Object.defineProperty(mongoose.connection, "db", original);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("initializes models and reports zero work when none are new", async () => {
|
|
28
|
+
const first = await initializeModels();
|
|
29
|
+
const second = await initializeModels();
|
|
30
|
+
expect(second.modelCount).toBe(first.modelCount);
|
|
31
|
+
expect(second.modelInitMs).toBe(0);
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
describe("ensureTestMongooseConnected", () => {
|
|
36
|
+
it("returns immediately when mongoose is already connected", async () => {
|
|
37
|
+
expect(mongoose.connection.readyState).toBe(1);
|
|
38
|
+
await ensureTestMongooseConnected();
|
|
39
|
+
expect(mongoose.connection.readyState).toBe(1);
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
describe("startMongoServer", () => {
|
|
44
|
+
it("retries ping on retryable mongo codes", async () => {
|
|
45
|
+
let calls = 0;
|
|
46
|
+
const original = mongoose.connection.db;
|
|
47
|
+
Object.defineProperty(mongoose.connection, "db", {
|
|
48
|
+
configurable: true,
|
|
49
|
+
get: () => ({
|
|
50
|
+
admin: () => ({
|
|
51
|
+
command: async () => {
|
|
52
|
+
calls += 1;
|
|
53
|
+
if (calls === 1) {
|
|
54
|
+
const error = new Error("not ready") as Error & {code: number};
|
|
55
|
+
error.code = 11600;
|
|
56
|
+
throw error;
|
|
57
|
+
}
|
|
58
|
+
return {ok: 1};
|
|
59
|
+
},
|
|
60
|
+
}),
|
|
61
|
+
}),
|
|
62
|
+
});
|
|
63
|
+
try {
|
|
64
|
+
const {waitForDatabaseReady} = await import("./mongoServer");
|
|
65
|
+
await waitForDatabaseReady(3, 1);
|
|
66
|
+
expect(calls).toBe(2);
|
|
67
|
+
} finally {
|
|
68
|
+
Object.defineProperty(mongoose.connection, "db", {
|
|
69
|
+
configurable: true,
|
|
70
|
+
get: () => original,
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
});
|
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {registerBackendPreload} from "./registerBackendPreload";
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
process.env.DEBUG_MONGO_PRELOAD = "true";
|
|
4
|
+
|
|
5
|
+
registerBackendPreload({
|
|
6
|
+
mongo: {useReplSet: true},
|
|
4
7
|
testEnv: {
|
|
5
8
|
tokenIssuer: "terreno-api.test",
|
|
6
9
|
},
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import {describe, expect, it} from "bun:test";
|
|
2
|
+
|
|
3
|
+
import {registerBackendPreload, registerSimpleMongoPreload} from "./registerBackendPreload";
|
|
4
|
+
|
|
5
|
+
describe("registerBackendPreload", () => {
|
|
6
|
+
it("exports preload helpers", () => {
|
|
7
|
+
expect(typeof registerSimpleMongoPreload).toBe("function");
|
|
8
|
+
expect(typeof registerBackendPreload).toBe("function");
|
|
9
|
+
});
|
|
10
|
+
});
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import {describe, expect, it} from "bun:test";
|
|
2
|
+
import mongoose from "mongoose";
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
abortTestTransaction,
|
|
6
|
+
getTestSession,
|
|
7
|
+
installTransactionPatches,
|
|
8
|
+
resetTestSessionAfterReconnect,
|
|
9
|
+
startTestTransaction,
|
|
10
|
+
} from "./testTransaction";
|
|
11
|
+
|
|
12
|
+
const TxWidget =
|
|
13
|
+
mongoose.models.TxCoverageWidget ??
|
|
14
|
+
mongoose.model(
|
|
15
|
+
"TxCoverageWidget",
|
|
16
|
+
new mongoose.Schema({name: String}, {collection: "tx_coverage_widgets"})
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
describe("testTransaction", () => {
|
|
20
|
+
it("installs patches idempotently and no-ops without a session", async () => {
|
|
21
|
+
expect(getTestSession()).toBeNull();
|
|
22
|
+
installTransactionPatches();
|
|
23
|
+
installTransactionPatches();
|
|
24
|
+
await abortTestTransaction();
|
|
25
|
+
await resetTestSessionAfterReconnect();
|
|
26
|
+
expect(getTestSession()).toBeNull();
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("serializes mongoose operations while a test session is active", async () => {
|
|
30
|
+
installTransactionPatches();
|
|
31
|
+
try {
|
|
32
|
+
await startTestTransaction();
|
|
33
|
+
expect(getTestSession()).toBeTruthy();
|
|
34
|
+
await TxWidget.create({name: "tx-a"});
|
|
35
|
+
await TxWidget.find({name: "tx-a"});
|
|
36
|
+
await TxWidget.updateOne({name: "tx-a"}, {name: "tx-b"});
|
|
37
|
+
await TxWidget.deleteOne({name: "tx-b"});
|
|
38
|
+
try {
|
|
39
|
+
await TxWidget.insertMany([{name: "tx-c"}]);
|
|
40
|
+
} catch {
|
|
41
|
+
// replica-set-only path
|
|
42
|
+
}
|
|
43
|
+
try {
|
|
44
|
+
await TxWidget.bulkWrite([{insertOne: {document: {name: "tx-d"}}}]);
|
|
45
|
+
} catch {
|
|
46
|
+
// replica-set-only path
|
|
47
|
+
}
|
|
48
|
+
try {
|
|
49
|
+
await TxWidget.aggregate([{$match: {name: "tx-a"}}]);
|
|
50
|
+
} catch {
|
|
51
|
+
// replica-set-only path
|
|
52
|
+
}
|
|
53
|
+
const session = await TxWidget.startSession();
|
|
54
|
+
expect(session).toBeDefined();
|
|
55
|
+
await Promise.allSettled([TxWidget.find({name: "tx-a"}), TxWidget.find({name: "tx-b"})]);
|
|
56
|
+
} catch {
|
|
57
|
+
// Standalone memory Mongo rejects multi-document transactions.
|
|
58
|
+
} finally {
|
|
59
|
+
await abortTestTransaction();
|
|
60
|
+
await resetTestSessionAfterReconnect();
|
|
61
|
+
expect(getTestSession()).toBeNull();
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
});
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import {describe, expect, it} from "bun:test";
|
|
2
|
+
import mongoose from "mongoose";
|
|
3
|
+
import {ensureAllIndexes} from "./ensureAllIndexes";
|
|
4
|
+
import {timeout} from "./timeout";
|
|
5
|
+
import {waitForDocument, waitForDocuments} from "./waitForDocuments";
|
|
6
|
+
|
|
7
|
+
const Widget =
|
|
8
|
+
mongoose.models.CoverageWidget ??
|
|
9
|
+
mongoose.model(
|
|
10
|
+
"CoverageWidget",
|
|
11
|
+
new mongoose.Schema({name: {required: true, type: String}}, {collection: "coverage_widgets"})
|
|
12
|
+
);
|
|
13
|
+
|
|
14
|
+
describe("timeout", () => {
|
|
15
|
+
it("resolves after the requested delay", async () => {
|
|
16
|
+
await timeout(1);
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
describe("ensureAllIndexes", () => {
|
|
21
|
+
it("creates indexes for registered models", async () => {
|
|
22
|
+
await ensureAllIndexes();
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
describe("waitForDocuments", () => {
|
|
27
|
+
it("returns matching documents and supports sort", async () => {
|
|
28
|
+
await Widget.deleteMany({});
|
|
29
|
+
await Widget.create({name: "b"});
|
|
30
|
+
await Widget.create({name: "a"});
|
|
31
|
+
const docs = await waitForDocuments(Widget, {}, 2, {
|
|
32
|
+
intervalMs: 10,
|
|
33
|
+
sort: {name: 1},
|
|
34
|
+
timeoutMs: 2000,
|
|
35
|
+
});
|
|
36
|
+
expect(docs.map((doc) => (doc as {name: string}).name)).toEqual(["a", "b"]);
|
|
37
|
+
const one = await waitForDocument(Widget, {name: "a"}, {intervalMs: 10, timeoutMs: 2000});
|
|
38
|
+
expect((one as {name: string}).name).toBe("a");
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("throws when the timeout elapses", async () => {
|
|
42
|
+
await expect(
|
|
43
|
+
waitForDocuments(Widget, {name: "missing-coverage-doc"}, 1, {intervalMs: 5, timeoutMs: 20})
|
|
44
|
+
).rejects.toThrow(/Timed out/);
|
|
45
|
+
});
|
|
46
|
+
});
|