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

Commit

Permalink
test(node): add task to run a subset of tests on node
Browse files Browse the repository at this point in the history
`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.
  • Loading branch information
juliemr committed Apr 12, 2016
1 parent 2063814 commit 89530fe
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 111 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ before_script:

script:
- node_modules/.bin/karma start karma-sauce.conf.js

- node_modules/.bin/gulp test/node
6 changes: 5 additions & 1 deletion DEVELOPER.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ In a separate process, run the WebSockets server:

`npm run ws-server`

Run the tests using Karma:
Run the browser tests using Karma:

`npm test`

Run the node.js tests:

`npm run test-node`
44 changes: 37 additions & 7 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,19 @@ function generateBrowserScript(inFile, outFile, minify, callback) {
});
}

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

gulp.task('build/zone.js.d.ts', ['compile'], function() {
Expand Down Expand Up @@ -121,3 +127,27 @@ gulp.task('build', [
'build/async-test.js',
'build/sync-test.js'
]);

gulp.task('test/node', ['compile'], function(cb) {
var JasmineRunner = require('jasmine');
var jrunner = new JasmineRunner();

var specFiles = ['build/test/node_entry_point.js'];

jrunner.configureDefaultReporter({showColors: true});

jrunner.onComplete(function(passed) {
if (!passed) {
var err = new Error('Jasmine node tests failed.');
// The stack is not useful in this context.
err.showStack = false;
cb(err);
} else {
cb();
}
});
jrunner.projectBaseDir = __dirname;
jrunner.specDir = '';
jrunner.addSpecFiles(specFiles);
jrunner.execute();
});
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"tsc": "./node_modules/.bin/tsc",
"tsc:w": "./node_modules/.bin/tsc -w",
"test": "karma start karma.conf.js",
"test-node": "./node_modules/.bin/gulp test/node",
"serve": "python -m SimpleHTTPServer 8000"
},
"repository": {
Expand All @@ -39,6 +40,7 @@
"gulp-tsc": "^1.1.4",
"gulp-uglify": "^1.2.0",
"gulp-util": "^3.0.7",
"jasmine": "^2.4.1",
"jasmine-core": "^2.2.0",
"karma": "^0.13.14",
"karma-chrome-launcher": "^0.2.1",
Expand Down
3 changes: 0 additions & 3 deletions source_map_test.html

This file was deleted.

99 changes: 99 additions & 0 deletions test/browser/browser.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
describe('Zone', function () {
var rootZone = Zone.current;

describe('hooks', function () {
it('should allow you to override alert/prompt/confirm', function () {
var alertSpy = jasmine.createSpy('alert');
var promptSpy = jasmine.createSpy('prompt');
var confirmSpy = jasmine.createSpy('confirm');
var spies = {
'alert': alertSpy,
'prompt': promptSpy,
'confirm': confirmSpy
};
var myZone = Zone.current.fork({
name: 'spy',
onInvoke: (parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone,
callback: Function, applyThis: any, applyArgs: any[], source: string): any =>
{
if (source) {
spies[source].apply(null, applyArgs);
} else {
return parentZoneDelegate.invoke(targetZone, callback, applyThis, applyArgs, source);
}
}
});

myZone.run(function () {
alert('alertMsg');
prompt('promptMsg', 'default');
confirm('confirmMsg');
});

expect(alertSpy).toHaveBeenCalledWith('alertMsg');
expect(promptSpy).toHaveBeenCalledWith('promptMsg', 'default');
expect(confirmSpy).toHaveBeenCalledWith('confirmMsg');
});

describe('eventListener hooks', function () {
var button;
var clickEvent;

beforeEach(function () {
button = document.createElement('button');
clickEvent = document.createEvent('Event');
clickEvent.initEvent('click', true, true);
document.body.appendChild(button);
});

afterEach(function () {
document.body.removeChild(button);
});

it('should support addEventListener', function () {
var hookSpy = jasmine.createSpy('hook');
var eventListenerSpy = jasmine.createSpy('eventListener');
var zone = rootZone.fork({
name: 'spy',
onScheduleTask: (parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone,
task: Task): any => {
hookSpy();
return parentZoneDelegate.scheduleTask(targetZone, task);
}
});

zone.run(function() {
button.addEventListener('click', eventListenerSpy);
});

button.dispatchEvent(clickEvent);

expect(hookSpy).toHaveBeenCalled();
expect(eventListenerSpy).toHaveBeenCalled();
});

it('should support removeEventListener', function () {
var hookSpy = jasmine.createSpy('hook');
var eventListenerSpy = jasmine.createSpy('eventListener');
var zone = rootZone.fork({
name: 'spy',
onCancelTask: (parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone,
task: Task): any => {
hookSpy();
return parentZoneDelegate.cancelTask(targetZone, task);
}
});

zone.run(function() {
button.addEventListener('click', eventListenerSpy);
button.removeEventListener('click', eventListenerSpy);
});

button.dispatchEvent(clickEvent);

expect(hookSpy).toHaveBeenCalled();
expect(eventListenerSpy).not.toHaveBeenCalled();
});
});
});
});
5 changes: 2 additions & 3 deletions test/browser_entry_point.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ import '../lib/zone-spec/wtf';
import './test-env-setup';

// List all tests here:
import './common/microtasks.spec';
import './common/zone.spec';
import './common/util.spec';
import './common_tests';
import './browser/browser.spec';
import './browser/element.spec';
import './browser/FileReader.spec';
import './browser/HTMLImports.spec';
Expand Down
96 changes: 0 additions & 96 deletions test/common/zone.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,104 +31,8 @@ describe('Zone', function () {

expect(errorSpy).toHaveBeenCalled();
});


it('should allow you to override alert/prompt/confirm', function () {
var alertSpy = jasmine.createSpy('alert');
var promptSpy = jasmine.createSpy('prompt');
var confirmSpy = jasmine.createSpy('confirm');
var spies = {
'alert': alertSpy,
'prompt': promptSpy,
'confirm': confirmSpy
};
var myZone = Zone.current.fork({
name: 'spy',
onInvoke: (parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone,
callback: Function, applyThis: any, applyArgs: any[], source: string): any =>
{
if (source) {
spies[source].apply(null, applyArgs);
} else {
return parentZoneDelegate.invoke(targetZone, callback, applyThis, applyArgs, source);
}
}
});

myZone.run(function () {
alert('alertMsg');
prompt('promptMsg', 'default');
confirm('confirmMsg');
});

expect(alertSpy).toHaveBeenCalledWith('alertMsg');
expect(promptSpy).toHaveBeenCalledWith('promptMsg', 'default');
expect(confirmSpy).toHaveBeenCalledWith('confirmMsg');
});

describe('eventListener hooks', function () {
var button;
var clickEvent;

beforeEach(function () {
button = document.createElement('button');
clickEvent = document.createEvent('Event');
clickEvent.initEvent('click', true, true);
document.body.appendChild(button);
});

afterEach(function () {
document.body.removeChild(button);
});

it('should support addEventListener', function () {
var hookSpy = jasmine.createSpy('hook');
var eventListenerSpy = jasmine.createSpy('eventListener');
var zone = rootZone.fork({
name: 'spy',
onScheduleTask: (parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone,
task: Task): any => {
hookSpy();
return parentZoneDelegate.scheduleTask(targetZone, task);
}
});

zone.run(function() {
button.addEventListener('click', eventListenerSpy);
});

button.dispatchEvent(clickEvent);

expect(hookSpy).toHaveBeenCalled();
expect(eventListenerSpy).toHaveBeenCalled();
});

it('should support removeEventListener', function () {
var hookSpy = jasmine.createSpy('hook');
var eventListenerSpy = jasmine.createSpy('eventListener');
var zone = rootZone.fork({
name: 'spy',
onCancelTask: (parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone,
task: Task): any => {
hookSpy();
return parentZoneDelegate.cancelTask(targetZone, task);
}
});

zone.run(function() {
button.addEventListener('click', eventListenerSpy);
button.removeEventListener('click', eventListenerSpy);
});

button.dispatchEvent(clickEvent);

expect(hookSpy).toHaveBeenCalled();
expect(eventListenerSpy).not.toHaveBeenCalled();
});
});
});


it('should allow zones to be run from within another zone', function () {
var zoneA = Zone.current.fork({ name: 'A' });
var zoneB = Zone.current.fork({ name: 'B' });
Expand Down
3 changes: 3 additions & 0 deletions test/common_tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import './common/microtasks.spec';
import './common/zone.spec';
import './common/util.spec';
11 changes: 11 additions & 0 deletions test/node_entry_point.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Setup tests for Zone without microtask support
import '../lib/zone';
import '../lib/node/node';
import '../lib/zone-spec/long-stack-trace';

// Setup test environment
import './test-env-setup';

// List all tests here:
import './common_tests';
import './zone-spec/long-stack-trace-zone.spec';

0 comments on commit 89530fe

Please sign in to comment.