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
Copy file name to clipboardExpand all lines: docs/guides/integration-examples/test-runners.md
+75-3Lines changed: 75 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -135,10 +135,82 @@ describe('...', () => {
135
135
});
136
136
```
137
137
138
-
## AVA test runner
138
+
## vitest
139
139
140
-
For AVA there is a [detailed written tutorial](https://github.com/zellwk/ava/blob/8b7ccba1d80258b272ae7cae6ba4967cd1c13030/docs/recipes/endpoint-testing-with-mongoose.md) on how to test mongoose models with mongodb-memory-server by [@zellwk](https://github.com/zellwk).
140
+
<spanclass="badge badge--secondary">vitest version 3</span>
141
+
142
+
For [vitest](https://vitest.dev/), create a [global setup file](https://vitest.dev/config/#globalsetup).
exportdefaultasyncfunction setup({ provide }:TestProject) {
169
+
const mongod =awaitMongoMemoryServer.create();
170
+
171
+
const uri =mongod.getUri();
172
+
173
+
provide('MONGO_URI', uri);
174
+
175
+
returnasync () => {
176
+
awaitmongod.stop();
177
+
};
178
+
}
179
+
```
180
+
181
+
Then use it in your tests:
182
+
183
+
`example.test.js`
184
+
185
+
```ts
186
+
import { inject, test } from'vitest';
187
+
import { MongoClient } from'mongodb';
188
+
189
+
const MONGO_URI =inject('MONGO_URI');
190
+
const mongoClient =newMongoClient(MONGO_URI);
191
+
192
+
beforeAll(async () => {
193
+
awaitmongoClient.connect();
194
+
return () =>mongoClient.disconnect();
195
+
});
196
+
197
+
test('...', () => {
198
+
const db =mongoClient.db('my-db');
199
+
});
200
+
```
141
201
142
202
:::note
143
-
Note that this tutorial is pre mongodb-memory-server 7.x.
203
+
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.
144
204
:::
205
+
206
+
See also [vitest-mms](https://github.com/danielpza/vitest-mms), which provides the `globalSetup` configuration among others helpers:
0 commit comments