Skip to content

Commit 89530fe

Browse files
committed
test(node): add task to run a subset of tests on node
`npm run test-node` or `gulp test/node` will now run tests on node.js. These are also run on TravisCI. Clean up gulp build.
1 parent 2063814 commit 89530fe

10 files changed

+160
-111
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ before_script:
2424

2525
script:
2626
- node_modules/.bin/karma start karma-sauce.conf.js
27-
27+
- node_modules/.bin/gulp test/node

DEVELOPER.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ In a separate process, run the WebSockets server:
1616

1717
`npm run ws-server`
1818

19-
Run the tests using Karma:
19+
Run the browser tests using Karma:
2020

2121
`npm test`
22+
23+
Run the node.js tests:
24+
25+
`npm run test-node`

gulpfile.js

+37-7
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,19 @@ function generateBrowserScript(inFile, outFile, minify, callback) {
4848
});
4949
}
5050

51-
gulp.task('compile', function(){
52-
gulp.src([
53-
'typings/browser/ambient/node/index.d.ts',
54-
'typings/browser/ambient/es6-promise/index.d.ts',
55-
'lib/utils.ts',
56-
'lib/zone.ts',
57-
]).pipe(typescript({ target: 'es5', "declaration": true })).pipe(gulp.dest('./build/'))
51+
// This is equivalent to `npm run tsc`.
52+
gulp.task('compile', function(cb) {
53+
var spawn = require('child_process').spawn;
54+
spawn('./node_modules/.bin/tsc', {stdio: 'inherit'}).on('close', function(exitCode) {
55+
if (exitCode) {
56+
var err = new Error('TypeScript compiler failed');
57+
// The stack is not useful in this context.
58+
err.showStack = false;
59+
cb(err);
60+
} else {
61+
cb();
62+
}
63+
});
5864
});
5965

6066
gulp.task('build/zone.js.d.ts', ['compile'], function() {
@@ -121,3 +127,27 @@ gulp.task('build', [
121127
'build/async-test.js',
122128
'build/sync-test.js'
123129
]);
130+
131+
gulp.task('test/node', ['compile'], function(cb) {
132+
var JasmineRunner = require('jasmine');
133+
var jrunner = new JasmineRunner();
134+
135+
var specFiles = ['build/test/node_entry_point.js'];
136+
137+
jrunner.configureDefaultReporter({showColors: true});
138+
139+
jrunner.onComplete(function(passed) {
140+
if (!passed) {
141+
var err = new Error('Jasmine node tests failed.');
142+
// The stack is not useful in this context.
143+
err.showStack = false;
144+
cb(err);
145+
} else {
146+
cb();
147+
}
148+
});
149+
jrunner.projectBaseDir = __dirname;
150+
jrunner.specDir = '';
151+
jrunner.addSpecFiles(specFiles);
152+
jrunner.execute();
153+
});

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"tsc": "./node_modules/.bin/tsc",
2121
"tsc:w": "./node_modules/.bin/tsc -w",
2222
"test": "karma start karma.conf.js",
23+
"test-node": "./node_modules/.bin/gulp test/node",
2324
"serve": "python -m SimpleHTTPServer 8000"
2425
},
2526
"repository": {
@@ -39,6 +40,7 @@
3940
"gulp-tsc": "^1.1.4",
4041
"gulp-uglify": "^1.2.0",
4142
"gulp-util": "^3.0.7",
43+
"jasmine": "^2.4.1",
4244
"jasmine-core": "^2.2.0",
4345
"karma": "^0.13.14",
4446
"karma-chrome-launcher": "^0.2.1",

source_map_test.html

-3
This file was deleted.

test/browser/browser.spec.ts

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
describe('Zone', function () {
2+
var rootZone = Zone.current;
3+
4+
describe('hooks', function () {
5+
it('should allow you to override alert/prompt/confirm', function () {
6+
var alertSpy = jasmine.createSpy('alert');
7+
var promptSpy = jasmine.createSpy('prompt');
8+
var confirmSpy = jasmine.createSpy('confirm');
9+
var spies = {
10+
'alert': alertSpy,
11+
'prompt': promptSpy,
12+
'confirm': confirmSpy
13+
};
14+
var myZone = Zone.current.fork({
15+
name: 'spy',
16+
onInvoke: (parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone,
17+
callback: Function, applyThis: any, applyArgs: any[], source: string): any =>
18+
{
19+
if (source) {
20+
spies[source].apply(null, applyArgs);
21+
} else {
22+
return parentZoneDelegate.invoke(targetZone, callback, applyThis, applyArgs, source);
23+
}
24+
}
25+
});
26+
27+
myZone.run(function () {
28+
alert('alertMsg');
29+
prompt('promptMsg', 'default');
30+
confirm('confirmMsg');
31+
});
32+
33+
expect(alertSpy).toHaveBeenCalledWith('alertMsg');
34+
expect(promptSpy).toHaveBeenCalledWith('promptMsg', 'default');
35+
expect(confirmSpy).toHaveBeenCalledWith('confirmMsg');
36+
});
37+
38+
describe('eventListener hooks', function () {
39+
var button;
40+
var clickEvent;
41+
42+
beforeEach(function () {
43+
button = document.createElement('button');
44+
clickEvent = document.createEvent('Event');
45+
clickEvent.initEvent('click', true, true);
46+
document.body.appendChild(button);
47+
});
48+
49+
afterEach(function () {
50+
document.body.removeChild(button);
51+
});
52+
53+
it('should support addEventListener', function () {
54+
var hookSpy = jasmine.createSpy('hook');
55+
var eventListenerSpy = jasmine.createSpy('eventListener');
56+
var zone = rootZone.fork({
57+
name: 'spy',
58+
onScheduleTask: (parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone,
59+
task: Task): any => {
60+
hookSpy();
61+
return parentZoneDelegate.scheduleTask(targetZone, task);
62+
}
63+
});
64+
65+
zone.run(function() {
66+
button.addEventListener('click', eventListenerSpy);
67+
});
68+
69+
button.dispatchEvent(clickEvent);
70+
71+
expect(hookSpy).toHaveBeenCalled();
72+
expect(eventListenerSpy).toHaveBeenCalled();
73+
});
74+
75+
it('should support removeEventListener', function () {
76+
var hookSpy = jasmine.createSpy('hook');
77+
var eventListenerSpy = jasmine.createSpy('eventListener');
78+
var zone = rootZone.fork({
79+
name: 'spy',
80+
onCancelTask: (parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone,
81+
task: Task): any => {
82+
hookSpy();
83+
return parentZoneDelegate.cancelTask(targetZone, task);
84+
}
85+
});
86+
87+
zone.run(function() {
88+
button.addEventListener('click', eventListenerSpy);
89+
button.removeEventListener('click', eventListenerSpy);
90+
});
91+
92+
button.dispatchEvent(clickEvent);
93+
94+
expect(hookSpy).toHaveBeenCalled();
95+
expect(eventListenerSpy).not.toHaveBeenCalled();
96+
});
97+
});
98+
});
99+
});

test/browser_entry_point.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ import '../lib/zone-spec/wtf';
1111
import './test-env-setup';
1212

1313
// List all tests here:
14-
import './common/microtasks.spec';
15-
import './common/zone.spec';
16-
import './common/util.spec';
14+
import './common_tests';
15+
import './browser/browser.spec';
1716
import './browser/element.spec';
1817
import './browser/FileReader.spec';
1918
import './browser/HTMLImports.spec';

test/common/zone.spec.ts

-96
Original file line numberDiff line numberDiff line change
@@ -31,104 +31,8 @@ describe('Zone', function () {
3131

3232
expect(errorSpy).toHaveBeenCalled();
3333
});
34-
35-
36-
it('should allow you to override alert/prompt/confirm', function () {
37-
var alertSpy = jasmine.createSpy('alert');
38-
var promptSpy = jasmine.createSpy('prompt');
39-
var confirmSpy = jasmine.createSpy('confirm');
40-
var spies = {
41-
'alert': alertSpy,
42-
'prompt': promptSpy,
43-
'confirm': confirmSpy
44-
};
45-
var myZone = Zone.current.fork({
46-
name: 'spy',
47-
onInvoke: (parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone,
48-
callback: Function, applyThis: any, applyArgs: any[], source: string): any =>
49-
{
50-
if (source) {
51-
spies[source].apply(null, applyArgs);
52-
} else {
53-
return parentZoneDelegate.invoke(targetZone, callback, applyThis, applyArgs, source);
54-
}
55-
}
56-
});
57-
58-
myZone.run(function () {
59-
alert('alertMsg');
60-
prompt('promptMsg', 'default');
61-
confirm('confirmMsg');
62-
});
63-
64-
expect(alertSpy).toHaveBeenCalledWith('alertMsg');
65-
expect(promptSpy).toHaveBeenCalledWith('promptMsg', 'default');
66-
expect(confirmSpy).toHaveBeenCalledWith('confirmMsg');
67-
});
68-
69-
describe('eventListener hooks', function () {
70-
var button;
71-
var clickEvent;
72-
73-
beforeEach(function () {
74-
button = document.createElement('button');
75-
clickEvent = document.createEvent('Event');
76-
clickEvent.initEvent('click', true, true);
77-
document.body.appendChild(button);
78-
});
79-
80-
afterEach(function () {
81-
document.body.removeChild(button);
82-
});
83-
84-
it('should support addEventListener', function () {
85-
var hookSpy = jasmine.createSpy('hook');
86-
var eventListenerSpy = jasmine.createSpy('eventListener');
87-
var zone = rootZone.fork({
88-
name: 'spy',
89-
onScheduleTask: (parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone,
90-
task: Task): any => {
91-
hookSpy();
92-
return parentZoneDelegate.scheduleTask(targetZone, task);
93-
}
94-
});
95-
96-
zone.run(function() {
97-
button.addEventListener('click', eventListenerSpy);
98-
});
99-
100-
button.dispatchEvent(clickEvent);
101-
102-
expect(hookSpy).toHaveBeenCalled();
103-
expect(eventListenerSpy).toHaveBeenCalled();
104-
});
105-
106-
it('should support removeEventListener', function () {
107-
var hookSpy = jasmine.createSpy('hook');
108-
var eventListenerSpy = jasmine.createSpy('eventListener');
109-
var zone = rootZone.fork({
110-
name: 'spy',
111-
onCancelTask: (parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone,
112-
task: Task): any => {
113-
hookSpy();
114-
return parentZoneDelegate.cancelTask(targetZone, task);
115-
}
116-
});
117-
118-
zone.run(function() {
119-
button.addEventListener('click', eventListenerSpy);
120-
button.removeEventListener('click', eventListenerSpy);
121-
});
122-
123-
button.dispatchEvent(clickEvent);
124-
125-
expect(hookSpy).toHaveBeenCalled();
126-
expect(eventListenerSpy).not.toHaveBeenCalled();
127-
});
128-
});
12934
});
13035

131-
13236
it('should allow zones to be run from within another zone', function () {
13337
var zoneA = Zone.current.fork({ name: 'A' });
13438
var zoneB = Zone.current.fork({ name: 'B' });

test/common_tests.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import './common/microtasks.spec';
2+
import './common/zone.spec';
3+
import './common/util.spec';

test/node_entry_point.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Setup tests for Zone without microtask support
2+
import '../lib/zone';
3+
import '../lib/node/node';
4+
import '../lib/zone-spec/long-stack-trace';
5+
6+
// Setup test environment
7+
import './test-env-setup';
8+
9+
// List all tests here:
10+
import './common_tests';
11+
import './zone-spec/long-stack-trace-zone.spec';

0 commit comments

Comments
 (0)