8000 fix(test): moved mailgunAppender, hipchatAppender test to tap · xcj-coding/log4js-node@582df9f · GitHub
[go: up one dir, main page]

Skip to content

Commit 582df9f

Browse files
author
Gareth Jones
committed
fix(test): moved mailgunAppender, hipchatAppender test to tap
1 parent c99165c commit 582df9f

File tree

2 files changed

+233
-50
lines changed

2 files changed

+233
-50
lines changed

test/vows/hipchatAppender-test.js renamed to test/tap/hipchatAppender-test.js

Lines changed: 51 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

3-
const vows = require('vows');
4-
const assert = require('assert');
3+
const test = require('tap').test;
54
const log4js = require('../../lib/log4js');
65
const sandbox = require('sandboxed-module');
76

@@ -65,66 +64,68 @@ function setupLogging(category, options) {
6564
};
6665
}
6766

68-
vows.describe('HipChat appender').addBatch({
69-
'when logging to HipChat v2 API': {
70-
topic: function () {
71-
const customCallback = function () {
72-
return 'works';
73-
};
67+
test('HipChat appender', (batch) => {
68+
batch.test('when logging to HipChat v2 API', (t) => {
69+
const customCallback = function () {
70+
return 'works';
71+
};
72+
73+
const topic = setupLogging('myCategory', {
74+
type: 'hipchat',
75+
hipchat_token: 'User_Token_With_Notification_Privs',
76+
hipchat_room: 'Room_ID_Or_Name',
77+
hipchat_from: 'Log4js_Test',
78+
hipchat_notify: true,
79+
hipchat_host: 'hipchat.your-company.tld',
80+
hipchat_response_callback: customCallback
81+
});
82+
topic.logger.warn('Log event #1');
7483

75-
const setup = setupLogging('myCategory', {
76-
type: 'hipchat',
77-
hipchat_token: 'User_Token_With_Notification_Privs',
78-
hipchat_room: 'Room_ID_Or_Name',
79-
hipchat_from: 'Log4js_Test',
80-
hipchat_notify: true,
81-
hipchat_host: 'hipchat.your-company.tld',
82-
hipchat_response_callback: customCallback
83-
});
84-
setup.logger.warn('Log event #1');
85-
return setup;
86-
},
87-
'a request to hipchat_host should be sent': function (topic) {
84+
t.test('a request to hipchat_host should be sent', (assert) => {
8885
assert.equal(topic.lastRequest.notifier.host, 'hipchat.your-company.tld');
8986
assert.equal(topic.lastRequest.notifier.notify, true);
9087
assert.equal(topic.lastRequest.body, 'Log event #1');
9188
assert.equal(topic.lastRequest.level, 'warning');
92-
},
93-
'a custom callback to the HipChat response is supported': function (topic) {
94-
assert.equal(topic.lastRequest.callback(), 'works');
95-
}
96-
},
97-
'when missing options': {
98-
topic: function () {
99-
const setup = setupLogging('myLogger', {
100-
type: 'hipchat',
101-
});
102-
setup.logger.error('Log event #2');
103-
return setup;
104-
},
105-
'it sets some defaults': function (topic) {
89+
assert.end();
90+
});
91+
92+
t.equal(topic.lastRequest.callback(), 'works', 'a custom callback to the HipChat response is supported');
93+
t.end();
94+
});
95+
96+
batch.test('when missing options', (t) => {
97+
const topic = setupLogging('myLogger', {
98+
type: 'hipchat',
99+
});
100+
topic.logger.error('Log event #2');
101+
102+
t.test('it sets some defaults', (assert) => {
106103
assert.equal(topic.lastRequest.notifier.host, 'api.hipchat.com');
107104
assert.equal(topic.lastRequest.notifier.notify, false);
108105
assert.equal(topic.lastRequest.body, 'Log event #2');
109106
assert.equal(topic.lastRequest.level, 'failure');
110-
}
111-
},
112-
'when basicLayout is provided': {
113-
topic: function () {
114-
const setup = setupLogging('myLogger', {
115-
type: 'hipchat',
116-
layout: log4js.layouts.basicLayout
117-
});
118-
setup.logger.debug('Log event #3');
119-
return setup;
120-
},
121-
'it should include the timestamp': function (topic) {
107+
assert.end();
108+
});
109+
t.end();
110+
});
111+
112+
batch.test('when basicLayout is provided', (t) => {
113+
const topic = setupLogging('myLogger', {
114+
type: 'hipchat',
115+
layout: log4js.layouts.basicLayout
116+
});
117+
topic.logger.debug('Log event #3');
118+
119+
t.test('it should include the timestamp', (assert) => {
122120
// basicLayout adds [TIMESTAMP] [LEVEL] category - message
123121
// e.g. [2016-06-10 11:50:53.819] [DEBUG] myLogger - Log event #23
124122

125123
assert.match(topic.lastRequest.body, /^\[[^\]]+] \[[^\]]+].*Log event #3$/);
126124
assert.equal(topic.lastRequest.level, 'info');
127-
}
128-
}
125+
assert.end();
126+
});
127+
t.end();
128+
});
129129

130-
}).export(module);
130+
batch.end();
131+
});

