vitest-environment-mongodb 1.0.2 → 2.0.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.
@@ -1,24 +1,31 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const mongodb_memory_server_1 = require("mongodb-memory-server");
4
- exports.default = {
1
+ import { MongoMemoryReplSet, MongoMemoryServer } from "mongodb-memory-server";
2
+ let sharedMongoServer = null;
3
+ let referenceCount = 0;
4
+ export default {
5
5
  name: "mongodb",
6
- transformMode: "ssr",
6
+ viteEnvironment: "ssr",
7
7
  async setup(_, options) {
8
- // parse out options
9
- const isReplicaSet = options?.replicaSet;
10
- const serverOptions = options?.serverOptions ?? {};
11
- const mongoUrlEnvName = options?.mongoUrlEnvName ?? "MONGO_CONNECTION_STRING";
12
- // create server
13
- const mongoServer = isReplicaSet
14
- ? await mongodb_memory_server_1.MongoMemoryServer.create(serverOptions)
15
- : await mongodb_memory_server_1.MongoMemoryReplSet.create(serverOptions);
16
- // put the connection string in the environment
17
- process.env[mongoUrlEnvName] = mongoServer.getUri();
8
+ referenceCount++;
9
+ if (!sharedMongoServer) {
10
+ // parse out options
11
+ const isReplicaSet = options?.replicaSet;
12
+ const serverOptions = options?.serverOptions ?? {};
13
+ const mongoUrlEnvName = options?.mongoUrlEnvName ?? "MONGO_CONNECTION_STRING";
14
+ // create server
15
+ sharedMongoServer = isReplicaSet
16
+ ? await MongoMemoryServer.create(serverOptions)
17
+ : await MongoMemoryReplSet.create(serverOptions);
18
+ // put the connection string in the environment
19
+ process.env[mongoUrlEnvName] = sharedMongoServer.getUri();
20
+ }
18
21
  return {
19
22
  async teardown() {
20
- // stop the server
21
- await mongoServer.stop();
23
+ referenceCount--;
24
+ // only stop the server if there are no more references to it
25
+ if (referenceCount === 0 && sharedMongoServer) {
26
+ await sharedMongoServer.stop();
27
+ sharedMongoServer = null;
28
+ }
22
29
  },
23
30
  };
24
31
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vitest-environment-mongodb",
3
- "version": "1.0.2",
3
+ "version": "2.0.0",
4
4
  "description": "Sets up a MongoDB memory-server environment for testing with Vitest.",
5
5
  "main": "dist/mongodb-environment.js",
6
6
  "scripts": {
@@ -9,6 +9,7 @@
9
9
  "prepack": "npm run build",
10
10
  "semantic-release": "semantic-release"
11
11
  },
12
+ "repository": "https://github.com/jb-1980/vitest-environment-mongodb",
12
13
  "keywords": [],
13
14
  "author": "Joseph Gilgen <2569898+jb-1980@users.noreply.github.com>",
14
15
  "license": "MIT",
@@ -19,6 +20,6 @@
19
20
  "@types/node": "^22.13.14",
20
21
  "semantic-release": "^24.2.3",
21
22
  "typescript": "^5.8.2",
22
- "vitest": "^3.0.9"
23
+ "vitest": "^4.0.16"
23
24
  }
24
25
  }