forked from sinonjs/sinon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
issues-test.js
248 lines (199 loc) · 7.54 KB
/
issues-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
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
238
239
240
241
242
243
244
245
246
247
248
"use strict";
var referee = require("referee");
var sinon = require("../../lib/sinon");
var configureLogError = require("../../lib/sinon/util/core/log_error.js");
var assert = referee.assert;
var refute = referee.refute;
describe("issues", function () {
beforeEach(function () {
this.sandbox = sinon.sandbox.create();
});
afterEach(function () {
this.sandbox.restore();
});
it("#283", function () {
function testSinonFakeTimersWith(interval, ticks) {
var clock = sinon.useFakeTimers();
var spy = sinon.spy();
var id = setInterval(spy, interval);
assert(!spy.calledOnce);
clock.tick(ticks);
assert(spy.callCount === Math.floor(ticks / interval));
clearInterval(id);
clock.restore();
}
testSinonFakeTimersWith(10, 101);
testSinonFakeTimersWith(99, 101);
testSinonFakeTimersWith(100, 200);
testSinonFakeTimersWith(199, 200);
testSinonFakeTimersWith(500, 1001);
testSinonFakeTimersWith(1000, 1001);
});
it.skip("#397", function () {
var clock = sinon.useFakeTimers();
var cb2 = sinon.spy();
var cb1 = sinon.spy(function () {
setTimeout(cb2, 0);
});
setTimeout(cb1, 0);
clock.tick(10);
assert(cb1.called);
assert(!cb2.called);
clock.tick(10);
assert(cb2.called);
clock.restore();
});
describe("#458", function () {
if (typeof require("fs").readFileSync !== "undefined") {
describe("on node", function () {
it("stub out fs.readFileSync", function () {
var fs = require("fs");
var testCase = this;
refute.exception(function () {
testCase.sandbox.stub(fs, "readFileSync");
});
});
});
}
});
describe("#624", function () {
it.skip("useFakeTimers should be idempotent", function () {
// Issue #624 shows that useFakeTimers is not idempotent when it comes to
// using Date.now
// This test verifies that it's working, at least for Date.now
var clock;
clock = sinon.useFakeTimers(new Date("2014-12-29").getTime());
assert.equals(clock.now, Date.now());
clock = sinon.useFakeTimers(new Date("2015-12-15").getTime());
assert.equals(clock.now, Date.now());
clock = sinon.useFakeTimers(new Date("2015-1-5").getTime());
assert.equals(clock.now, Date.now());
});
});
describe("#835", function () {
it("logError() throws an exception if the passed err is read-only", function () {
var logError = configureLogError({useImmediateExceptions: true});
// passes
var err = { name: "TestError", message: "this is a proper exception" };
try {
logError("#835 test", err);
} catch (ex) {
assert.equals(ex.name, err.name);
}
// fails until this issue is fixed
try {
logError("#835 test", "this literal string is not a proper exception");
} catch (ex) {
assert.equals(ex.name, "#835 test");
}
});
});
describe("#852 - createStubInstance on intherited constructors", function () {
it("must not throw error", function () {
var A = function () {};
var B = function () {};
B.prototype = Object.create(A.prototype);
B.prototype.constructor = A;
refute.exception(function () {
sinon.createStubInstance(B);
});
});
});
describe("#852(2) - createStubInstance should on same constructor", function () {
it("must be idempotent", function () {
var A = function () {};
refute.exception(function () {
sinon.createStubInstance(A);
sinon.createStubInstance(A);
});
});
});
describe("#1026", function () {
it("should stub `watch` method on any Object", function () {
// makes sure that Object.prototype.watch is set back to its old value
function restore(oldWatch) {
if (oldWatch) {
Object.prototype.watch = oldWatch; // eslint-disable-line no-extend-native
} else {
delete Object.prototype.watch;
}
}
try {
var oldWatch = Object.prototype.watch;
if (typeof Object.prototype.watch !== "function") {
Object.prototype.watch = function rolex() {}; // eslint-disable-line no-extend-native
}
var stubbedObject = sinon.stub({
watch: function () {}
});
stubbedObject.watch();
assert.isArray(stubbedObject.watch.args);
} catch (error) {
restore(oldWatch);
throw error;
}
restore(oldWatch);
});
});
describe("#1154", function () {
it("Ensures different matchers will not be tested against each other", function () {
var match = sinon.match;
var stub = sinon.stub;
var readFile = stub();
function endsWith(str, suffix) {
return str.indexOf(suffix) + suffix.length === str.length;
}
function suffixA(fileName) {
return endsWith(fileName, "suffixa");
}
function suffixB(fileName) {
return endsWith(fileName, "suffixb");
}
var argsA = match(suffixA);
var argsB = match(suffixB);
var firstFake = readFile
.withArgs(argsA);
var secondFake = readFile
.withArgs(argsB);
assert(firstFake !== secondFake);
});
});
describe("#1274 - spy.withArgs(args...).callCount is incorrect", function () {
it("case1: should count accurately", function () {
var obj = {
f: function () {}
};
// case1: withArgs(1) => withArgs(1, 1)
var spy = sinon.spy(obj, "f");
assert.equals(spy.callCount, 0);
assert.equals(spy.withArgs(1).callCount, 0);
assert.equals(spy.withArgs(1, 1).callCount, 0);
obj.f();
obj.f(1);
obj.f(1, 1);
obj.f(1, 2);
assert.equals(spy.callCount, 4);
assert.equals(spy.withArgs(1).callCount, 3);
assert.equals(spy.withArgs(1, 1).callCount, 1);
assert.equals(spy.withArgs(1, 2).callCount, 1);
});
it("case2: should count accurately", function () {
var obj = {
f: function () {}
};
// case2: withArgs(1, 1) => withArgs(1)
var spy = sinon.spy(obj, "f");
assert.equals(spy.callCount, 0);
assert.equals(spy.withArgs(1, 1).callCount, 0);
assert.equals(spy.withArgs(1).callCount, 0);
obj.f();
obj.f(1);
obj.f(1, 1);
obj.f(1, 2);
assert.equals(spy.callCount, 4);
assert.equals(spy.withArgs(1).callCount, 3);
assert.equals(spy.withArgs(1, 1).callCount, 1);
assert.equals(spy.withArgs(1, 2).callCount, 1);
});
});
});