vitest-environment-mongodb 1.0.3 → 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.
- package/dist/mongodb-environment.js +24 -17
- package/package.json +2 -2
|
@@ -1,24 +1,31 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { MongoMemoryReplSet, MongoMemoryServer } from "mongodb-memory-server";
|
|
2
|
+
let sharedMongoServer = null;
|
|
3
|
+
let referenceCount = 0;
|
|
4
|
+
export default {
|
|
5
5
|
name: "mongodb",
|
|
6
|
-
|
|
6
|
+
viteEnvironment: "ssr",
|
|
7
7
|
async setup(_, options) {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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
|
-
|
|
21
|
-
|
|
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": "
|
|
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": {
|
|
@@ -20,6 +20,6 @@
|
|
|
20
20
|
"@types/node": "^22.13.14",
|
|
21
21
|
"semantic-release": "^24.2.3",
|
|
22
22
|
"typescript": "^5.8.2",
|
|
23
|
-
"vitest": "^
|
|
23
|
+
"vitest": "^4.0.16"
|
|
24
24
|
}
|
|
25
25
|
}
|