-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.js
103 lines (87 loc) · 2.9 KB
/
test.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
'use strict';
const expect = require('chai').expect;
const bunyanLogger = require('./index'),
MongoClient = require('mongodb').MongoClient;
const dbName = 'logger-test';
const mongoUrl = `mongodb://localhost/${dbName}`;
describe('bunyan-mongodb-logger', () => {
let loggerCollection, logger;
beforeEach('connect to MongoDB', () => {
return MongoClient.connect(mongoUrl, { useNewUrlParser: true })
.then((client) => client.db(dbName))
.then((db) => {
loggerCollection = db.collection('logs');
loggerCollection.removeMany({});
});
});
describe('requiring', () => {
it('should not throw an error', () => {
expect(() => require('./index')).to.not.throw();
});
});
describe('init', () => {
it('should throw an error when no logger name is specified', () => {
expect(() => bunyanLogger({
stream: 'mongodb',
url: mongoUrl
})).to.throw('Missing logger `name` option');
});
it('should throw an error when no logger stream is specified', () => {
expect(() => bunyanLogger({
name: 'test',
url: mongoUrl
})).to.throw('Missing logger `stream` or `streams` options');
});
it('should throw an error when no logger streams is specified', () => {
expect(() => bunyanLogger({
name: 'test',
streams: 'mongodb',
url: mongoUrl
})).to.throw('Expected `options.streams` to be an Array.');
});
it('should create logger with level\'s methods', () => {
logger = bunyanLogger({
name: 'test',
stream: 'mongodb',
url: mongoUrl
});
expect(logger).to.have.property('error');
expect(logger).to.have.property('info');
expect(logger).to.have.property('debug');
});
describe('when `logger.error` was called', () => {
beforeEach('create logger with single mongoDB stream', () => {
logger = bunyanLogger({
name: 'test',
stream: 'mongodb',
url: mongoUrl
});
});
beforeEach('call `logger.error`', () => {
logger.error(new Error('some error'), 'Some custom message');
});
it('should save `log` with selected stream', done => {
// timeout for inserting error into mongoDB collection
setTimeout(() => {
loggerCollection.findOne({})
.then(result => {
expect(result).to.have.property('msg', 'Some custom message');
done();
})
.catch(() => done());
}, 100);
});
it('should save `log` with right log level', done => {
// timeout for inserting error into mongoDB collection
setTimeout(() => {
loggerCollection.findOne({})
.then(result => {
expect(result).to.have.property('level', 50);
done();
})
.catch(() => done());
}, 100);
});
});
});
});