forked from Coding-Coach/coding-coach-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetupJest.js
More file actions
34 lines (29 loc) · 933 Bytes
/
setupJest.js
File metadata and controls
34 lines (29 loc) · 933 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { GraphQLClient } from 'graphql-request';
import mongoose from 'mongoose';
global.client = new GraphQLClient(`http://localhost:${process.env.PORT || 3000}/graphql`);
function clearDatabase() {
return new Promise((resolve, reject) => {
mongoose.connection.db.listCollections().toArray((err, collections) => {
if (err) reject(err);
if (collections.length === 0) resolve();
collections.forEach(({ name }, i) => {
mongoose.connection.db.dropCollection(name, (_err) => {
if (_err) reject(_err);
if (i === collections.length - 1) resolve();
});
});
});
});
}
beforeAll((done) => {
mongoose.connect('mongodb://localhost/test', { useNewUrlParser: true });
mongoose.connection.once('open', done);
});
beforeEach(async (done) => {
await clearDatabase();
done();
});
afterAll(async (done) => {
await clearDatabase();
mongoose.disconnect(done);
});