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

Commit

Permalink
fix(console): console.log in nodejs should run in root Zone (#855)
Browse files Browse the repository at this point in the history
  • Loading branch information
JiaLiPassion authored and mhevery committed Jul 27, 2017
1 parent 5c5ca1a commit 5900d3a
Showing 3 changed files with 60 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/node/node.ts
Original file line number Diff line number Diff line change
@@ -134,3 +134,21 @@ Zone.__load_patch('crypto', (global: any, Zone: ZoneType, api: _ZonePrivate) =>
});
}
});

Zone.__load_patch('console', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
const consoleMethods =
['dir', 'log', 'info', 'error', 'warn', 'assert', 'debug', 'timeEnd', 'trace'];
consoleMethods.forEach((m: string) => {
const originalMethod = (console as any)[Zone.__symbol__(m)] = (console as any)[m];
if (originalMethod) {
(console as any)[m] = function() {
const args = Array.prototype.slice.call(arguments);
if (Zone.current === Zone.root) {
return originalMethod.apply(this, args);
} else {
return Zone.root.run(originalMethod, this, args);
}
};
}
});
});
41 changes: 41 additions & 0 deletions test/node/console.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
describe('node console', () => {
const log: string[] = [];
const zone = Zone.current.fork({
name: 'console',
onScheduleTask: function(
delegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, task: Task) {
log.push(task.source);
return delegate.scheduleTask(targetZone, task);
}
});

beforeEach(() => {
log.length = 0;
});

it('console methods should run in root zone', () => {
zone.run(() => {
console.log('test');
console.warn('test');
console.error('test');
console.info('test');
console.trace('test');
try {
console.assert(false, 'test');
} catch (error) {
}
console.dir('.');
console.time('start');
console.timeEnd('start');
console.debug && console.debug('test');
});
expect(log).toEqual([]);
});
});
1 change: 1 addition & 0 deletions test/node_tests.ts
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@ import './node/process.spec';
import './node/Error.spec';
import './node/crypto.spec';
import './node/http.spec';
import './node/console.spec';

// before test bluebird, must run npm install bluebird first.
// then remove the comment below

0 comments on commit 5900d3a

Please sign in to comment.