test/tap/mailgunAppender-test.js

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
'use strict';
2+
3+
const test = require('tap').test;
4+
const log4js = require('../../lib/log4js');
5+
const sandbox = require('sandboxed-module');
6+
7+
function setupLogging(category, options) {
8+
const msgs = [];
9+
10+
const mailgunCredentials = {
11+
apiKey: options.apikey,
12+
domain: options.domain
13+
};
14+
15+
const fakeMailgun = function () {
16+
return {
17+
messages: function () {
18+
return {
19+
config: options,
20+
send: function (data, callback) {
21+
msgs.push(data);
22+
callback(false, { status: 'OK' });
23+
}
24+
};
25+
}
26+
};
27+
};
28+
29+
const fakeLayouts = {
30+
layout: function (type, config) {
31+
this.type = type;
32+
this.config = config;
33+
return log4js.layouts.messagePassThroughLayout;
34+
},
35+
basicLayout: log4js.layouts.basicLayout,
36+
messagePassThroughLayout: log4js.layouts.messagePassThroughLayout
37+
};
38+
39+
const fakeConsole = {
40+
errors: [],
41+
logs: [],
42+
error: function (msg, value) {
43+
this.errors.push({ msg: msg, value: value });
44+
},
45+
log: function (msg, value) {
46+
this.logs.push({ msg: msg, value: value });
47+
}
48+
};
49+
50+
51+
const mailgunModule = sandbox.require('../../lib/appenders/mailgun', {
52+
requires: {
53+
'mailgun-js': fakeMailgun,
54+
'../layouts': fakeLayouts
55+
},
56+
globals: {
57+
console: fakeConsole
58+
}
59+
});
60+
61+
62+
log4js.addAppender(mailgunModule.configure(options), category);
63+
64+
return {
65+
logger: log4js.getLogger(category),
66+
mailer: fakeMailgun,
67+
layouts: fakeLayouts,
68+
console: fakeConsole,
69+
mails: msgs,
70+
credentials: mailgunCredentials
71+
};
72+
}
73+
74+
function checkMessages(assert, result) {
75+
for (let i = 0; i < result.mails.length; ++i) {
76+
assert.equal(result.mails[i].from, 'sender@domain.com');
77+
assert.equal(result.mails[i].to, 'recepient@domain.com');
78+
assert.equal(result.mails[i].subject, 'This is subject');
79+
assert.ok(new RegExp(`.+Log event #${i + 1}`).test(result.mails[i].text));
80+
}
81+
}
82+
83+
log4js.clearAppenders();
84+
85+
test('log4js mailgunAppender', (batch) => {
86+
batch.test('mailgun setup', (t) => {
87+
const result = setupLogging('mailgun setup', {
88+
apikey: 'APIKEY',
89+
domain: 'DOMAIN',
90+
from: 'sender@domain.com',
91+
to: 'recepient@domain.com',
92+
subject: 'This is subject'
93+
});
94+
95+
t.test('mailgun credentials should match', (assert) => {
96+
assert.equal(result.credentials.apiKey, 'APIKEY');
97+
assert.equal(result.credentials.domain, 'DOMAIN');
98+
assert.end();
99+
});
100+
t.end();
101+
});
102+
103+
batch.test('basic usage', (t) => {
104+
const result = setupLogging('basic usage', {
105+
apikey: 'APIKEY',
106+
domain: 'DOMAIN',
107+
from: 'sender@domain.com',
108+
to: 'recepient@domain.com',
109+
subject: 'This is subject'
110+
});
111+
112+
result.logger.info('Log event #1');
113+
114+
t.equal(result.mails.length, 1, 'should be one message only');
115+
checkMessages(t, result);
116+
t.end();
117+
});
118+
119+
batch.test('config with layout', (t) => {
120+
const result = setupLogging('config with layout', {
121+
layout: {
122+
type: 'tester'
123+
}
124+
});
125+
t.equal(result.layouts.type, 'tester', 'should configure layout');
126+
t.end();
127+
});
128+
129+
batch.test('error when sending email', (t) => {
130+
const setup = setupLogging('separate email for each event', {
131+
apikey: 'APIKEY',
132+
domain: 'DOMAIN',
133+
from: 'sender@domain.com',
134+
to: 'recepient@domain.com',
135+
subject: 'This is subject'
136+
});
137+
138+
setup.mailer.messages = function () {
139+
return {
140+
send: function (msg, cb) {
141+
cb({ msg: 'log4js.mailgunAppender - Error happened' }, null);
142+
}
143+
};
144+
};
145+
146+
setup.logger.info('This will break');
147+
const cons = setup.console;
148+
149+
t.test('should be logged to console', (assert) => {
150+
assert.equal(cons.errors.length, 1);
151+
assert.equal(cons.errors[0].msg, 'log4js.mailgunAppender - Error happened');
152+
assert.end();
153+
});
154+
t.end();
155+
});
156+
157+
batch.test('separate email for each event', (t) => {
158+
const setup = setupLogging('separate email for each event', {
159+
apikey: 'APIKEY',
160+
domain: 'DOMAIN',
161+
from: 'sender@domain.com',
162+
to: 'recepient@domain.com',
163+
subject: 'This is subject'
164+
});
165+
setTimeout(() => {
166+
setup.logger.info('Log event #1');
167+
}, 0);
168+
setTimeout(() => {
169+
setup.logger.info('Log event #2');
170+
}, 500);
171+
setTimeout(() => {
172+
setup.logger.info('Log event #3');
173+
}, 1100);
174+
setTimeout(() => {
175+
t.equal(setup.mails.length, 3, 'should be three messages');
176+
checkMessages(t, setup);
177+
t.end();
178+
}, 3000);
179+
});
180+
181+
batch.end();
182+
});

0 commit comments

Comments
 (0)
0