You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
exportdefaultasyncfunction setup({ provide }:TestProject) {
177
+
const mongod =awaitMongoMemoryServer.create();
178
+
179
+
const uri =mongod.getUri();
180
+
181
+
provide('MONGO_URI', uri);
182
+
183
+
returnasync () => {
184
+
awaitmongod.stop();
185
+
};
186
+
}
187
+
```
188
+
189
+
Then use it in your tests:
190
+
191
+
`example.test.js`
192
+
193
+
```ts
194
+
import { inject, test } from'vitest';
195
+
import { MongoClient } from'mongodb';
196
+
197
+
const MONGO_URI =inject('MONGO_URI');
198
+
const mongoClient =newMongoClient(MONGO_URI);
199
+
200
+
beforeAll(async () => {
201
+
awaitmongoClient.connect();
202
+
return () =>mongoClient.disconnect();
203
+
});
204
+
205
+
test('...', () => {
206
+
const db =mongoClient.db('my-db');
207
+
});
208
+
```
209
+
210
+
:::note
211
+
Keep in mind that the global setup is running in a different global scope, so your tests don't have access to variables defined here. However, you can pass down serializable data to tests via [provide](https://vitest.dev/config/#provide) method as described above.
212
+
:::
213
+
214
+
See also [vitest-mms](https://github.com/danielpza/vitest-mms), which provides the `globalSetup` configuration among others helpers:
0 commit comments