/
list.spec.js
167 lines (148 loc) · 4.88 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
'use strict';
var reporters = require('../../').reporters;
var List = reporters.List;
var Base = reporters.Base;
var createMockRunner = require('./helpers').createMockRunner;
var makeRunReporter = require('./helpers.js').createRunReporterFunction;
describe('List reporter', function() {
var runner;
var options = {};
var runReporter = makeRunReporter(List);
var useColors;
var expectedTitle = 'some title';
var expectedDuration = 100;
var test = {
fullTitle: function() {
return expectedTitle;
},
duration: expectedDuration,
slow: function() {}
};
beforeEach(function() {
useColors = Base.useColors;
Base.useColors = false;
});
afterEach(function() {
Base.useColors = useColors;
runner = undefined;
});
describe('on start and test', function() {
it('should write expected new line and title to the console', function() {
runner = createMockRunner('start test', 'start', 'test', null, test);
var stdout = runReporter({epilogue: function() {}}, runner, options);
var startString = '\n';
var testString = ' ' + expectedTitle + ': ';
var expectedArray = [startString, testString];
expect(stdout, 'to equal', expectedArray);
});
});
describe('on pending', function() {
it('should write expected title to the console', function() {
runner = createMockRunner('pending test', 'pending', null, null, test);
var stdout = runReporter({epilogue: function() {}}, runner, options);
expect(stdout[0], 'to equal', ' - ' + 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;
};
runner = createMockRunner('pass', 'pass', null, null, test);
runReporter({epilogue: function() {}}, runner, options);
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() {};
runner = createMockRunner('pass', 'pass', null, null, test);
var stdout = runReporter({epilogue: function() {}}, runner, options);
expect(
stdout[0],
'to be',
' ' +
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;
};
runner = createMockRunner('fail', 'fail', null, null, test);
runReporter({epilogue: function() {}}, runner, options);
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() {};
runner = createMockRunner('fail', 'fail', null, null, test);
var stdout = runReporter({epilogue: function() {}}, runner, options);
expect(
stdout[0],
'to be',
' ' + expectedErrorCount + ') ' + expectedTitle + '\n'
);
Base.cursor = cachedCursor;
});
it('should immediately construct fail strings', function() {
var actual = {a: 'actual'};
var expected = {a: 'expected'};
var checked = false;
var err;
test = {};
runner = createMockRunner('fail', 'fail', null, null, test);
runner.on = runner.once = function(event, callback) {
if (
!checked &&
event === 'fail' &&
callback.toString().includes('stringifyDiffObjs') // target correct fail event callback
) {
err = new Error('fake failure object with actual/expected');
err.actual = actual;
err.expected = expected;
err.showDiff = true;
callback(test, err);
checked = true;
}
};
runReporter({epilogue: function() {}}, runner, options);
expect(typeof err.actual, 'to be', 'string');
expect(typeof err.expected, 'to be', 'string');
});
});
describe('on end', function() {
it('should call epilogue', function() {
var calledEpilogue = false;
runner = createMockRunner('end', 'end');
runReporter(
{
epilogue: function() {
calledEpilogue = true;
}
},
runner,
options
);
expect(calledEpilogue, 'to be', true);
});
});
});