Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit b3fdd7e

Browse files
JiaLiPassionmhevery
authored andcommitted
fix(test): fix #1069, FakeDate should handle constructor parameter (#1070)
1 parent a86bddb commit b3fdd7e

File tree

4 files changed

+228
-36
lines changed

4 files changed

+228
-36
lines changed

Diff for: lib/jasmine/jasmine.ts

+26-19
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,14 @@
6767
return originalJasmineFn.apply(this, arguments);
6868
};
6969
});
70-
if (enableClockPatch) {
71-
const originalClockFn: Function = ((jasmine as any)[symbol('clock')] = jasmine['clock']);
72-
(jasmine as any)['clock'] = function() {
73-
const clock = originalClockFn.apply(this, arguments);
70+
71+
// need to patch jasmine.clock().mockDate and jasmine.clock().tick() so
72+
// they can work properly in FakeAsyncTest
73+
const originalClockFn: Function = ((jasmine as any)[symbol('clock')] = jasmine['clock']);
74+
(jasmine as any)['clock'] = function() {
75+
const clock = originalClockFn.apply(this, arguments);
76+
if (!clock[symbol('patched')]) {
77+
clock[symbol('patched')] = symbol('patched');
7478
const originalTick = (clock[symbol('tick')] = clock.tick);
7579
clock.tick = function() {
7680
const fakeAsyncZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec');
@@ -83,28 +87,31 @@
8387
clock.mockDate = function() {
8488
const fakeAsyncZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec');
8589
if (fakeAsyncZoneSpec) {
86-
const dateTime = arguments[0];
90+
const dateTime = arguments.length > 0 ? arguments[0] : new Date();
8791
return fakeAsyncZoneSpec.setCurrentRealTime.apply(
8892
fakeAsyncZoneSpec,
8993
dateTime && typeof dateTime.getTime === 'function' ? [dateTime.getTime()] :
9094
arguments);
9195
}
9296
return originalMockDate.apply(this, arguments);
9397
};
94-
['install', 'uninstall'].forEach(methodName => {
95-
const originalClockFn: Function = (clock[symbol(methodName)] = clock[methodName]);
96-
clock[methodName] = function() {
97-
const FakeAsyncTestZoneSpec = (Zone as any)['FakeAsyncTestZoneSpec'];
98-
if (FakeAsyncTestZoneSpec) {
99-
(jasmine as any)[symbol('clockInstalled')] = 'install' === methodName;
100-
return;
101-
}
102-
return originalClockFn.apply(this, arguments);
103-
};
104-
});
105-
return clock;
106-
};
107-
}
98+
// for auto go into fakeAsync feature, we need the flag to enable it
99+
if (enableClockPatch) {
100+
['install', 'uninstall'].forEach(methodName => {
101+
const originalClockFn: Function = (clock[symbol(methodName)] = clock[methodName]);
102+
clock[methodName] = function() {
103+
const FakeAsyncTestZoneSpec = (Zone as any)['FakeAsyncTestZoneSpec'];
104+
if (FakeAsyncTestZoneSpec) {
105+
(jasmine as any)[symbol('clockInstalled')] = 'install' === methodName;
106+
return;
107+
}
108+
return originalClockFn.apply(this, arguments);
109+
};
110+
});
111+
}
112+
}
113+
return clock;
114+
};
108115

109116
/**
110117
* Gets a function wrapping the body of a Jasmine `describe` block to execute in a

Diff for: lib/zone-spec/fake-async-test.ts

+36-12
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,14 @@
3232
const OriginalDate = global.Date;
3333
class FakeDate {
3434
constructor() {
35-
const d = new OriginalDate();
36-
d.setTime(global.Date.now());
37-
return d;
38-
}
39-
40-
static UTC() {
41-
return OriginalDate.UTC();
35+
if (arguments.length === 0) {
36+
const d = new OriginalDate();
37+
d.setTime(FakeDate.now());
38+
return d;
39+
} else {
40+
const args = Array.prototype.slice.call(arguments);
41+
return new OriginalDate(...args);
42+
}
4243
}
4344

4445
static now() {
@@ -48,12 +49,19 @@
4849
}
4950
return OriginalDate.now.apply(this, arguments);
5051
}
51-
52-
static parse() {
53-
return OriginalDate.parse();
54-
}
5552
}
5653

54+
(FakeDate as any).UTC = OriginalDate.UTC;
55+
(FakeDate as any).parse = OriginalDate.parse;
56+
57+
// keep a reference for zone patched timer function
58+
const timers = {
59+
setTimeout: global.setTimeout,
60+
setInterval: global.setInterval,
61+
clearTimeout: global.clearTimeout,
62+
clearInterval: global.clearInterval
63+
};
64+
5765
class Scheduler {
5866
// Next scheduler id.
5967
public nextId: number = 1;
@@ -63,7 +71,7 @@
6371
// Current simulated time in millis.
6472
private _currentTime: number = 0;
6573
// Current real time in millis.
66-
private _currentRealTime: number = Date.now();
74+
private _currentRealTime: number = OriginalDate.now();
6775

6876
constructor() {}
6977

@@ -341,6 +349,11 @@
341349
}
342350
global['Date'] = FakeDate;
343351
FakeDate.prototype = OriginalDate.prototype;
352+
353+
// try check and reset timers
354+
// because jasmine.clock().install() may
355+
// have replaced the global timer
356+
FakeAsyncTestZoneSpec.checkTimerPatch();
344357
}
345358

346359
static resetDate() {
@@ -349,6 +362,17 @@
349362
}
350363
}
351364

365+
static checkTimerPatch() {
366+
if (global.setTimeout !== timers.setTimeout) {
367+
global.setTimeout = timers.setTimeout;
368+
global.clearTimeout = timers.clearTimeout;
369+
}
370+
if (global.setInterval !== timers.setInterval) {
371+
global.setInterval = timers.setInterval;
372+
global.clearInterval = timers.clearInterval;
373+
}
374+
}
375+
352376
lockDatePatch() {
353377
this.patchDateLocked = true;
354378
FakeAsyncTestZoneSpec.patchDate();

Diff for: test/test-env-setup-mocha.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ declare const global: any;
8181
throw new Error(`Expected ${expected} to be greater than ${actual}`);
8282
}
8383
},
84+
toBeLessThan: function(actual: number) {
85+
if (expected >= actual) {
86+
throw new Error(`Expected ${expected} to be lesser than ${actual}`);
87+
}
88+
},
8489
toBeDefined: function() {
8590
if (!expected) {
8691
throw new Error(`Expected ${expected} to be defined`);
@@ -109,6 +114,11 @@ declare const global: any;
109114
throw new Error(`Expected ${expected} to be truthy`);
110115
}
111116
},
117+
toBeFalsy: function(actual: any) {
118+
if (!!actual) {
119+
throw new Error(`Expected ${actual} to be falsy`);
120+
}
121+
},
112122
toContain: function(actual: any) {
113123
if (expected.indexOf(actual) === -1) {
114124
throw new Error(`Expected ${expected} to contain ${actual}`);
@@ -159,7 +169,11 @@ declare const global: any;
159169
if (expected > actual) {
160170
throw new Error(`Expected ${expected} not to be greater than ${actual}`);
161171
}
162-
172+
},
173+
toBeLessThan: function(actual: number) {
174+
if (expected < actual) {
175+
throw new Error(`Expected ${expected} not to be lesser than ${actual}`);
176+
}
163177
},
164178
toHaveBeenCalledWith: function(params: any[]) {
165179
if (!eq(expected.callArgs, params)) {

0 commit comments

Comments
 (0)