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

fix(console): console.log in nodejs should run in root Zone #855

Merged
merged 1 commit into from
Jul 27, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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