-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.spec.js
59 lines (47 loc) · 1.62 KB
/
test.spec.js
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
require('mocha');
const chai = require('chai');
const { TestWithMongo } = require('./dist');
const MongoClient = require('mongodb').MongoClient;
const { expect } = chai;
describe('test with mongo tests!', () => {
const testDoc = { val: 1 };
const insertDoc = () => collection.insert(testDoc);
const getDoc = () => collection.findOne(testDoc);
const testWithMongo = new TestWithMongo(26016);
const DB_NAME = 'testDb';
let db;
let collection
before(() => {
return testWithMongo.startMongoServer();
});
after(() => {
return testWithMongo.clean();
});
beforeEach(() => {
const connectionString = testWithMongo.getConnectionString(DB_NAME);
return MongoClient.connect(connectionString)
.then((retDb) => db = retDb)
.then(() => db.createCollection('testCollection'))
.then((retCollection) => collection = retCollection)
});
afterEach(() => {
return testWithMongo.dropDb(DB_NAME)
});
it('Can find a document inserted in a fresh test', () => {
return insertDoc()
.then(getDoc)
.then((doc) => expect(doc).not.to.be.null);
});
it('cannot find a document inserted in a previous test', () => {
return getDoc()
.then((doc) => expect(doc).to.be.null);
});
it('clean db actually cleans db', () => {
return insertDoc()
.then(getDoc)
.then((doc) => expect(doc).not.to.be.null)
.then(() => testWithMongo.dropDb(DB_NAME))
.then(getDoc)
.then((doc) => expect(doc).to.be.null);
})
});