File tree Expand file tree Collapse file tree 1 file changed +63
-0
lines changed
docs/guides/integration-examples Expand file tree Collapse file tree 1 file changed +63
-0
lines changed 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
142
142
::: note
143
143
Note that this tutorial is pre mongodb-memory-server 7.x.
144
144
:::
145
+
146
+ ## vitest
147
+
148
+ For [ vitest] ( https://vitest.dev/ ) , create a [ global setup file] ( https://vitest.dev/config/#globalsetup ) .
149
+
150
+ ` vitest.config.mts ` :
151
+
152
+ ``` ts
153
+ import { defineConfig } from ' vitest/config' ;
154
+
155
+ export default defineConfig ({
156
+ test: {
157
+ globalSetup: [' ./globalSetup.ts' ],
158
+ },
159
+ });
160
+ ```
161
+
162
+ ` globalSetup.ts ` :
163
+
164
+ ``` ts
165
+ import type { TestProject } from ' vitest/node' ;
166
+
167
+ declare module ' vitest' {
168
+ export interface ProvidedContext {
169
+ MONGO_URI: string ;
170
+ }
171
+ }
172
+
173
+ export default async function setup({ provide }: TestProject ) {
174
+ const mongod = await MongoMemoryServer .create ();
175
+
176
+ const uri = mongod .getUri ();
177
+
178
+ provide (' MONGO_URI' , uri );
179
+
180
+ return async () => {
181
+ await mongod .stop ();
182
+ };
183
+ }
184
+ ```
185
+
186
+ Then use it in your tests:
187
+
188
+ ` example.test.js `
189
+
190
+ ``` ts
191
+ import { inject , test } from ' vitest' ;
192
+ import { MongoClient } from ' mongodb' ;
193
+
194
+ const MONGO_URI = inject (' MONGO_URI' );
195
+ const mongoClient = new MongoClient (MONGO_URI );
196
+
197
+ beforeAll (async () => {
198
+ await mongoClient .connect ();
199
+ return () => mongoClient .disconnect ();
200
+ });
201
+
202
+ test (' ...' , () => {
203
+ const db = mongoClient .db (' my-db' );
204
+ });
205
+ ```
206
+
207
+ See also [ vitest-mms] ( https://github.com/danielpza/vitest-mms )
You can’t perform that action at this time.
0 commit comments