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

Commit e13bb93

Browse files
committed
fix(console): console.log in nodejs should run in root Zone
1 parent ccf002d commit e13bb93

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

lib/node/node.ts

+14
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,17 @@ Zone.__load_patch('crypto', (global: any, Zone: ZoneType, api: _ZonePrivate) =>
134134
});
135135
}
136136
});
137+
138+
Zone.__load_patch('console', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
139+
const consoleMethods =
140+
['dir', 'log', 'info', 'error', 'warn', 'assert', 'debug', 'timeEnd', 'trace'];
141+
consoleMethods.forEach((m: string) => {
142+
const originalMethod = (console as any)[m];
143+
if (originalMethod) {
144+
(console as any)[m] = function() {
145+
const args = Array.prototype.slice.call(arguments);
146+
return Zone.root.run(originalMethod, this, args);
147+
};
148+
}
149+
});
150+
});

test/node/console.spec.ts

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
describe('node console', () => {
9+
const log: string[] = [];
10+
const zone = Zone.current.fork({
11+
name: 'console',
12+
onScheduleTask: function(
13+
delegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, task: Task) {
14+
log.push(task.source);
15+
return delegate.scheduleTask(targetZone, task);
16+
}
17+
});
18+
19+
beforeEach(() => {
20+
log.length = 0;
21+
});
22+
23+
it('console methods should run in root zone', () => {
24+
zone.run(() => {
25+
console.log('test');
26+
console.warn('test');
27+
console.error('test');
28+
console.info('test');
29+
console.trace('test');
30+
try {
31+
console.assert(false, 'test');
32+
} catch (error) {
33+
}
34+
console.dir('.');
35+
console.time('start');
36+
console.timeEnd('end');
37+
console.debug && console.debug('test');
38+
});
39+
expect(log).toEqual([]);
40+
});
41+
});

test/node_tests.ts

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import './node/process.spec';
1212
import './node/Error.spec';
1313
import './node/crypto.spec';
1414
import './node/http.spec';
15+
import './node/console.spec';
1516

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

0 commit comments

Comments
 (0)