vitest-environment-mongodb 1.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/.github/workflows/release.yml +36 -0
- package/README.md +74 -0
- package/dist/mongodb-environment.d.ts +3 -0
- package/dist/mongodb-environment.js +25 -0
- package/package.json +24 -0
- package/src/mongodb-environment.ts +45 -0
- package/tsconfig.json +12 -0
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
name: Release
|
|
2
|
+
on:
|
|
3
|
+
push:
|
|
4
|
+
branches:
|
|
5
|
+
- main
|
|
6
|
+
|
|
7
|
+
permissions:
|
|
8
|
+
contents: read # for checkout
|
|
9
|
+
|
|
10
|
+
jobs:
|
|
11
|
+
release:
|
|
12
|
+
name: Release
|
|
13
|
+
runs-on: ubuntu-latest
|
|
14
|
+
permissions:
|
|
15
|
+
contents: write # to be able to publish a GitHub release
|
|
16
|
+
issues: write # to be able to comment on released issues
|
|
17
|
+
pull-requests: write # to be able to comment on released pull requests
|
|
18
|
+
id-token: write # to enable use of OIDC for npm provenance
|
|
19
|
+
steps:
|
|
20
|
+
- name: Checkout
|
|
21
|
+
uses: actions/checkout@v4
|
|
22
|
+
with:
|
|
23
|
+
fetch-depth: 0
|
|
24
|
+
- name: Setup Node.js
|
|
25
|
+
uses: actions/setup-node@v4
|
|
26
|
+
with:
|
|
27
|
+
node-version: "lts/*"
|
|
28
|
+
- name: Install dependencies
|
|
29
|
+
run: npm clean-install
|
|
30
|
+
- name: Verify the integrity of provenance attestations and registry signatures for installed dependencies
|
|
31
|
+
run: npm audit signatures
|
|
32
|
+
- name: Release
|
|
33
|
+
env:
|
|
34
|
+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
35
|
+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
|
|
36
|
+
run: npx semantic-release
|
package/README.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# vitest-environment-mongodb
|
|
2
|
+
|
|
3
|
+
Setup a MongoDB environment for Vitest tests using mongodb-memory-server.
|
|
4
|
+
|
|
5
|
+
This package provides a Vitest environment that uses `mongodb-memory-server` to create an in-memory MongoDB instance for testing purposes. It is designed to be used with Vitest, a Vite-native test framework.
|
|
6
|
+
|
|
7
|
+
It will spin up a memory server instance, and set the connection string on
|
|
8
|
+
`process.env.MONGO_CONNECTION_STRING` by default, or value you can configure
|
|
9
|
+
with `mongoUrlEnvName` option.
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
Use your favorite package manager to install the package:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install --save-dev vitest-environment-mongodb
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
or
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
yarn add --dev vitest-environment-mongodb
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
or
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pnpm add --save-dev vitest-environment-mongodb
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Usage
|
|
32
|
+
|
|
33
|
+
Once installed, you can use the environment in your Vitest configuration per the
|
|
34
|
+
[documentation](https://vitest.dev/guide/environment.html).
|
|
35
|
+
|
|
36
|
+
### Use in Vitest config
|
|
37
|
+
|
|
38
|
+
```js
|
|
39
|
+
import { defineConfig } from "vitest/config"
|
|
40
|
+
|
|
41
|
+
export default defineConfig({
|
|
42
|
+
test: {
|
|
43
|
+
environment: "mongodb",
|
|
44
|
+
environmentOptions: {
|
|
45
|
+
mongoUrlEnvName: "MONGO_CONNECTION_STRING", // optional, default is MONGO_CONNECTION_STRING
|
|
46
|
+
replicaSet: false, // optional, default is false
|
|
47
|
+
serverOptions: MongoMemoryServerOpts | MongoMemoryReplSetOpts, //optional, server options for the chosen server type
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
})
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### Use control comment in a test file
|
|
54
|
+
|
|
55
|
+
```js
|
|
56
|
+
// @vitest-environment mongodb
|
|
57
|
+
import { expect, test } from "vitest"
|
|
58
|
+
import { UserCollection } from "./models/UserCollection"
|
|
59
|
+
|
|
60
|
+
test("should create a user", async () => {
|
|
61
|
+
const { insertedId } = await UserCollection.insertOne({ name: "John Doe" })
|
|
62
|
+
const user = await UserCollection.findOne({ _id: insertedId })
|
|
63
|
+
expect(user).toBeDefined()
|
|
64
|
+
expect(user).toHaveProperty("name")
|
|
65
|
+
expect(user).toHaveProperty("_id")
|
|
66
|
+
expect(user.name).toBe("John Doe")
|
|
67
|
+
})
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Options
|
|
71
|
+
|
|
72
|
+
- `mongoUrlEnvName` (string): The name of the environment variable that will contain the MongoDB connection string. Default is `MONGO_CONNECTION_STRING`.
|
|
73
|
+
- `replicaSet` (boolean): Whether to use a replica set. Default is `false`.
|
|
74
|
+
And any other options that are supported by `mongodb-memory-server` package.
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const mongodb_memory_server_1 = require("mongodb-memory-server");
|
|
4
|
+
exports.default = {
|
|
5
|
+
name: "mongodb",
|
|
6
|
+
transformMode: "ssr",
|
|
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();
|
|
18
|
+
return {
|
|
19
|
+
async teardown() {
|
|
20
|
+
// stop the server
|
|
21
|
+
await mongoServer.stop();
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
},
|
|
25
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "vitest-environment-mongodb",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Sets up a MongoDB memory-server environment for testing with Vitest.",
|
|
5
|
+
"main": "dist/mongodb-environment.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build": "tsc",
|
|
8
|
+
"build:watch": "tsc --watch",
|
|
9
|
+
"prepack": "npm run build",
|
|
10
|
+
"semantic-release": "semantic-release"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [],
|
|
13
|
+
"author": "Joseph Gilgen <2569898+jb-1980@users.noreply.github.com>",
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"mongodb-memory-server": "^10.1.4"
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@types/node": "^22.13.14",
|
|
20
|
+
"semantic-release": "^24.2.3",
|
|
21
|
+
"typescript": "^5.8.2",
|
|
22
|
+
"vitest": "^3.0.9"
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { Environment } from "vitest/environments"
|
|
2
|
+
import { MongoMemoryReplSet, MongoMemoryServer } from "mongodb-memory-server"
|
|
3
|
+
|
|
4
|
+
type Options =
|
|
5
|
+
| {
|
|
6
|
+
replicaSet?: false
|
|
7
|
+
mongoUrlEnvName?: string
|
|
8
|
+
serverOptions?: NonNullable<
|
|
9
|
+
Parameters<(typeof MongoMemoryServer)["create"]>[0]
|
|
10
|
+
>
|
|
11
|
+
}
|
|
12
|
+
| {
|
|
13
|
+
replicaSet: true
|
|
14
|
+
mongoUrlEnvName?: string
|
|
15
|
+
serverOptions?: NonNullable<
|
|
16
|
+
Parameters<(typeof MongoMemoryReplSet)["create"]>[0]
|
|
17
|
+
>
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export default <Environment>{
|
|
21
|
+
name: "mongodb",
|
|
22
|
+
transformMode: "ssr",
|
|
23
|
+
async setup(_, options?: Options) {
|
|
24
|
+
// parse out options
|
|
25
|
+
const isReplicaSet = options?.replicaSet
|
|
26
|
+
const serverOptions = options?.serverOptions ?? {}
|
|
27
|
+
const mongoUrlEnvName =
|
|
28
|
+
options?.mongoUrlEnvName ?? "MONGO_CONNECTION_STRING"
|
|
29
|
+
|
|
30
|
+
// create server
|
|
31
|
+
const mongoServer = isReplicaSet
|
|
32
|
+
? await MongoMemoryServer.create(serverOptions)
|
|
33
|
+
: await MongoMemoryReplSet.create(serverOptions)
|
|
34
|
+
|
|
35
|
+
// put the connection string in the environment
|
|
36
|
+
process.env[mongoUrlEnvName] = mongoServer.getUri()
|
|
37
|
+
|
|
38
|
+
return {
|
|
39
|
+
async teardown() {
|
|
40
|
+
// stop the server
|
|
41
|
+
await mongoServer.stop()
|
|
42
|
+
},
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
}
|