diff --git a/README.md b/README.md index 955b80b..d0b387d 100644 --- a/README.md +++ b/README.md @@ -10,26 +10,13 @@ yarn add -D vitest-mms mongodb-memory-server pnpm add -D vitest-mms mongodb-memory-server ``` -## Usage +## Usage with mongodb -Setup: - -Add `vitest-mms/globalSetup` to globalSetup in your vitest config - -```js -// vitest.config.mjs -import { defineConfig } from "vitest/config"; - -export default defineConfig({ - test: { - globalSetup: ["vitest-mms/globalSetup"], - }, -}); -``` +NOTE: You need to install `mongodb` separately. -### Extending the global context +Setup: -To make it available in the global context for every test you also need to add the `vitest-mms/setupFile` to your vitest config +To make it available in the global context for every test you need to add a globalSetup and setupFile in your vitest config: ```js // vitest.config.mjs @@ -43,8 +30,6 @@ export default defineConfig({ }); ``` -This will make it available in the tests context globally - ```js // index.test.js import { test } from "vitest"; @@ -70,7 +55,45 @@ For typescript support add the following to your tsconfig.json } ``` -### Alternative extending locally with test.extend +## Usage with mongoose + +NOTE: You need to install `mongoose` separately. + +```js +// vitest.config.mjs +import { defineConfig } from "vitest/config"; + +export default defineConfig({ + test: { + globalSetup: ["vitest-mms/globalSetup"], + setupFile: ["vitest-mms/mongoose/setupFile"], + }, +}); +``` + +```json +// tsconfig.json +{ + "compilerOptions": { + "types": ["vitest-mms/mongoose/setupFile"] + } +} +``` + +```js +// index.test.js +test("my test", async ({ mongoose }) => { + mongoose.connection.db; // use db + + const User = mongoose.model("User", new mongoose.Schema({ name: String })); + await User.create({ name: "John" }); + expect(await User.countDocuments()).toBe(1); +}); +``` + +- `mongoose` is the mongoose instance returned by `mongoose.connect` + +## Alternative using extended test context vitest.config.mjs: @@ -129,37 +152,3 @@ test("my test", async ({ db, mongoClient }) => { expect(await users.countDocuments()).toBe(1); }); ``` - -## Usage with mongoose - -```js -// vitest.config.mjs -import { defineConfig } from "vitest/config"; - -export default defineConfig({ - test: { - globalSetup: ["vitest-mms/globalSetup"], - setupFile: ["vitest-mms/mongoose/setupFile"], - }, -}); -``` - -```json -// tsconfig.json -{ - "compilerOptions": { - "types": ["vitest-mms/mongoose/setupFile"] - } -} -``` - -```js -// index.test.js -test("my test", async ({ mongoose }) => { - mongoose.connection.db; // use db - - const User = mongoose.model("User", new mongoose.Schema({ name: String })); - await User.create({ name: "John" }); - expect(await User.countDocuments()).toBe(1); -}); -```