Skip to content

Commit

Permalink
feat: add new entrypoint for mongodb ReplicaSet
Browse files Browse the repository at this point in the history
  • Loading branch information
danielpza committed Oct 17, 2024
1 parent 14fda89 commit 374de60
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export default defineConfig({
// optional configuration
mongodbMemoryServer: {
// these options are passed to MongoMemoryServer.create(), see https://typegoose.github.io/mongodb-memory-server/docs/guides/quick-start-guide#normal-server
// replSet: { count: 4 }
},
},
},
Expand Down Expand Up @@ -90,7 +89,6 @@ export default defineConfig({
// optional configuration
mongodbMemoryServer: {
// these options are passed to MongoMemoryServer.create(), see https://typegoose.github.io/mongodb-memory-server/docs/guides/quick-start-guide#normal-server
// replSet: { count: 4 }
},
},
},
Expand Down Expand Up @@ -119,6 +117,28 @@ test("my test", async ({ connection }) => {

- `connection` is the `Connection` instance returned by `mongoose.createConnection`. See https://mongoosejs.com/docs/api/connection.html

## Using ReplSet

See https://typegoose.github.io/mongodb-memory-server/docs/guides/quick-start-guide#replicaset

`vitest.config.mjs`:

```js
import { defineConfig } from "vitest/config";

export default defineConfig({
test: {
globalSetup: ["vitest-mms/globalSetupReplSet"],
setupFiles: ["vitest-mms/mongodb/setupFile"],
vitestMms: {
mongodbMemoryServer: {
replSet: { count: 4 },
},
},
},
});
```

## Alternative using a extended test context

If you want to avoid the overhead of vitest-mms on every test and instead just want to use it for a subset of your tests, you can use `vitest-mms/*/test` instead:
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
"require": "./dist/globalSetup.cjs",
"import": "./dist/globalSetup.mjs"
},
"./globalSetupReplSet": {
"types": "./dist/globalSetupReplSet.d.ts",
"require": "./dist/globalSetupReplSet.cjs",
"import": "./dist/globalSetupReplSet.mjs"
},
"./mongodb/setupFile": {
"types": "./dist/mongodb/setupFile.d.ts",
"require": "./dist/mongodb/setupFile.cjs",
Expand Down
36 changes: 36 additions & 0 deletions src/globalSetupReplSet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import type { GlobalSetupContext } from "vitest/node";

import { MongoMemoryReplSet } from "mongodb-memory-server";

export type { ProvidedContext } from "vitest";
export type { ResolvedConfig } from "vitest/node";

declare module "vitest" {
export interface ProvidedContext {
MONGO_URI: string;
}
}

type MongoMemoryServerOpts = Parameters<typeof MongoMemoryReplSet.create>[0];

declare module "vitest/node" {
export interface ResolvedConfig {
vitestMms?: {
mongodbMemoryServerOptions: MongoMemoryServerOpts;
};
}
}

export default async function setup({ provide, config }: GlobalSetupContext) {
const mongod = await MongoMemoryReplSet.create(
config.vitestMms?.mongodbMemoryServerOptions,
);

const uri = mongod.getUri();

provide("MONGO_URI", uri);

return async () => {
await mongod.stop();
};
}

0 comments on commit 374de60

Please # to comment.