/
errors.spec.js
172 lines (150 loc) · 4.6 KB
/
errors.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
'use strict';
var errors = require('../../lib/errors');
const sinon = require('sinon');
const {createNoFilesMatchPatternError} = require('../../lib/errors');
describe('Errors', function() {
afterEach(function() {
sinon.restore();
});
var message = 'some message';
describe('createInvalidReporterError()', function() {
it('should include expected code in thrown reporter errors', function() {
expect(
errors.createInvalidReporterError(message, 'badReporter'),
'to satisfy',
{
message: message,
code: 'ERR_MOCHA_INVALID_REPORTER',
reporter: 'badReporter'
}
);
});
});
describe('createInvalidInterfaceError()', function() {
it('should include expected code in thrown interface errors', function() {
expect(
errors.createInvalidInterfaceError(message, 'badUi'),
'to satisfy',
{
message: message,
code: 'ERR_MOCHA_INVALID_INTERFACE',
interface: 'badUi'
}
);
});
});
describe('createForbiddenExclusivityError()', function() {
describe('when Mocha instance is running in a worker process', function() {
it('should output a message regarding incompatibility', function() {
var mocha = {isWorker: true};
expect(
errors.createForbiddenExclusivityError(mocha, {}),
'to satisfy',
{
message: /parallel/,
code: errors.constants.FORBIDDEN_EXCLUSIVITY
}
);
});
});
describe('when Mocha instance is not running in a worker process', function() {
it('should output a message regarding --forbid-only', function() {
var mocha = {};
expect(
errors.createForbiddenExclusivityError(mocha, {}),
'to satisfy',
{
message: /--forbid-only/,
code: errors.constants.FORBIDDEN_EXCLUSIVITY
}
);
});
});
});
describe('deprecate()', function() {
var emitWarning;
beforeEach(function() {
if (process.emitWarning) {
emitWarning = process.emitWarning;
sinon.stub(process, 'emitWarning');
} else {
process.emitWarning = sinon.spy();
}
errors.deprecate.cache = {};
});
afterEach(function() {
// if this is not set, then we created it, so we should remove it.
if (!emitWarning) {
delete process.emitWarning;
}
});
it('should coerce its parameter to a string', function() {
errors.deprecate(1);
expect(process.emitWarning, 'to have a call satisfying', [
'1',
'DeprecationWarning'
]);
});
it('should cache the message', function() {
errors.deprecate('foo');
errors.deprecate('foo');
expect(process.emitWarning, 'was called times', 1);
});
it('should ignore falsy messages', function() {
errors.deprecate('');
expect(process.emitWarning, 'was not called');
});
});
describe('warn()', function() {
var emitWarning;
beforeEach(function() {
if (process.emitWarning) {
emitWarning = process.emitWarning;
sinon.stub(process, 'emitWarning');
} else {
process.emitWarning = sinon.spy();
}
});
afterEach(function() {
// if this is not set, then we created it, so we should remove it.
if (!emitWarning) {
delete process.emitWarning;
}
});
it('should call process.emitWarning', function() {
errors.warn('foo');
expect(process.emitWarning, 'was called times', 1);
});
it('should not cache messages', function() {
errors.warn('foo');
errors.warn('foo');
expect(process.emitWarning, 'was called times', 2);
});
it('should ignore falsy messages', function() {
errors.warn('');
expect(process.emitWarning, 'was not called');
});
});
describe('isMochaError()', function() {
describe('when provided an Error object having a known Mocha error code', function() {
it('should return true', function() {
expect(
errors.isMochaError(createNoFilesMatchPatternError('derp')),
'to be true'
);
});
});
describe('when provided an Error object with a non-Mocha error code', function() {
it('should return false', function() {
const err = new Error();
err.code = 'ENOTEA';
expect(errors.isMochaError(err), 'to be false');
});
});
describe('when provided a non-error', function() {
it('should return false', function() {
expect(errors.isMochaError(), 'to be false');
});
});
});
});