| 
 | 1 | +'use strict';  | 
 | 2 | + | 
 | 3 | +const common = require('../common');  | 
 | 4 | +const dc = require('diagnostics_channel');  | 
 | 5 | +const assert = require('assert');  | 
 | 6 | + | 
 | 7 | +const channel = dc.tracingChannel('test');  | 
 | 8 | + | 
 | 9 | +const expectedError = new Error('test');  | 
 | 10 | +const input = { foo: 'bar' };  | 
 | 11 | +const thisArg = { baz: 'buz' };  | 
 | 12 | + | 
 | 13 | +process.on('unhandledRejection', common.mustCall((reason) => {  | 
 | 14 | +  assert.deepStrictEqual(reason, expectedError);  | 
 | 15 | +}));  | 
 | 16 | + | 
 | 17 | +function check(found) {  | 
 | 18 | +  assert.deepStrictEqual(found, input);  | 
 | 19 | +}  | 
 | 20 | + | 
 | 21 | +const handlers = {  | 
 | 22 | +  start: common.mustCall(check),  | 
 | 23 | +  end: common.mustCall(check),  | 
 | 24 | +  asyncStart: common.mustCall(check),  | 
 | 25 | +  asyncEnd: common.mustCall(check),  | 
 | 26 | +  error: common.mustCall((found) => {  | 
 | 27 | +    check(found);  | 
 | 28 | +    assert.deepStrictEqual(found.error, expectedError);  | 
 | 29 | +  })  | 
 | 30 | +};  | 
 | 31 | + | 
 | 32 | +channel.subscribe(handlers);  | 
 | 33 | + | 
 | 34 | +// Set no then/catch handler to verify unhandledRejection happens  | 
 | 35 | +channel.tracePromise(function(value) {  | 
 | 36 | +  assert.deepStrictEqual(this, thisArg);  | 
 | 37 | +  return Promise.reject(value);  | 
 | 38 | +}, input, thisArg, expectedError);  | 
0 commit comments