Skip to content

Commit 05e8abe

Browse files
hiroppyevilebottnawi
authored andcommitted
test: add output tests (#25)
1 parent f7ec65b commit 05e8abe

File tree

2 files changed

+75
-5
lines changed

2 files changed

+75
-5
lines changed

test/__snapshots__/test.js.snap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,12 @@ exports[`log should log for unique loggers 1`] = `"ℹ 「wds」: webpack-dev-se
44

55
exports[`log should log for unique loggers 2`] = `"ℹ 「wdm」: webpack-dev-middleware"`;
66

7+
exports[`log should return logs with prefix 1`] = `"₸ 「wds」: webpack-dev-server"`;
8+
9+
exports[`log should return logs with prefix 2`] = `"ℹ 「wds」: webpack-dev-server"`;
10+
11+
exports[`log should return logs with prefix 3`] = `"⚠ 「wds」: webpack-dev-server"`;
12+
13+
exports[`log should return logs with prefix 4`] = `"Trace: ₸ 「wds」: webpack-dev-server"`;
14+
715
exports[`log should return no color string 1`] = `"「wds」: webpack-dev-server"`;

test/test.js

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,26 @@ const strip = require('strip-ansi');
1010
const weblog = require('../src');
1111

1212
describe('log', () => {
13-
let consoleMock;
13+
let consoleInfoMock;
14+
let consoleWarnMock;
15+
let consoleDebugMock;
16+
let consoleTraceMock;
17+
let consoleErrorMock;
1418

1519
beforeEach(() => {
16-
consoleMock = jest.spyOn(console, 'info');
20+
consoleInfoMock = jest.spyOn(console, 'info');
21+
consoleWarnMock = jest.spyOn(console, 'warn');
22+
consoleDebugMock = jest.spyOn(console, 'debug');
23+
consoleTraceMock = jest.spyOn(console, 'trace');
24+
consoleErrorMock = jest.spyOn(console, 'error');
1725
});
1826

1927
afterEach(() => {
20-
consoleMock.mockRestore();
28+
consoleInfoMock.mockRestore();
29+
consoleWarnMock.mockRestore();
30+
consoleDebugMock.mockRestore();
31+
consoleTraceMock.mockRestore();
32+
consoleErrorMock.mockRestore();
2133
});
2234

2335
it('should exist', () => {
@@ -71,7 +83,7 @@ describe('log', () => {
7183
log.info('webpack-dev-server');
7284
log2.info('webpack-dev-middleware');
7385

74-
const [first, last] = consoleMock.mock.calls;
86+
const [first, last] = consoleInfoMock.mock.calls;
7587

7688
expect(strip(first[0])).toMatchSnapshot();
7789
expect(strip(last[0])).toMatchSnapshot();
@@ -103,8 +115,58 @@ describe('log', () => {
103115

104116
log.info('webpack-dev-server');
105117

106-
const [first] = consoleMock.mock.calls;
118+
const [first] = consoleInfoMock.mock.calls;
107119

108120
expect(first[0]).toMatchSnapshot();
109121
});
122+
123+
it('should return logs with prefix', () => {
124+
const log = weblog({ name: 'wds', id: 'foo' });
125+
126+
{
127+
log.level = 'trace';
128+
log.trace('webpack-dev-server');
129+
130+
const [first] = consoleTraceMock.mock.calls;
131+
132+
expect(strip(first[0])).toMatchSnapshot();
133+
}
134+
135+
{
136+
log.level = 'info';
137+
log.info('webpack-dev-server');
138+
139+
const [first] = consoleInfoMock.mock.calls;
140+
141+
expect(strip(first[0])).toMatchSnapshot();
142+
}
143+
144+
{
145+
log.level = 'warn';
146+
log.warn('webpack-dev-server');
147+
148+
const [first] = consoleWarnMock.mock.calls;
149+
150+
expect(strip(first[0])).toMatchSnapshot();
151+
}
152+
153+
{
154+
log.level = 'error';
155+
log.error('webpack-dev-server');
156+
157+
const [first] = consoleErrorMock.mock.calls;
158+
159+
expect(strip(first[0].split('\n')[0])).toMatchSnapshot();
160+
}
161+
});
162+
163+
it('should attach timestamp', () => {
164+
const log = weblog({ name: 'wds', timestamp: true });
165+
166+
log.info('webpack-dev-server');
167+
168+
const [first] = consoleInfoMock.mock.calls;
169+
170+
expect(!!first[0].match(/[\d\d:\d\d:\d\d]/)).toBeTruthy();
171+
});
110172
});

0 commit comments

Comments
 (0)