jasmine-core 2.8.0 → 2.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright (c) 2008-2017 Pivotal Labs
2
+ Copyright (c) 2008-2018 Pivotal Labs
3
3
 
4
4
  Permission is hereby granted, free of charge, to any person obtaining
5
5
  a copy of this software and associated documentation files (the
@@ -1,5 +1,7 @@
1
1
  describe("Clock", function() {
2
2
 
3
+ var NODE_JS = typeof process !== 'undefined' && process.versions && typeof process.versions.node === 'string';
4
+
3
5
  it("does not replace setTimeout until it is installed", function() {
4
6
  var fakeSetTimeout = jasmine.createSpy("global setTimeout"),
5
7
  fakeGlobal = { setTimeout: fakeSetTimeout },
@@ -294,13 +296,19 @@ describe("Clock", function() {
294
296
  fakeGlobal = { setTimeout: fakeSetTimeout },
295
297
  delayedFn = jasmine.createSpy('delayedFn'),
296
298
  mockDate = { install: function() {}, tick: function() {}, uninstall: function() {} },
297
- clock = new jasmineUnderTest.Clock(fakeGlobal, function () { return delayedFunctionScheduler; }, mockDate);
299
+ clock = new jasmineUnderTest.Clock(fakeGlobal, function () { return delayedFunctionScheduler; }, mockDate),
300
+ timeout = new clock.FakeTimeout();
298
301
 
299
302
  clock.install();
300
303
  clock.setTimeout(delayedFn, 0, 'a', 'b');
301
304
 
302
305
  expect(fakeSetTimeout).not.toHaveBeenCalled();
303
- expect(delayedFunctionScheduler.scheduleFunction).toHaveBeenCalledWith(delayedFn, 0, ['a', 'b']);
306
+
307
+ if (!NODE_JS) {
308
+ expect(delayedFunctionScheduler.scheduleFunction).toHaveBeenCalledWith(delayedFn, 0, ['a', 'b']);
309
+ } else {
310
+ expect(delayedFunctionScheduler.scheduleFunction).toHaveBeenCalledWith(delayedFn, 0, ['a', 'b'], false, timeout);
311
+ }
304
312
  });
305
313
 
306
314
  it("returns an id for the delayed function", function() {
@@ -312,12 +320,16 @@ describe("Clock", function() {
312
320
  delayedFn = jasmine.createSpy('delayedFn'),
313
321
  mockDate = { install: function() {}, tick: function() {}, uninstall: function() {} },
314
322
  clock = new jasmineUnderTest.Clock(fakeGlobal, function () { return delayedFunctionScheduler; }, mockDate),
315
- timeoutId;
323
+ timeout;
316
324
 
317
325
  clock.install();
318
- timeoutId = clock.setTimeout(delayedFn, 0);
326
+ timeout = clock.setTimeout(delayedFn, 0);
319
327
 
320
- expect(timeoutId).toEqual(123);
328
+ if (!NODE_JS) {
329
+ expect(timeout).toEqual(123);
330
+ } else {
331
+ expect(timeout.constructor.name).toEqual('FakeTimeout');
332
+ }
321
333
  });
322
334
 
323
335
  it("clears the scheduled function with the scheduler", function() {
@@ -342,13 +354,19 @@ describe("Clock", function() {
342
354
  fakeGlobal = { setInterval: fakeSetInterval },
343
355
  delayedFn = jasmine.createSpy('delayedFn'),
344
356
  mockDate = { install: function() {}, tick: function() {}, uninstall: function() {} },
345
- clock = new jasmineUnderTest.Clock(fakeGlobal, function () { return delayedFunctionScheduler; }, mockDate);
357
+ clock = new jasmineUnderTest.Clock(fakeGlobal, function () { return delayedFunctionScheduler; }, mockDate),
358
+ timeout = new clock.FakeTimeout;
346
359
 
347
360
  clock.install();
348
361
  clock.setInterval(delayedFn, 0, 'a', 'b');
349
362
 
350
363
  expect(fakeSetInterval).not.toHaveBeenCalled();
351
- expect(delayedFunctionScheduler.scheduleFunction).toHaveBeenCalledWith(delayedFn, 0, ['a', 'b'], true);
364
+
365
+ if (!NODE_JS) {
366
+ expect(delayedFunctionScheduler.scheduleFunction).toHaveBeenCalledWith(delayedFn, 0, ['a', 'b'], true);
367
+ } else {
368
+ expect(delayedFunctionScheduler.scheduleFunction).toHaveBeenCalledWith(delayedFn, 0, ['a', 'b'], true, timeout);
369
+ }
352
370
  });
353
371
 
354
372
  it("returns an id for the delayed function", function() {
@@ -360,12 +378,16 @@ describe("Clock", function() {
360
378
  delayedFn = jasmine.createSpy('delayedFn'),
361
379
  mockDate = { install: function() {}, tick: function() {}, uninstall: function() {} },
362
380
  clock = new jasmineUnderTest.Clock(fakeGlobal, function () { return delayedFunctionScheduler; }, mockDate),
363
- intervalId;
381
+ interval;
364
382
 
365
383
  clock.install();
366
- intervalId = clock.setInterval(delayedFn, 0);
384
+ interval = clock.setInterval(delayedFn, 0);
367
385
 
368
- expect(intervalId).toEqual(123);
386
+ if (!NODE_JS) {
387
+ expect(interval).toEqual(123);
388
+ } else {
389
+ expect(interval.constructor.name).toEqual('FakeTimeout');
390
+ }
369
391
  });
370
392
 
371
393
  it("clears the scheduled function with the scheduler", function() {
@@ -401,7 +423,8 @@ describe("Clock", function() {
401
423
  setInterval: fakeSetInterval
402
424
  },
403
425
  mockDate = { install: function() {}, tick: function() {}, uninstall: function() {} },
404
- clock = new jasmineUnderTest.Clock(fakeGlobal, function () { return delayedFunctionScheduler; }, mockDate);
426
+ clock = new jasmineUnderTest.Clock(fakeGlobal, function () { return delayedFunctionScheduler; }, mockDate),
427
+ timeout = new clock.FakeTimeout();
405
428
 
406
429
  fakeSetTimeout.apply = null;
407
430
  fakeSetInterval.apply = null;
@@ -409,13 +432,25 @@ describe("Clock", function() {
409
432
  clock.install();
410
433
 
411
434
  clock.setTimeout(fn, 0);
412
- expect(delayedFunctionScheduler.scheduleFunction).toHaveBeenCalledWith(fn, 0, []);
435
+
436
+ if (!NODE_JS) {
437
+ expect(delayedFunctionScheduler.scheduleFunction).toHaveBeenCalledWith(fn, 0, []);
438
+ } else {
439
+ expect(delayedFunctionScheduler.scheduleFunction).toHaveBeenCalledWith(fn, 0, [], false, timeout);
440
+ }
441
+
413
442
  expect(function() {
414
443
  clock.setTimeout(fn, 0, 'extra');
415
444
  }).toThrow();
416
445
 
417
446
  clock.setInterval(fn, 0);
418
- expect(delayedFunctionScheduler.scheduleFunction).toHaveBeenCalledWith(fn, 0, [], true);
447
+
448
+ if (!NODE_JS) {
449
+ expect(delayedFunctionScheduler.scheduleFunction).toHaveBeenCalledWith(fn, 0, [], true);
450
+ } else {
451
+ expect(delayedFunctionScheduler.scheduleFunction).toHaveBeenCalledWith(fn, 0, [], true, timeout);
452
+ }
453
+
419
454
  expect(function() {
420
455
  clock.setInterval(fn, 0, 'extra');
421
456
  }).toThrow();
@@ -679,4 +714,41 @@ describe("Clock (acceptance)", function() {
679
714
 
680
715
  expect(actualTimes).toEqual([baseTime.getTime(), baseTime.getTime() + 1, baseTime.getTime() + 3]);
681
716
  })
717
+
718
+ it('correctly clears a scheduled timeout while the Clock is advancing', function () {
719
+ var delayedFunctionScheduler = new jasmineUnderTest.DelayedFunctionScheduler(),
720
+ global = {Date: Date, setTimeout: undefined},
721
+ mockDate = new jasmineUnderTest.MockDate(global),
722
+ clock = new jasmineUnderTest.Clock(global, function () { return delayedFunctionScheduler; }, mockDate);
723
+
724
+ clock.install();
725
+
726
+ var timerId2;
727
+
728
+ global.setTimeout(function () {
729
+ global.clearTimeout(timerId2);
730
+ }, 100);
731
+
732
+ timerId2 = global.setTimeout(fail, 100);
733
+
734
+ clock.tick(100);
735
+ });
736
+
737
+ it('correctly clears a scheduled interval while the Clock is advancing', function () {
738
+ var delayedFunctionScheduler = new jasmineUnderTest.DelayedFunctionScheduler(),
739
+ global = {Date: Date, setTimeout: undefined},
740
+ mockDate = new jasmineUnderTest.MockDate(global),
741
+ clock = new jasmineUnderTest.Clock(global, function () { return delayedFunctionScheduler; }, mockDate);
742
+
743
+ clock.install();
744
+
745
+ var timerId2;
746
+ var timerId1 = global.setInterval(function () {
747
+ global.clearInterval(timerId2);
748
+ }, 100);
749
+
750
+ timerId2 = global.setInterval(fail, 100);
751
+
752
+ clock.tick(400);
753
+ });
682
754
  });
@@ -216,21 +216,23 @@ describe("DelayedFunctionScheduler", function() {
216
216
 
217
217
  it("removes functions during a tick that runs the function", function() {
218
218
  var scheduler = new jasmineUnderTest.DelayedFunctionScheduler(),
219
- fn = jasmine.createSpy('fn'),
219
+ spy = jasmine.createSpy('fn'),
220
+ spyAndRemove = jasmine.createSpy('fn'),
220
221
  fnDelay = 10,
221
222
  timeoutKey;
222
223
 
223
- timeoutKey = scheduler.scheduleFunction(fn, fnDelay, [], true);
224
- scheduler.scheduleFunction(function () {
224
+ spyAndRemove.and.callFake(function() {
225
225
  scheduler.removeFunctionWithId(timeoutKey);
226
- }, 2 * fnDelay);
226
+ });
227
227
 
228
- expect(fn).not.toHaveBeenCalled();
228
+ scheduler.scheduleFunction(spyAndRemove, fnDelay);
229
229
 
230
- scheduler.tick(3 * fnDelay);
230
+ timeoutKey = scheduler.scheduleFunction(spy, fnDelay, [], true);
231
231
 
232
- expect(fn).toHaveBeenCalled();
233
- expect(fn.calls.count()).toBe(2);
232
+ scheduler.tick(2 * fnDelay);
233
+
234
+ expect(spy).not.toHaveBeenCalled();
235
+ expect(spyAndRemove).toHaveBeenCalled();
234
236
  });
235
237
 
236
238
  it("removes functions during the first tick that runs the function", function() {
@@ -17,7 +17,10 @@ describe("jasmineUnderTest.pp", function () {
17
17
  describe('stringify sets', function() {
18
18
  it("should stringify sets properly", function() {
19
19
  jasmine.getEnv().requireFunctioningSets();
20
- expect(jasmineUnderTest.pp(new Set([1, 2]))).toEqual("Set( 1, 2 )");
20
+ var set = new Set();
21
+ set.add(1);
22
+ set.add(2);
23
+ expect(jasmineUnderTest.pp(set)).toEqual("Set( 1, 2 )");
21
24
  });
22
25
 
23
26
  it("should truncate sets with more elments than jasmineUnderTest.MAX_PRETTY_PRINT_ARRAY_LENGTH", function() {
@@ -26,7 +29,11 @@ describe("jasmineUnderTest.pp", function () {
26
29
 
27
30
  try {
28
31
  jasmineUnderTest.MAX_PRETTY_PRINT_ARRAY_LENGTH = 2;
29
- expect(jasmineUnderTest.pp(new Set(["a", "b", "c"]))).toEqual("Set( 'a', 'b', ... )");
32
+ var set = new Set();
33
+ set.add('a');
34
+ set.add('b');
35
+ set.add('c');
36
+ expect(jasmineUnderTest.pp(set)).toEqual("Set( 'a', 'b', ... )");
30
37
  } finally {
31
38
  jasmineUnderTest.MAX_PRETTY_PRINT_ARRAY_LENGTH = originalMaxSize;
32
39
  }
@@ -36,7 +43,9 @@ describe("jasmineUnderTest.pp", function () {
36
43
  describe('stringify maps', function() {
37
44
  it("should stringify maps properly", function() {
38
45
  jasmine.getEnv().requireFunctioningMaps();
39
- expect(jasmineUnderTest.pp(new Map([[1, 2]]))).toEqual("Map( [ 1, 2 ] )");
46
+ var map = new Map();
47
+ map.set(1,2);
48
+ expect(jasmineUnderTest.pp(map)).toEqual("Map( [ 1, 2 ] )");
40
49
  });
41
50
 
42
51
  it("should truncate maps with more elments than jasmineUnderTest.MAX_PRETTY_PRINT_ARRAY_LENGTH", function() {
@@ -45,7 +54,11 @@ describe("jasmineUnderTest.pp", function () {
45
54
 
46
55
  try {
47
56
  jasmineUnderTest.MAX_PRETTY_PRINT_ARRAY_LENGTH = 2;
48
- expect(jasmineUnderTest.pp(new Map([["a", 1], ["b", 2], ["c", 3]]))).toEqual("Map( [ 'a', 1 ], [ 'b', 2 ], ... )");
57
+ var map = new Map();
58
+ map.set("a",1);
59
+ map.set("b",2);
60
+ map.set("c",3);
61
+ expect(jasmineUnderTest.pp(map)).toEqual("Map( [ 'a', 1 ], [ 'b', 2 ], ... )");
49
62
  } finally {
50
63
  jasmineUnderTest.MAX_PRETTY_PRINT_ARRAY_LENGTH = originalMaxSize;
51
64
  }
@@ -120,15 +133,50 @@ describe("jasmineUnderTest.pp", function () {
120
133
  });
121
134
 
122
135
  it("should truncate objects with too many keys", function () {
123
- var originalMaxLength = jasmineUnderTest.MAX_PRETTY_PRINT_ARRAY_LENGTH;
124
- var long = {a: 1, b: 2, c: 3};
136
+ var originalMaxLength = jasmineUnderTest.MAX_PRETTY_PRINT_ARRAY_LENGTH;
137
+ var long = {a: 1, b: 2, c: 3};
125
138
 
126
- try {
127
- jasmineUnderTest.MAX_PRETTY_PRINT_ARRAY_LENGTH = 2;
128
- expect(jasmineUnderTest.pp(long)).toEqual("Object({ a: 1, b: 2, ... })");
129
- } finally {
130
- jasmineUnderTest.MAX_PRETTY_PRINT_ARRAY_LENGTH = originalMaxLength;
131
- }
139
+ try {
140
+ jasmineUnderTest.MAX_PRETTY_PRINT_ARRAY_LENGTH = 2;
141
+ expect(jasmineUnderTest.pp(long)).toEqual("Object({ a: 1, b: 2, ... })");
142
+ } finally {
143
+ jasmineUnderTest.MAX_PRETTY_PRINT_ARRAY_LENGTH = originalMaxLength;
144
+ }
145
+ });
146
+
147
+ function withMaxChars(maxChars, fn) {
148
+ var originalMaxChars = jasmineUnderTest.MAX_PRETTY_PRINT_CHARS;
149
+
150
+ try {
151
+ jasmineUnderTest.MAX_PRETTY_PRINT_CHARS = maxChars;
152
+ fn();
153
+ } finally {
154
+ jasmineUnderTest.MAX_PRETTY_PRINT_CHARS = originalMaxChars;
155
+ }
156
+ }
157
+
158
+ it("should truncate outputs that are too long", function() {
159
+ var big = [
160
+ { a: 1, b: "a long string" },
161
+ {}
162
+ ];
163
+
164
+ withMaxChars(34, function() {
165
+ expect(jasmineUnderTest.pp(big)).toEqual("[ Object({ a: 1, b: 'a long st ...");
166
+ });
167
+ });
168
+
169
+ it("should not serialize more objects after hitting MAX_PRETTY_PRINT_CHARS", function() {
170
+ var a = { jasmineToString: function() { return 'object a'; } },
171
+ b = { jasmineToString: function() { return 'object b'; } },
172
+ c = { jasmineToString: jasmine.createSpy('c jasmineToString').and.returnValue('') },
173
+ d = { jasmineToString: jasmine.createSpy('d jasmineToString').and.returnValue('') };
174
+
175
+ withMaxChars(30, function() {
176
+ jasmineUnderTest.pp([{a: a, b: b, c: c}, d]);
177
+ expect(c.jasmineToString).not.toHaveBeenCalled();
178
+ expect(d.jasmineToString).not.toHaveBeenCalled();
179
+ });
132
180
  });
133
181
 
134
182
  it("should print 'null' as the constructor of an object with its own constructor property", function() {
@@ -216,9 +264,9 @@ describe("jasmineUnderTest.pp", function () {
216
264
 
217
265
  it("should stringify spy objects properly", function() {
218
266
  var TestObject = {
219
- someFunction: function() {}
220
- },
221
- env = new jasmineUnderTest.Env();
267
+ someFunction: function() {}
268
+ },
269
+ env = new jasmineUnderTest.Env();
222
270
 
223
271
  var spyRegistry = new jasmineUnderTest.SpyRegistry({currentSpies: function() {return [];}});
224
272
 
@@ -375,27 +375,30 @@ describe("QueueRunner", function() {
375
375
  expect(onComplete).toHaveBeenCalled();
376
376
  });
377
377
 
378
- it("fails the function when the promise is rejected", function() {
378
+ it("handles a rejected promise like an unhandled exception", function() {
379
379
  var promise = new StubPromise(),
380
380
  queueableFn1 = { fn: function() {
381
- setTimeout(function() { promise.rejectHandler('foo'); }, 100);
381
+ setTimeout(function() {
382
+ promise.rejectHandler('foo')
383
+ }, 100);
382
384
  return promise;
383
385
  } },
384
386
  queueableFn2 = { fn: jasmine.createSpy('fn2') },
385
387
  failFn = jasmine.createSpy('fail'),
388
+ onExceptionCallback = jasmine.createSpy('on exception callback'),
386
389
  queueRunner = new jasmineUnderTest.QueueRunner({
387
390
  queueableFns: [queueableFn1, queueableFn2],
388
- fail: failFn
391
+ onException: onExceptionCallback
389
392
  });
390
393
 
391
394
  queueRunner.execute();
392
395
 
393
- expect(failFn).not.toHaveBeenCalled();
396
+ expect(onExceptionCallback).not.toHaveBeenCalled();
394
397
  expect(queueableFn2.fn).not.toHaveBeenCalled();
395
398
 
396
399
  jasmine.clock().tick(100);
397
400
 
398
- expect(failFn).toHaveBeenCalledWith('foo');
401
+ expect(onExceptionCallback).toHaveBeenCalledWith('foo');
399
402
  expect(queueableFn2.fn).toHaveBeenCalled();
400
403
  });
401
404
  });
@@ -287,6 +287,24 @@ describe("SpyRegistry", function() {
287
287
 
288
288
  expect(jasmineUnderTest.isSpy(subject.spiedFunc)).toBe(false);
289
289
  });
290
+
291
+ it("restores window.onerror by overwriting, not deleting", function() {
292
+ function FakeWindow() {
293
+ }
294
+ FakeWindow.prototype.onerror = function() {};
295
+
296
+ var spies = [],
297
+ global = new FakeWindow(),
298
+ spyRegistry = new jasmineUnderTest.SpyRegistry({
299
+ currentSpies: function() { return spies; },
300
+ global: global
301
+ });
302
+
303
+ spyRegistry.spyOn(global, 'onerror');
304
+ spyRegistry.clearSpies();
305
+ expect(global.onerror).toBe(FakeWindow.prototype.onerror);
306
+ expect(global.hasOwnProperty('onerror')).toBe(true);
307
+ });
290
308
  });
291
309
 
292
310
  describe('spying on properties', function() {
@@ -92,12 +92,35 @@ describe("SpyStrategy", function() {
92
92
  expect(returnValue).toEqual(67);
93
93
  });
94
94
 
95
+ it("allows a fake async function to be called instead", function(done) {
96
+ jasmine.getEnv().requireAsyncAwait();
97
+ var originalFn = jasmine.createSpy("original"),
98
+ fakeFn = jasmine.createSpy("fake").and.callFake(eval("async () => { return 67; }")),
99
+ spyStrategy = new jasmineUnderTest.SpyStrategy({fn: originalFn}),
100
+ returnValue;
101
+
102
+ spyStrategy.callFake(fakeFn);
103
+ spyStrategy.exec().then(function (returnValue) {
104
+ expect(originalFn).not.toHaveBeenCalled();
105
+ expect(fakeFn).toHaveBeenCalled();
106
+ expect(returnValue).toEqual(67);
107
+ done();
108
+ }).catch(function (err) {
109
+ done.fail(err);
110
+ })
111
+ });
112
+
95
113
  it('throws an error when a non-function is passed to callFake strategy', function() {
96
114
  var originalFn = jasmine.createSpy('original'),
97
115
  spyStrategy = new jasmineUnderTest.SpyStrategy({fn: originalFn}),
98
116
  invalidFakes = [5, 'foo', {}, true, false, null, void 0, new Date(), /.*/];
99
117
 
100
118
  spyOn(jasmineUnderTest, 'isFunction_').and.returnValue(false);
119
+ spyOn(jasmineUnderTest, 'isAsyncFunction_').and.returnValue(false);
120
+
121
+ expect(function () {
122
+ spyStrategy.callFake(function() {});
123
+ }).toThrowError(/^Argument passed to callFake should be a function, got/);
101
124
 
102
125
  expect(function () {
103
126
  spyStrategy.callFake(function() {});
@@ -29,6 +29,38 @@ describe("Any", function() {
29
29
  expect(any.asymmetricMatch(true)).toBe(true);
30
30
  });
31
31
 
32
+ it("matches a Map", function() {
33
+ jasmine.getEnv().requireFunctioningMaps();
34
+
35
+ var any = new jasmineUnderTest.Any(Map);
36
+
37
+ expect(any.asymmetricMatch(new Map())).toBe(true);
38
+ });
39
+
40
+ it("matches a Set", function() {
41
+ jasmine.getEnv().requireFunctioningSets();
42
+
43
+ var any = new jasmineUnderTest.Any(Set);
44
+
45
+ expect(any.asymmetricMatch(new Set())).toBe(true);
46
+ });
47
+
48
+ it("matches a TypedArray", function() {
49
+ jasmine.getEnv().requireFunctioningTypedArrays();
50
+
51
+ var any = new jasmineUnderTest.Any(Uint32Array);
52
+
53
+ expect(any.asymmetricMatch(new Uint32Array([]))).toBe(true);
54
+ });
55
+
56
+ it("matches a Symbol", function() {
57
+ jasmine.getEnv().requireFunctioningSymbols();
58
+
59
+ var any = new jasmineUnderTest.Any(Symbol);
60
+
61
+ expect(any.asymmetricMatch(Symbol())).toBe(true);
62
+ });
63
+
32
64
  it("matches another constructed object", function() {
33
65
  var Thing = function() {},
34
66
  any = new jasmineUnderTest.Any(Thing);