Skip to content

Commit

Permalink
docs: update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
danielpza committed Sep 7, 2024
1 parent 05fa1f4 commit cd58774
Showing 1 changed file with 43 additions and 54 deletions.
97 changes: 43 additions & 54 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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";
Expand All @@ -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:

Expand Down Expand Up @@ -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);
});
```

0 comments on commit cd58774

Please sign in to comment.