-
-
Notifications
You must be signed in to change notification settings - Fork 183
docs(guides): add vitest integration example #937
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -142,3 +142,66 @@ For AVA there is a [detailed written tutorial](https://github.com/zellwk/ava/blo | |||||||
:::note | ||||||||
Note that this tutorial is pre mongodb-memory-server 7.x. | ||||||||
::: | ||||||||
|
||||||||
## vitest | ||||||||
|
||||||||
For [vitest](https://vitest.dev/), create a [global setup file](https://vitest.dev/config/#globalsetup). | ||||||||
|
||||||||
`vitest.config.mts`: | ||||||||
|
||||||||
```ts | ||||||||
import { defineConfig } from 'vitest/config'; | ||||||||
|
||||||||
export default defineConfig({ | ||||||||
test: { | ||||||||
globalSetup: ['./globalSetup.ts'], | ||||||||
}, | ||||||||
}); | ||||||||
``` | ||||||||
|
||||||||
`globalSetup.ts`: | ||||||||
|
||||||||
```ts | ||||||||
import type { TestProject } from 'vitest/node'; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Import for |
||||||||
|
||||||||
declare module 'vitest' { | ||||||||
export interface ProvidedContext { | ||||||||
MONGO_URI: string; | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
export default async function setup({ provide }: TestProject) { | ||||||||
const mongod = await MongoMemoryServer.create(); | ||||||||
|
||||||||
const uri = mongod.getUri(); | ||||||||
|
||||||||
provide('MONGO_URI', uri); | ||||||||
|
||||||||
return async () => { | ||||||||
await mongod.stop(); | ||||||||
}; | ||||||||
} | ||||||||
``` | ||||||||
|
||||||||
Then use it in your tests: | ||||||||
|
||||||||
`example.test.js` | ||||||||
|
||||||||
```ts | ||||||||
import { inject, test } from 'vitest'; | ||||||||
import { MongoClient } from 'mongodb'; | ||||||||
|
||||||||
const MONGO_URI = inject('MONGO_URI'); | ||||||||
const mongoClient = new MongoClient(MONGO_URI); | ||||||||
|
||||||||
beforeAll(async () => { | ||||||||
await mongoClient.connect(); | ||||||||
return () => mongoClient.disconnect(); | ||||||||
}); | ||||||||
|
||||||||
test('...', () => { | ||||||||
const db = mongoClient.db('my-db'); | ||||||||
}); | ||||||||
``` | ||||||||
|
||||||||
See also [vitest-mms](https://github.com/danielpza/vitest-mms) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of just a simple There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a note similar to jests mongodb-memory-server/docs/guides/integration-examples/test-runners.md Lines 102 to 104 in e4992fd
re:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a badge similar to jest and mocha:
mongodb-memory-server/docs/guides/integration-examples/test-runners.md
Line 10 in e4992fd