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

Commit 0a6a434

Browse files
vikermanmhevery
authored andcommitted
feat(zonespec): add a spec for synchronous tests
Will throw an exception if any asynchornous microtask or macrotask is scheduled. Allows event tasks to be scheduled.
1 parent f3f8628 commit 0a6a434

File tree

4 files changed

+83
-1
lines changed

4 files changed

+83
-1
lines changed

Diff for: gulpfile.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ gulp.task('build/async-test.js', function(cb) {
102102
return generateBrowserScript('./lib/zone-spec/async-test.ts', 'async-test.js', false, cb);
103103
});
104104

105+
gulp.task('build/sync-test.js', function(cb) {
106+
return generateBrowserScript('./lib/zone-spec/sync-test.ts', 'sync-test.js', false, cb);
107+
});
108+
105109
gulp.task('build', [
106110
'build/zone.js',
107111
'build/zone.js.d.ts',
@@ -113,7 +117,8 @@ gulp.task('build', [
113117
'build/long-stack-trace-zone.min.js',
114118
'build/wtf.js',
115119
'build/wtf.min.js',
116-
'build/async-test.js'
120+
'build/async-test.js',
121+
'build/sync-test.js'
117122
]);
118123

119124

Diff for: lib/zone-spec/sync-test.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
(function() {
2+
class SyncTestZoneSpec implements ZoneSpec {
3+
runZone = Zone.current;
4+
5+
constructor(namePrefix: string) {
6+
this.name = 'syncTestZone for ' + namePrefix;
7+
}
8+
9+
// ZoneSpec implementation below.
10+
11+
name: string;
12+
13+
onScheduleTask(delegate: ZoneDelegate, current: Zone, target: Zone, task: Task): Task {
14+
switch (task.type) {
15+
case 'microTask':
16+
case 'macroTask':
17+
throw new Error(`Cannot call ${task.source} from within a sync test.`);
18+
case 'eventTask':
19+
task = delegate.scheduleTask(target, task);
20+
break;
21+
}
22+
return task;
23+
}
24+
}
25+
26+
// Export the class so that new instances can be created with proper
27+
// constructor params.
28+
Zone['SyncTestZoneSpec'] = SyncTestZoneSpec;
29+
})();

Diff for: test/browser_entry_point.ts

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import './test-env-setup';
1313
// List all tests here:
1414
import './long-stack-trace-zone.spec';
1515
import './async-test.spec';
16+
import './sync-test.spec';
1617
import './microtasks.spec';
1718
import './zone.spec';
1819
import './integration/brick.spec';

Diff for: test/sync-test.spec.ts

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import '../lib/zone-spec/sync-test';
2+
3+
describe('SyncTestZoneSpec', () => {
4+
var SyncTestZoneSpec = Zone['SyncTestZoneSpec'];
5+
var testZoneSpec;
6+
var syncTestZone;
7+
8+
beforeEach(() => {
9+
testZoneSpec = new SyncTestZoneSpec('name');
10+
syncTestZone = Zone.current.fork(testZoneSpec);
11+
});
12+
13+
it('should fail on Promise.then', () => {
14+
syncTestZone.run(() => {
15+
expect(() => { Promise.resolve().then(function() {}); })
16+
.toThrow(new Error("Cannot call Promise.then from within a sync test."));
17+
});
18+
});
19+
20+
it('should fail on setTimeout', () => {
21+
syncTestZone.run(() => {
22+
expect(() => { setTimeout(() => { }, 100); })
23+
.toThrow(new Error("Cannot call setTimeout from within a sync test."));
24+
});
25+
});
26+
27+
it('should work with event tasks', () => {
28+
syncTestZone.run(() => {
29+
var button = document.createElement('button');
30+
document.body.appendChild(button);
31+
var x = 1;
32+
try {
33+
button.addEventListener('click', () => { x++; });
34+
35+
button.click();
36+
expect(x).toEqual(2);
37+
38+
button.click();
39+
expect(x).toEqual(3);
40+
} finally {
41+
document.body.removeChild(button);
42+
}
43+
});
44+
});
45+
});
46+
47+
export var __something__;

0 commit comments

Comments
 (0)