@jongodb/memory-server 0.1.1 → 0.1.3

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/README.md CHANGED
@@ -1,68 +1,38 @@
1
1
  # @jongodb/memory-server
2
2
 
3
- Node test runtime adapter for `jongodb`.
3
+ Node test runtime adapter for [`jongodb`](https://github.com/midagedev/jongodb).
4
4
 
5
- It starts a local `jongodb` launcher process, exposes a MongoDB URI, and manages process lifecycle for test runners.
5
+ It starts a local `jongodb` launcher process, provides a MongoDB URI, and manages lifecycle for test runners.
6
+
7
+ ## Why This Package Exists
8
+
9
+ `mongodb-memory-server` is useful, but it still depends on `mongod` bootstrap behavior.
10
+
11
+ `@jongodb/memory-server` focuses on a different tradeoff:
12
+ - faster integration-test startup loops
13
+ - deterministic local process lifecycle control
14
+ - simple test runner integration (Jest, Vitest, Nest Jest)
15
+
16
+ This package is for test runtime usage only. It is not a production MongoDB server.
17
+
18
+ ## Value for Integration Test Suites
19
+
20
+ Observed benchmark summary on one NestJS integration suite shape:
21
+ - historical suite-scoped lifecycle: about `55.9%` faster than `mongodb-memory-server`
22
+ - current single-boot lifecycle: about `64.2%` faster than `mongodb-memory-server`
23
+
24
+ Actual deltas vary by test shape, dataset size, and CI machine characteristics.
6
25
 
7
26
  ## Scope
8
27
 
9
- - integration-test runtime only
10
- - launcher lifecycle management (start/stop/detach)
11
- - framework-agnostic base helper
12
- - Jest/Vitest/Nest Jest convenience helpers
13
-
14
- This package is not a production MongoDB server.
15
-
16
- ## Why This vs Embedded MongoDB (`mongodb-memory-server`)
17
-
18
- `@jongodb/memory-server` can run in native binary mode instead of booting a full `mongod` test binary.
19
-
20
- Practical effects:
21
-
22
- - no `mongod` binary download/extract step in cold environments
23
- - lower launcher startup overhead in many integration-test loops
24
- - fewer environment issues tied to `mongod` binary provisioning
25
-
26
- Rough performance expectation (not a guarantee):
27
-
28
- - cold CI/dev machine: often much faster because there is no `mongod` artifact fetch/unpack
29
- - warm cache environment: startup-phase wins are usually smaller
30
-
31
- Observed benchmarks (NestJS integration suite subset, same machine):
32
-
33
- - Benchmark A: suite-scoped lifecycle (historical)
34
- - workload: `9 suites / 102 tests` (`--runInBand`)
35
- - server boots: `9` times per run (once per suite)
36
- - `mongodb-memory-server` real:
37
- - run1: `28.17s`
38
- - run2: `27.19s`
39
- - average: `27.68s`
40
- - `jongodb` native real:
41
- - run1: `12.35s`
42
- - run2: `12.04s`
43
- - average: `12.20s`
44
- - delta: about `55.9%` faster (`15.48s` less wall clock)
45
- - Benchmark B: single-boot lifecycle (current)
46
- - workload: `45 suites / 510 tests` (`--runInBand`)
47
- - server boots: `1` time per run (shared for full Jest process)
48
- - `mongodb-memory-server` real:
49
- - run1: `92.40s`
50
- - run2: `91.84s`
51
- - average: `92.12s`
52
- - `jongodb` native real:
53
- - run1: `33.03s`
54
- - run2: `32.90s`
55
- - average: `32.97s`
56
- - delta: about `64.2%` faster (`59.15s` less wall clock)
57
-
58
- Your actual delta depends on test shape, I/O, and query workload.
28
+ - integration-test runtime helper
29
+ - launcher process start/stop/detach lifecycle
30
+ - framework-agnostic runtime API
31
+ - convenience hooks for Jest, Vitest, and Nest Jest
59
32
 
60
33
  ## Requirements
61
34
 
62
35
  - Node.js 20+
63
- - one launcher runtime:
64
- - native binary, or
65
- - Java 17+ with `jongodb` classpath
66
36
 
67
37
  ## Install
68
38
 
@@ -70,16 +40,24 @@ Your actual delta depends on test shape, I/O, and query workload.
70
40
  npm i -D @jongodb/memory-server
71
41
  ```
72
42
 
73
- ## Quick Start (Recommended)
43
+ ## Module Format
44
+
45
+ This package supports both:
46
+ - ESM (`import`)
47
+ - CommonJS (`require`)
74
48
 
75
- Use `createJongodbEnvRuntime` and keep one runtime per test process.
49
+ ## Quick Start
50
+
51
+ Recommended pattern: one runtime per test process.
76
52
 
77
53
  ```ts
78
54
  import { beforeAll, afterAll } from "@jest/globals";
79
55
  import { createJongodbEnvRuntime } from "@jongodb/memory-server/runtime";
80
56
 
81
57
  const runtime = createJongodbEnvRuntime({
82
- databaseName: "test"
58
+ databaseName: "test",
59
+ databaseNameStrategy: "worker",
60
+ envVarNames: ["MONGODB_URI", "DATABASE_URL"],
83
61
  });
84
62
 
85
63
  beforeAll(async () => {
@@ -91,77 +69,67 @@ afterAll(async () => {
91
69
  });
92
70
  ```
93
71
 
94
- Behavior:
95
-
96
- - `setup()` starts launcher and writes `process.env.MONGODB_URI` (default)
97
- - `teardown()` stops launcher and restores previous env value
98
-
99
- ## Launcher Modes
100
-
101
- `launchMode` values:
102
-
103
- - `auto` (default): binary first, Java fallback
104
- - `binary`: binary only, fail fast if binary not resolvable
105
- - `java`: Java only, fail fast if classpath not configured
106
-
107
- In `auto` mode, launch failures are attempted in order and reported with source tags.
72
+ Runtime behavior:
73
+ - `setup()` starts launcher and writes URI to configured env key(s)
74
+ - `teardown()` stops launcher and restores previous env values
108
75
 
109
- ## Binary Runtime
76
+ CommonJS example:
110
77
 
111
- Binary resolution order:
112
-
113
- 1. `options.binaryPath`
114
- 2. `JONGODB_BINARY_PATH`
115
- 3. bundled platform package (if installed)
78
+ ```js
79
+ const { createJongodbEnvRuntime } = require("@jongodb/memory-server/runtime");
80
+ const runtime = createJongodbEnvRuntime({ databaseName: "test" });
81
+ ```
116
82
 
117
- Current bundled package targets:
83
+ ## Migration from `mongodb-memory-server`
118
84
 
119
- - `@jongodb/memory-server-bin-darwin-arm64`
120
- - `@jongodb/memory-server-bin-linux-x64-gnu`
121
- - `@jongodb/memory-server-bin-win32-x64`
85
+ Basic Jest migration:
122
86
 
123
- Version policy:
87
+ Before:
124
88
 
125
- - binary package versions are pinned to the core package version (`optionalDependencies`)
126
- - if a bundled binary is not installed/available for your version, `auto` mode can still run with Java fallback
89
+ ```ts
90
+ import { MongoMemoryServer } from "mongodb-memory-server";
127
91
 
128
- Binary-only example:
92
+ let mongod: MongoMemoryServer;
129
93
 
130
- ```ts
131
- import { startJongodbMemoryServer } from "@jongodb/memory-server";
94
+ beforeAll(async () => {
95
+ mongod = await MongoMemoryServer.create();
96
+ process.env.MONGODB_URI = mongod.getUri();
97
+ });
132
98
 
133
- const server = await startJongodbMemoryServer({
134
- launchMode: "binary",
135
- binaryPath: process.env.JONGODB_BINARY_PATH
99
+ afterAll(async () => {
100
+ await mongod.stop();
136
101
  });
137
102
  ```
138
103
 
139
- ## Java Runtime
104
+ After:
140
105
 
141
- Provide classpath by:
106
+ ```ts
107
+ import { beforeAll, afterAll } from "@jest/globals";
108
+ import { createJongodbEnvRuntime } from "@jongodb/memory-server/runtime";
142
109
 
143
- 1. `startJongodbMemoryServer({ classpath: "..." })`, or
144
- 2. `JONGODB_CLASSPATH`
110
+ const runtime = createJongodbEnvRuntime({ databaseName: "test" });
145
111
 
146
- Repository helper command (when using this repo launcher build):
112
+ beforeAll(async () => {
113
+ await runtime.setup();
114
+ });
147
115
 
148
- ```bash
149
- ./.tooling/gradle-8.10.2/bin/gradle -q printLauncherClasspath
116
+ afterAll(async () => {
117
+ await runtime.teardown();
118
+ });
150
119
  ```
151
120
 
152
- ## Standalone Launcher Contract
153
-
154
- Binary and Java launchers follow one contract:
121
+ Option mapping:
122
+ - `MongoMemoryServer.create({ instance: { port } })` -> `createJongodbEnvRuntime({ port })`
123
+ - `MongoMemoryServer.create({ instance: { dbName } })` -> `createJongodbEnvRuntime({ databaseName })`
124
+ - `mongod.getUri()` -> `runtime.uri` (after `setup`) or configured env var
125
+ - `mongod.stop()` -> `runtime.teardown()`
155
126
 
156
- - readiness line on stdout: `JONGODB_URI=mongodb://...`
157
- - optional failure line on stderr: `JONGODB_START_FAILURE=...`
158
- - process remains alive until terminated by signal (`SIGTERM`/`SIGINT`)
159
-
160
- The adapter relies on this contract for deterministic startup/teardown handling.
127
+ Parallel test tip:
128
+ - set `databaseNameStrategy: "worker"` to isolate worker data by database name
161
129
 
162
- ## Runner Helpers
130
+ ## Runner Integrations
163
131
 
164
- ### Jest (per-file hooks)
132
+ Jest (per-file hooks):
165
133
 
166
134
  ```ts
167
135
  import { beforeAll, afterAll } from "@jest/globals";
@@ -170,9 +138,9 @@ import { registerJongodbForJest } from "@jongodb/memory-server/jest";
170
138
  registerJongodbForJest({ beforeAll, afterAll });
171
139
  ```
172
140
 
173
- ### Jest (global setup/teardown)
141
+ Jest (global setup/teardown):
174
142
 
175
- `jest.global-setup.ts`:
143
+ `jest.global-setup.ts`
176
144
 
177
145
  ```ts
178
146
  import { createJestGlobalSetup } from "@jongodb/memory-server/jest";
@@ -180,7 +148,7 @@ import { createJestGlobalSetup } from "@jongodb/memory-server/jest";
180
148
  export default createJestGlobalSetup();
181
149
  ```
182
150
 
183
- `jest.global-teardown.ts`:
151
+ `jest.global-teardown.ts`
184
152
 
185
153
  ```ts
186
154
  import { createJestGlobalTeardown } from "@jongodb/memory-server/jest";
@@ -188,7 +156,7 @@ import { createJestGlobalTeardown } from "@jongodb/memory-server/jest";
188
156
  export default createJestGlobalTeardown();
189
157
  ```
190
158
 
191
- ### Vitest
159
+ Vitest:
192
160
 
193
161
  ```ts
194
162
  import { beforeAll, afterAll } from "vitest";
@@ -197,7 +165,7 @@ import { registerJongodbForVitest } from "@jongodb/memory-server/vitest";
197
165
  registerJongodbForVitest({ beforeAll, afterAll });
198
166
  ```
199
167
 
200
- ### NestJS (Jest E2E)
168
+ NestJS (Jest E2E):
201
169
 
202
170
  ```ts
203
171
  import { beforeAll, afterAll } from "@jest/globals";
@@ -206,25 +174,45 @@ import { registerJongodbForNestJest } from "@jongodb/memory-server/nestjs";
206
174
  registerJongodbForNestJest({ beforeAll, afterAll });
207
175
  ```
208
176
 
209
- ## Common Integration Patterns
177
+ Common patterns:
178
+ - NestJS + Mongoose: use `process.env.MONGODB_URI` in `forRootAsync`
179
+ - Prisma (Mongo): set runtime `envVarNames` to include `DATABASE_URL`
180
+ - TypeORM (Mongo): pass runtime URI into `url`
210
181
 
211
- - NestJS + Mongoose: `MongooseModule.forRootAsync({ useFactory: () => ({ uri: process.env.MONGODB_URI }) })`
212
- - Express/Fastify: inject `process.env.MONGODB_URI` into DB bootstrap before app init
213
- - Prisma (Mongo): set `envVarName: "DATABASE_URL"` for runtime helper
214
- - TypeORM (Mongo): pass runtime URI as `url`
182
+ ## Runtime Resolution
183
+
184
+ `launchMode` values:
185
+ - `auto` (default): binary first, Java fallback
186
+ - `binary`: binary only
187
+ - `java`: Java only
188
+
189
+ Binary resolution order:
190
+ 1. `options.binaryPath`
191
+ 2. `JONGODB_BINARY_PATH`
192
+ 3. bundled platform binary package
193
+
194
+ Bundled targets:
195
+ - `@jongodb/memory-server-bin-darwin-arm64`
196
+ - `@jongodb/memory-server-bin-linux-x64-gnu`
197
+ - `@jongodb/memory-server-bin-win32-x64`
198
+
199
+ Java fallback:
200
+ - set `classpath` option or `JONGODB_CLASSPATH`
201
+
202
+ Launcher contract:
203
+ - stdout ready line: `JONGODB_URI=mongodb://...`
204
+ - optional stderr failure line: `JONGODB_START_FAILURE=...`
205
+ - process stays alive until termination signal
215
206
 
216
207
  ## API Surface
217
208
 
218
209
  Main export (`@jongodb/memory-server`):
219
-
220
210
  - `startJongodbMemoryServer(options?)`
221
211
 
222
212
  Runtime export (`@jongodb/memory-server/runtime`):
223
-
224
213
  - `createJongodbEnvRuntime(options?)`
225
214
 
226
215
  Jest export (`@jongodb/memory-server/jest`):
227
-
228
216
  - `registerJongodbForJest(hooks, options?)`
229
217
  - `createJestGlobalSetup(options?)`
230
218
  - `createJestGlobalTeardown(options?)`
@@ -232,39 +220,40 @@ Jest export (`@jongodb/memory-server/jest`):
232
220
  - `readJestGlobalUri(options?)`
233
221
 
234
222
  Nest export (`@jongodb/memory-server/nestjs`):
235
-
236
223
  - `registerJongodbForNestJest(hooks, options?)`
237
224
 
238
225
  Vitest export (`@jongodb/memory-server/vitest`):
239
-
240
226
  - `registerJongodbForVitest(hooks, options?)`
241
227
 
242
228
  ## Options Reference
243
229
 
244
- Core options:
245
-
230
+ Core:
246
231
  - `launchMode`: `auto` | `binary` | `java` (default: `auto`)
247
- - `binaryPath`: binary executable path
232
+ - `binaryPath`: binary executable override path
248
233
  - `classpath`: Java classpath string or string array
249
234
  - `javaPath`: Java executable path (default: `java`)
250
235
  - `launcherClass`: Java launcher class (default: `org.jongodb.server.TcpMongoServerLauncher`)
251
- - `databaseName`: default DB in generated URI (default: `test`)
236
+ - `databaseName`: base DB name (default: `test`)
237
+ - `databaseNameSuffix`: suffix appended to `databaseName` (example: `_ci`)
238
+ - `databaseNameStrategy`: `static` | `worker` (default: `static`)
252
239
  - `host`: bind host (default: `127.0.0.1`)
253
- - `port`: bind port (`0` for ephemeral, default: `0`)
240
+ - `port`: bind port (`0` means ephemeral)
254
241
  - `startupTimeoutMs`: startup timeout (default: `15000`)
255
242
  - `stopTimeoutMs`: stop timeout before forced kill (default: `5000`)
256
- - `env`: additional child process env vars
243
+ - `env`: extra child process environment variables
257
244
  - `logLevel`: `silent` | `info` | `debug` (default: `silent`)
258
- - `envVarName` (runtime/jest/vitest helpers): target env key (default: `MONGODB_URI`)
245
+
246
+ Runtime helper options:
247
+ - `envVarName`: single target env key (default: `MONGODB_URI`)
248
+ - `envVarNames`: multiple target env keys (example: `["MONGODB_URI", "DATABASE_URL"]`)
259
249
 
260
250
  ## Troubleshooting
261
251
 
262
- - `No launcher runtime configured`: set binary path/env or classpath/env
252
+ - `No launcher runtime configured`: set `binaryPath` / `JONGODB_BINARY_PATH` / `classpath` / `JONGODB_CLASSPATH`
263
253
  - `Binary launch mode requested but no binary was found`: provide `binaryPath` or `JONGODB_BINARY_PATH`
264
254
  - `Java launch mode requested but Java classpath is not configured`: provide `classpath` or `JONGODB_CLASSPATH`
265
- - `spawn ... ENOENT`: missing binary/java executable path
255
+ - `spawn ... ENOENT`: missing runtime executable path
266
256
  - startup timeout: launcher did not emit `JONGODB_URI=...`
267
257
 
268
258
  Parallel test tip:
269
-
270
- - use unique `databaseName` per worker/process to avoid data collisions
259
+ - use `databaseNameStrategy: "worker"` or per-worker suffixes to prevent data collisions