-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
172 lines (133 loc) · 4.52 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
'use strict';
const proxyquire = require('proxyquire');
const stripAnsi = require('strip-ansi');
const expect = require('chai').expect;
const chalk = require('chalk');
const sinon = require('sinon');
/*
* The Chalk object is read-only so we'll need to stub everything for
* Sinon to work.
*/
const chalkStub = Object.create(chalk, {
cyan: {
value: (str) => {
return chalk.cyan(str);
},
writable: true
},
green: {
value: (str) => {
return chalk.green(str);
},
writable: true
},
magenta: {
value: (str) => {
return chalk.magenta(str);
},
writable: true
},
red: {
value: (str) => {
return chalk.red(str);
},
writable: true
},
yellow: {
value: (str) => {
return chalk.yellow(str);
},
writable: true
}
});
const reporter = proxyquire('./index', {
chalk: chalkStub
});
describe('reporter:stylish', () => {
const colorsEnabled = chalk.enabled;
beforeEach(() => {
chalk.enabled = false;
sinon.stub(process.stdout, 'write');
});
afterEach(() => {
if (process.stdout.write.restore) {
process.stdout.write.restore();
}
chalk.enabled = colorsEnabled;
});
it('should not print anything when not passed any errors', () => {
const errors = [];
sinon.spy(console, 'log');
reporter.report(errors);
expect(console.log.called).to.equal(false);
console.log.restore();
});
it('should print errors with colors', () => {
const errors = [{
column: 5,
file: 'file.less',
line: 1,
linter: 'spaceBeforeBrace',
message: 'Opening curly brace should be preceded by one space.',
source: '.foo{ color: red; }'
}];
sinon.spy(console, 'log');
sinon.spy(chalkStub, 'cyan');
sinon.spy(chalkStub, 'green');
sinon.spy(chalkStub, 'magenta');
sinon.spy(chalkStub, 'yellow');
reporter.report(errors);
expect(chalkStub.cyan.called).to.equal(true);
expect(chalkStub.green.called).to.equal(true);
expect(chalkStub.magenta.called).to.equal(true);
expect(chalkStub.yellow.called).to.equal(true);
const message = stripAnsi(console.log.getCall(0).args[0]);
expect(message).to.equal('Warning: file.less: line 1, col 5, spaceBeforeBrace: Opening curly brace should be preceded by one space.');
console.log.restore();
});
it('should not print line when not passed one', () => {
const errors = [{
column: 5,
file: 'file.less',
linter: 'spaceBeforeBrace',
message: 'Opening curly brace should be preceded by one space.',
source: '.foo{ color: red; }'
}];
sinon.spy(console, 'log');
reporter.report(errors);
const message = stripAnsi(console.log.getCall(0).args[0]);
expect(message).to.equal('Warning: file.less: col 5, spaceBeforeBrace: Opening curly brace should be preceded by one space.');
console.log.restore();
});
it('should not print column when not passed one', () => {
const errors = [{
line: 1,
file: 'file.less',
linter: 'spaceBeforeBrace',
message: 'Opening curly brace should be preceded by one space.',
source: '.foo{ color: red; }'
}];
sinon.spy(console, 'log');
reporter.report(errors);
const message = stripAnsi(console.log.getCall(0).args[0]);
expect(message).to.equal('Warning: file.less: line 1, spaceBeforeBrace: Opening curly brace should be preceded by one space.');
console.log.restore();
});
it('should print the result severity', () => {
const errors = [{
line: 1,
file: 'file.less',
linter: 'spaceBeforeBrace',
message: 'Opening curly brace should be preceded by one space.',
severity: 'error',
source: '.foo{ color: red; }'
}];
sinon.spy(console, 'log');
sinon.spy(chalkStub, 'red');
reporter.report(errors);
expect(chalkStub.red.called).to.equal(true);
const message = stripAnsi(console.log.getCall(0).args[0]);
expect(message).to.equal('Error: file.less: line 1, spaceBeforeBrace: Opening curly brace should be preceded by one space.');
console.log.restore();
});
});