forked from mochajs/mocha
/
list.spec.js
237 lines (214 loc) · 6.32 KB
/
list.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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
'use strict';
var reporters = require('../../').reporters;
var List = reporters.List;
var Base = reporters.Base;
describe('List reporter', function () {
var stdout;
var stdoutWrite;
var runner;
var useColors;
beforeEach(function () {
stdout = [];
runner = {};
stdoutWrite = process.stdout.write;
process.stdout.write = function (string) {
stdout.push(string);
};
useColors = Base.useColors;
Base.useColors = false;
});
afterEach(function () {
Base.useColors = useColors;
});
describe('on start and test', function () {
it('should write expected new line and title to the console', function () {
var expectedTitle = 'some title';
var test = {
fullTitle: function () {
return expectedTitle;
}
};
runner.on = function (event, callback) {
if (event === 'start') {
callback();
}
if (event === 'test') {
callback(test);
}
};
List.call({epilogue: function () {}}, runner);
process.stdout.write = stdoutWrite;
var startString = '\n';
var testString = ' ' + expectedTitle + ': ';
var expectedArray = [
startString,
testString
];
expect(stdout).to.eql(expectedArray);
});
});
describe('on pending', function () {
it('should write expected title to the console', function () {
var expectedTitle = 'some title';
var test = {
fullTitle: function () {
return expectedTitle;
}
};
runner.on = function (event, callback) {
if (event === 'pending') {
callback(test);
}
};
List.call({epilogue: function () {}}, runner);
process.stdout.write = stdoutWrite;
expect(stdout[0]).to.eql(' - ' + expectedTitle + '\n');
});
});
describe('on pass', function () {
it('should call cursor CR', function () {
var calledCursorCR = false;
var cachedCursor = Base.cursor;
Base.cursor.CR = function () {
calledCursorCR = true;
};
var expectedTitle = 'some title';
var expectedDuration = 100;
var test = {
fullTitle: function () {
return expectedTitle;
},
duration: expectedDuration,
slow: function () {}
};
runner.on = function (event, callback) {
if (event === 'pass') {
callback(test);
}
};
List.call({epilogue: function () {}}, runner);
process.stdout.write = stdoutWrite;
expect(calledCursorCR).to.be(true);
Base.cursor = cachedCursor;
});
it('should write expected symbol, title and duration to the console', function () {
var cachedSymbols = Base.symbols;
var expectedOkSymbol = 'OK';
Base.symbols.ok = expectedOkSymbol;
var cachedCursor = Base.cursor;
Base.cursor.CR = function () {};
var expectedTitle = 'some title';
var expectedDuration = 100;
var test = {
fullTitle: function () {
return expectedTitle;
},
duration: expectedDuration,
slow: function () {}
};
runner.on = function (event, callback) {
if (event === 'pass') {
callback(test);
}
};
List.call({epilogue: function () {}}, runner);
process.stdout.write = stdoutWrite;
expect(stdout[0]).to.equal(' ' + expectedOkSymbol + ' ' + expectedTitle + ': ' + expectedDuration + 'ms\n');
Base.cursor = cachedCursor;
Base.symbols = cachedSymbols;
});
});
describe('on fail', function () {
it('should call cursor CR', function () {
var calledCursorCR = false;
var cachedCursor = Base.cursor;
Base.cursor.CR = function () {
calledCursorCR = true;
};
var expectedTitle = 'some title';
var expectedDuration = 100;
var test = {
fullTitle: function () {
return expectedTitle;
},
duration: expectedDuration,
slow: function () {}
};
runner.on = function (event, callback) {
if (event === 'fail') {
callback(test);
}
};
List.call({epilogue: function () {}}, runner);
process.stdout.write = stdoutWrite;
expect(calledCursorCR).to.be(true);
Base.cursor = cachedCursor;
});
it('should write expected error number and title', function () {
var cachedCursor = Base.cursor;
var expectedErrorCount = 1;
Base.cursor.CR = function () {};
var expectedTitle = 'some title';
var expectedDuration = 100;
var test = {
fullTitle: function () {
return expectedTitle;
},
duration: expectedDuration,
slow: function () {}
};
runner.on = function (event, callback) {
if (event === 'fail') {
callback(test);
}
};
runner.on = function (event, callback) {
if (event === 'fail') {
callback(test);
}
};
List.call({epilogue: function () {}}, runner);
process.stdout.write = stdoutWrite;
expect(stdout[0]).to.equal(' ' + expectedErrorCount + ') ' + expectedTitle + '\n');
Base.cursor = cachedCursor;
});
it('should immediately construct fail strings', function () {
var actual = { a: 'actual' };
var expected = { a: 'expected' };
var test = {};
var checked = false;
var err;
runner.on = function (event, callback) {
if (!checked && event === 'fail') {
err = new Error('fake failure object with actual/expected');
err.actual = actual;
err.expected = expected;
err.showDiff = true;
callback(test, err);
checked = true;
}
};
List.call({epilogue: function () {}}, runner);
process.stdout.write = stdoutWrite;
expect(typeof err.actual).to.equal('string');
expect(typeof err.expected).to.equal('string');
});
});
describe('on end', function () {
it('should call epilogue', function () {
var calledEpilogue = false;
runner.on = function (event, callback) {
if (event === 'end') {
callback();
}
};
List.call({
epilogue: function () {
calledEpilogue = true;
}
}, runner);
process.stdout.write = stdoutWrite;
expect(calledEpilogue).to.be(true);
});
});
});