@terreno/test 0.22.1
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 +3 -0
- package/dist/cache/mongoTestCache.d.ts +16 -0
- package/dist/cache/mongoTestCache.js +541 -0
- package/dist/env/setTerrenoTestEnv.d.ts +13 -0
- package/dist/env/setTerrenoTestEnv.js +89 -0
- package/dist/http/authAsUser.d.ts +17 -0
- package/dist/http/authAsUser.js +165 -0
- package/dist/http/authAsUser.test.d.ts +1 -0
- package/dist/http/authAsUser.test.js +93 -0
- package/dist/http/getBaseServer.d.ts +9 -0
- package/dist/http/getBaseServer.js +33 -0
- package/dist/http/getBaseServer.test.d.ts +1 -0
- package/dist/http/getBaseServer.test.js +76 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +46 -0
- package/dist/logging/setupEnvironment.d.ts +2 -0
- package/dist/logging/setupEnvironment.js +19 -0
- package/dist/logging/silenceLogs.d.ts +20 -0
- package/dist/logging/silenceLogs.js +168 -0
- package/dist/logging/testLogger.d.ts +7 -0
- package/dist/logging/testLogger.js +22 -0
- package/dist/mocks/sentryBun.d.ts +2 -0
- package/dist/mocks/sentryBun.js +136 -0
- package/dist/mongo/connection.d.ts +16 -0
- package/dist/mongo/connection.js +134 -0
- package/dist/mongo/connection.test.d.ts +1 -0
- package/dist/mongo/connection.test.js +28 -0
- package/dist/mongo/ensureMongoBinary.d.ts +6 -0
- package/dist/mongo/ensureMongoBinary.js +104 -0
- package/dist/mongo/mongoServer.d.ts +26 -0
- package/dist/mongo/mongoServer.js +424 -0
- package/dist/preload/defaultBackendPreload.d.ts +1 -0
- package/dist/preload/defaultBackendPreload.js +8 -0
- package/dist/preload/memoryMongo.d.ts +0 -0
- package/dist/preload/memoryMongo.js +2 -0
- package/dist/preload/registerBackendPreload.d.ts +33 -0
- package/dist/preload/registerBackendPreload.js +302 -0
- package/dist/transaction/testTransaction.d.ts +91 -0
- package/dist/transaction/testTransaction.js +719 -0
- package/dist/utils/ensureAllIndexes.d.ts +2 -0
- package/dist/utils/ensureAllIndexes.js +84 -0
- package/dist/utils/timeout.d.ts +1 -0
- package/dist/utils/timeout.js +45 -0
- package/dist/utils/waitForDocuments.d.ts +13 -0
- package/dist/utils/waitForDocuments.js +127 -0
- package/package.json +65 -0
- package/src/cache/mongoTestCache.ts +311 -0
- package/src/env/setTerrenoTestEnv.ts +50 -0
- package/src/http/authAsUser.test.ts +40 -0
- package/src/http/authAsUser.ts +59 -0
- package/src/http/getBaseServer.test.ts +30 -0
- package/src/http/getBaseServer.ts +34 -0
- package/src/index.ts +53 -0
- package/src/logging/setupEnvironment.ts +15 -0
- package/src/logging/silenceLogs.ts +134 -0
- package/src/logging/testLogger.ts +19 -0
- package/src/mocks/sentryBun.ts +140 -0
- package/src/mongo/connection.test.ts +30 -0
- package/src/mongo/connection.ts +65 -0
- package/src/mongo/ensureMongoBinary.ts +25 -0
- package/src/mongo/mongoServer.ts +214 -0
- package/src/preload/defaultBackendPreload.ts +7 -0
- package/src/preload/memoryMongo.ts +1 -0
- package/src/preload/registerBackendPreload.ts +174 -0
- package/src/transaction/testTransaction.ts +524 -0
- package/src/utils/ensureAllIndexes.ts +11 -0
- package/src/utils/timeout.ts +3 -0
- package/src/utils/waitForDocuments.ts +50 -0
- package/tsconfig.json +31 -0
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pre-downloads the MongoDB binary used by mongodb-memory-server.
|
|
3
|
+
*
|
|
4
|
+
* Usage: bun ./src/mongo/ensureMongoBinary.ts
|
|
5
|
+
*/
|
|
6
|
+
export const ensureMongoBinary = async (): Promise<void> => {
|
|
7
|
+
if (process.env.CI) {
|
|
8
|
+
console.info("[ensureMongoBinary] Skipping on CI (using external MongoDB when configured)");
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const {MongoBinary} = await import("mongodb-memory-server-core");
|
|
13
|
+
|
|
14
|
+
const startTime = Date.now();
|
|
15
|
+
console.info("[ensureMongoBinary] Ensuring MongoDB binary is downloaded and cached...");
|
|
16
|
+
|
|
17
|
+
const binaryPath = await MongoBinary.getPath();
|
|
18
|
+
const elapsed = Date.now() - startTime;
|
|
19
|
+
|
|
20
|
+
console.info(`[ensureMongoBinary] MongoDB binary ready at: ${binaryPath} (${elapsed}ms)`);
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
if (require.main === module) {
|
|
24
|
+
void ensureMongoBinary();
|
|
25
|
+
}
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import mongoose from "mongoose";
|
|
2
|
+
|
|
3
|
+
import {testLogger} from "../logging/testLogger";
|
|
4
|
+
|
|
5
|
+
import {buildDatabaseUri} from "./connection";
|
|
6
|
+
|
|
7
|
+
export interface MemoryMongoHandle {
|
|
8
|
+
getUri: () => string;
|
|
9
|
+
stop: () => Promise<boolean>;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface MongoServerOptions {
|
|
13
|
+
/** Env var checked first for an external MongoDB URI (CI containers). */
|
|
14
|
+
externalUriEnvVar?: string;
|
|
15
|
+
/** Env var written with the resolved Mongo URI for consumers. */
|
|
16
|
+
publishedUriEnvVar?: string;
|
|
17
|
+
baseDatabaseName?: string;
|
|
18
|
+
useReplSet?: boolean;
|
|
19
|
+
debug?: boolean;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const DEFAULT_OPTIONS: Required<
|
|
23
|
+
Pick<MongoServerOptions, "externalUriEnvVar" | "publishedUriEnvVar" | "baseDatabaseName">
|
|
24
|
+
> = {
|
|
25
|
+
baseDatabaseName: "terrenoTest_base",
|
|
26
|
+
externalUriEnvVar: "TERRENO_TEST_MONGODB_URI",
|
|
27
|
+
publishedUriEnvVar: "TERRENO_TEST_MONGO_URI",
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const RETRYABLE_MONGO_CODES = new Set([11600, 91]);
|
|
31
|
+
|
|
32
|
+
const initializedModelNames = new Set<string>();
|
|
33
|
+
|
|
34
|
+
let memoryMongo: MemoryMongoHandle | undefined;
|
|
35
|
+
let mongoServerUri: string | null = null;
|
|
36
|
+
|
|
37
|
+
export const waitForDatabaseReady = async (maxAttempts = 15, delayMs = 1000): Promise<void> => {
|
|
38
|
+
const db = mongoose.connection.db;
|
|
39
|
+
if (!db) {
|
|
40
|
+
throw new Error("mongoose.connection.db is undefined");
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
|
44
|
+
try {
|
|
45
|
+
await db.admin().command({ping: 1});
|
|
46
|
+
return;
|
|
47
|
+
} catch (error: unknown) {
|
|
48
|
+
const err = error as {code?: number; cause?: {code?: number}};
|
|
49
|
+
const code = err?.code ?? err?.cause?.code;
|
|
50
|
+
const isRetryable = code != null && RETRYABLE_MONGO_CODES.has(code);
|
|
51
|
+
if (!isRetryable || attempt === maxAttempts) {
|
|
52
|
+
throw error;
|
|
53
|
+
}
|
|
54
|
+
if (process.env.DEBUG_MONGO_PRELOAD === "true") {
|
|
55
|
+
testLogger.debug(
|
|
56
|
+
`[mongoServer] Database not ready (attempt ${attempt}/${maxAttempts}, code=${code}), retrying in ${delayMs}ms...`
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
export const initializeModels = async (): Promise<{modelCount: number; modelInitMs: number}> => {
|
|
65
|
+
const models = Object.keys(mongoose.models);
|
|
66
|
+
const newModels = models.filter((modelName) => !initializedModelNames.has(modelName));
|
|
67
|
+
|
|
68
|
+
if (newModels.length === 0) {
|
|
69
|
+
return {modelCount: models.length, modelInitMs: 0};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const startTime = Date.now();
|
|
73
|
+
const initPromises = newModels.map(async (modelName) => {
|
|
74
|
+
await mongoose.models[modelName].init();
|
|
75
|
+
});
|
|
76
|
+
await Promise.all(initPromises);
|
|
77
|
+
|
|
78
|
+
for (const modelName of newModels) {
|
|
79
|
+
initializedModelNames.add(modelName);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return {modelCount: models.length, modelInitMs: Date.now() - startTime};
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const shouldUseReplSet = (options: MongoServerOptions): boolean => {
|
|
86
|
+
if (options.useReplSet !== undefined) {
|
|
87
|
+
return options.useReplSet;
|
|
88
|
+
}
|
|
89
|
+
return process.env.TERRENO_TEST_USE_REPLSET === "true";
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const startMemoryServer = async (useReplSet: boolean): Promise<string> => {
|
|
93
|
+
if (useReplSet) {
|
|
94
|
+
const {MongoMemoryReplSet} = await import("mongodb-memory-server-global");
|
|
95
|
+
const replSet = await MongoMemoryReplSet.create({
|
|
96
|
+
replSet: {
|
|
97
|
+
args: ["--wiredTigerCacheSizeGB", "0.25"],
|
|
98
|
+
count: 1,
|
|
99
|
+
storageEngine: "wiredTiger",
|
|
100
|
+
},
|
|
101
|
+
});
|
|
102
|
+
await replSet.waitUntilRunning();
|
|
103
|
+
memoryMongo = replSet;
|
|
104
|
+
return replSet.getUri();
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const {MongoMemoryServer} = await import("mongodb-memory-server");
|
|
108
|
+
const server = await MongoMemoryServer.create();
|
|
109
|
+
memoryMongo = server;
|
|
110
|
+
return server.getUri();
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Starts or reuses the MongoDB server for tests and connects mongoose once per worker.
|
|
115
|
+
*/
|
|
116
|
+
export const startMongoServer = async (options: MongoServerOptions = {}): Promise<string> => {
|
|
117
|
+
if (mongoServerUri) {
|
|
118
|
+
return mongoServerUri;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const merged = {...DEFAULT_OPTIONS, ...options};
|
|
122
|
+
const startTime = Date.now();
|
|
123
|
+
const externalUri = process.env[merged.externalUriEnvVar]?.trim();
|
|
124
|
+
let uri: string;
|
|
125
|
+
|
|
126
|
+
if (externalUri) {
|
|
127
|
+
uri = externalUri;
|
|
128
|
+
if (process.env.DEBUG_MONGO_PRELOAD === "true") {
|
|
129
|
+
testLogger.debug(`[mongoServer] Using external MongoDB at ${externalUri}`);
|
|
130
|
+
}
|
|
131
|
+
} else {
|
|
132
|
+
uri = await startMemoryServer(shouldUseReplSet(options));
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
mongoServerUri = uri;
|
|
136
|
+
process.env[merged.publishedUriEnvVar] = uri;
|
|
137
|
+
|
|
138
|
+
const connectionUri = buildDatabaseUri({databaseName: merged.baseDatabaseName, uri});
|
|
139
|
+
await mongoose.connect(connectionUri, {
|
|
140
|
+
connectTimeoutMS: 5000,
|
|
141
|
+
maxPoolSize: 5,
|
|
142
|
+
minPoolSize: 1,
|
|
143
|
+
serverSelectionTimeoutMS: 5000,
|
|
144
|
+
socketTimeoutMS: 0,
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
await waitForDatabaseReady();
|
|
148
|
+
const {modelCount, modelInitMs} = await initializeModels();
|
|
149
|
+
|
|
150
|
+
if (process.env.DEBUG_MONGO_PRELOAD === "true") {
|
|
151
|
+
testLogger.debug(
|
|
152
|
+
`[mongoServer] Initialized ${modelCount} models in ${modelInitMs}ms, connected in ${Date.now() - startTime}ms`
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
return uri;
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
/** Force-restarts the in-memory MongoDB server and reconnects mongoose. */
|
|
160
|
+
export const restartMongoServer = async (options: MongoServerOptions = {}): Promise<void> => {
|
|
161
|
+
const merged = {...DEFAULT_OPTIONS, ...options};
|
|
162
|
+
const startTime = Date.now();
|
|
163
|
+
testLogger.warn("[mongoServer] Force-restarting MongoDB server...");
|
|
164
|
+
|
|
165
|
+
try {
|
|
166
|
+
await mongoose.disconnect();
|
|
167
|
+
} catch {
|
|
168
|
+
// Connection may already be dead.
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (memoryMongo) {
|
|
172
|
+
try {
|
|
173
|
+
await memoryMongo.stop();
|
|
174
|
+
} catch {
|
|
175
|
+
// Ignore stop errors.
|
|
176
|
+
}
|
|
177
|
+
memoryMongo = undefined;
|
|
178
|
+
mongoServerUri = null;
|
|
179
|
+
const uri = await startMemoryServer(shouldUseReplSet(options));
|
|
180
|
+
mongoServerUri = uri;
|
|
181
|
+
process.env[merged.publishedUriEnvVar] = uri;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const uri = mongoServerUri;
|
|
185
|
+
if (!uri) {
|
|
186
|
+
throw new Error("[mongoServer] No Mongo URI available after restart");
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const connectionUri = buildDatabaseUri({databaseName: merged.baseDatabaseName, uri});
|
|
190
|
+
await mongoose.connect(connectionUri, {
|
|
191
|
+
connectTimeoutMS: 2000,
|
|
192
|
+
maxPoolSize: 10,
|
|
193
|
+
minPoolSize: 10,
|
|
194
|
+
serverSelectionTimeoutMS: 2000,
|
|
195
|
+
socketTimeoutMS: 2000,
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
await waitForDatabaseReady(5, 500);
|
|
199
|
+
initializedModelNames.clear();
|
|
200
|
+
await initializeModels();
|
|
201
|
+
|
|
202
|
+
testLogger.warn(`[mongoServer] MongoDB server restarted in ${Date.now() - startTime}ms`);
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
export const stopMongoServer = async (): Promise<void> => {
|
|
206
|
+
await mongoose.connection.close();
|
|
207
|
+
if (memoryMongo) {
|
|
208
|
+
await memoryMongo.stop();
|
|
209
|
+
memoryMongo = undefined;
|
|
210
|
+
}
|
|
211
|
+
mongoServerUri = null;
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
export const getMongoServerUri = (): string | null => mongoServerUri;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
process.env.TERRENO_TEST_USE_MEMORY_MONGO = "true";
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import {afterAll, afterEach, beforeAll, beforeEach, jest, mock, setSystemTime} from "bun:test";
|
|
2
|
+
import mongoose from "mongoose";
|
|
3
|
+
|
|
4
|
+
import {testLogger} from "../logging/testLogger";
|
|
5
|
+
|
|
6
|
+
import {setTerrenoTestEnv, type TerrenoTestEnvOptions} from "../env/setTerrenoTestEnv";
|
|
7
|
+
import {
|
|
8
|
+
createLogSilencer,
|
|
9
|
+
registerLogSilencing,
|
|
10
|
+
type SilenceLogsController,
|
|
11
|
+
type SilenceLogsOptions,
|
|
12
|
+
} from "../logging/silenceLogs";
|
|
13
|
+
import {registerSentryBunMock} from "../mocks/sentryBun";
|
|
14
|
+
import {
|
|
15
|
+
initializeModels,
|
|
16
|
+
startMongoServer,
|
|
17
|
+
stopMongoServer,
|
|
18
|
+
type MongoServerOptions,
|
|
19
|
+
} from "../mongo/mongoServer";
|
|
20
|
+
import {
|
|
21
|
+
abortTestTransaction,
|
|
22
|
+
installTransactionPatches,
|
|
23
|
+
startTestTransaction,
|
|
24
|
+
} from "../transaction/testTransaction";
|
|
25
|
+
|
|
26
|
+
export interface BackendPreloadOptions {
|
|
27
|
+
disableDb?: boolean;
|
|
28
|
+
mongo?: MongoServerOptions;
|
|
29
|
+
/** When true, `beforeAll` connects via `startMongoServer`. When false, the caller manages mongoose. */
|
|
30
|
+
connectMongoInBeforeAll?: boolean;
|
|
31
|
+
useTransactions?: boolean;
|
|
32
|
+
silenceLogs?: boolean | SilenceLogsOptions;
|
|
33
|
+
sentryMock?: boolean;
|
|
34
|
+
testEnv?: TerrenoTestEnvOptions;
|
|
35
|
+
onBeforeAll?: () => void | Promise<void>;
|
|
36
|
+
onAfterAll?: () => void | Promise<void>;
|
|
37
|
+
onBeforeEach?: () => void | Promise<void>;
|
|
38
|
+
onAfterEach?: () => void | Promise<void>;
|
|
39
|
+
loadModels?: () => Promise<void>;
|
|
40
|
+
loadTestDataFromCache?: () => Promise<void>;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
type LogSilencer = SilenceLogsController;
|
|
44
|
+
|
|
45
|
+
const shouldDisableDb = (options: BackendPreloadOptions): boolean => {
|
|
46
|
+
if (options.disableDb) {
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
return process.env.BUN_TEST_DISABLE_DB === "true";
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
let isServerStarted = false;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Registers Bun test lifecycle hooks for Terreno backend packages.
|
|
56
|
+
* Call once from a package preload file (e.g. `api/src/tests/bunSetup.ts`).
|
|
57
|
+
*/
|
|
58
|
+
export const registerBackendPreload = (options: BackendPreloadOptions = {}): void => {
|
|
59
|
+
const connectMongoInBeforeAll = options.connectMongoInBeforeAll ?? true;
|
|
60
|
+
|
|
61
|
+
if (options.sentryMock !== false) {
|
|
62
|
+
registerSentryBunMock();
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
let logSilencer: LogSilencer | undefined;
|
|
66
|
+
if (options.silenceLogs !== false) {
|
|
67
|
+
const silencerOptions = typeof options.silenceLogs === "object" ? options.silenceLogs : {};
|
|
68
|
+
if (connectMongoInBeforeAll) {
|
|
69
|
+
registerLogSilencing(silencerOptions);
|
|
70
|
+
} else {
|
|
71
|
+
logSilencer = createLogSilencer(silencerOptions);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (options.useTransactions) {
|
|
76
|
+
installTransactionPatches();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (!shouldDisableDb(options)) {
|
|
80
|
+
beforeAll(async () => {
|
|
81
|
+
if (connectMongoInBeforeAll) {
|
|
82
|
+
if (!isServerStarted) {
|
|
83
|
+
setTerrenoTestEnv(options.testEnv);
|
|
84
|
+
await startMongoServer(options.mongo);
|
|
85
|
+
if (options.loadModels) {
|
|
86
|
+
await options.loadModels();
|
|
87
|
+
}
|
|
88
|
+
if (options.loadTestDataFromCache) {
|
|
89
|
+
await options.loadTestDataFromCache();
|
|
90
|
+
}
|
|
91
|
+
isServerStarted = true;
|
|
92
|
+
} else {
|
|
93
|
+
await initializeModels();
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
await options.onBeforeAll?.();
|
|
97
|
+
}, 60000);
|
|
98
|
+
|
|
99
|
+
if (connectMongoInBeforeAll) {
|
|
100
|
+
afterAll(async () => {
|
|
101
|
+
await options.onAfterAll?.();
|
|
102
|
+
await stopMongoServer();
|
|
103
|
+
});
|
|
104
|
+
} else {
|
|
105
|
+
afterAll(async () => {
|
|
106
|
+
await options.onAfterAll?.();
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
beforeEach(async () => {
|
|
112
|
+
setTerrenoTestEnv(options.testEnv);
|
|
113
|
+
logSilencer?.reapply();
|
|
114
|
+
logSilencer?.clearLogs();
|
|
115
|
+
if (options.useTransactions) {
|
|
116
|
+
await startTestTransaction();
|
|
117
|
+
}
|
|
118
|
+
await options.onBeforeEach?.();
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
afterEach(async () => {
|
|
122
|
+
if (options.useTransactions) {
|
|
123
|
+
await abortTestTransaction();
|
|
124
|
+
}
|
|
125
|
+
setSystemTime();
|
|
126
|
+
mock.clearAllMocks();
|
|
127
|
+
jest.restoreAllMocks();
|
|
128
|
+
logSilencer?.clearLogs();
|
|
129
|
+
await options.onAfterEach?.();
|
|
130
|
+
setTerrenoTestEnv(options.testEnv);
|
|
131
|
+
});
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Simple Mongo preload: single memory server or external URI, no transactions.
|
|
136
|
+
* Matches the historical `@terreno/api` `bunSetup.ts` behavior.
|
|
137
|
+
*/
|
|
138
|
+
export interface SimpleMongoPreloadOptions
|
|
139
|
+
extends Omit<BackendPreloadOptions, "useTransactions" | "connectMongoInBeforeAll"> {
|
|
140
|
+
/** Fallback URI when no env override or memory server is configured. */
|
|
141
|
+
defaultLocalMongoUri?: string;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export const registerSimpleMongoPreload = (options: SimpleMongoPreloadOptions = {}): void => {
|
|
145
|
+
let memoryMongo: {getUri: () => string; stop: () => Promise<boolean>} | undefined;
|
|
146
|
+
const {defaultLocalMongoUri = "mongodb://127.0.0.1/terreno?&connectTimeoutMS=360000", ...preloadOptions} =
|
|
147
|
+
options;
|
|
148
|
+
|
|
149
|
+
registerBackendPreload({
|
|
150
|
+
...preloadOptions,
|
|
151
|
+
connectMongoInBeforeAll: false,
|
|
152
|
+
onAfterAll: async () => {
|
|
153
|
+
await mongoose.connection.close();
|
|
154
|
+
if (memoryMongo) {
|
|
155
|
+
await memoryMongo.stop();
|
|
156
|
+
}
|
|
157
|
+
await preloadOptions.onAfterAll?.();
|
|
158
|
+
},
|
|
159
|
+
onBeforeAll: async () => {
|
|
160
|
+
let uri = process.env.TERRENO_TEST_MONGODB_URI?.trim();
|
|
161
|
+
if (!uri && process.env.TERRENO_TEST_USE_MEMORY_MONGO === "true") {
|
|
162
|
+
const {MongoMemoryServer} = await import("mongodb-memory-server");
|
|
163
|
+
memoryMongo = await MongoMemoryServer.create();
|
|
164
|
+
uri = memoryMongo.getUri();
|
|
165
|
+
}
|
|
166
|
+
const connectUri = uri ?? defaultLocalMongoUri;
|
|
167
|
+
if (mongoose.connection.readyState === 0) {
|
|
168
|
+
await mongoose.connect(connectUri).catch(testLogger.catch);
|
|
169
|
+
}
|
|
170
|
+
await preloadOptions.onBeforeAll?.();
|
|
171
|
+
},
|
|
172
|
+
useTransactions: false,
|
|
173
|
+
});
|
|
174
|
+
};
|