1'use strict'; 2 3// Do not read filesystem when creating AssertionError messages for code in 4// builtin modules. 5 6require('../common'); 7const assert = require('assert'); 8const EventEmitter = require('events'); 9const e = new EventEmitter(); 10e.on('hello', assert); 11 12if (process.argv[2] !== 'child') { 13 const tmpdir = require('../common/tmpdir'); 14 tmpdir.refresh(); 15 const { spawnSync } = require('child_process'); 16 17 let threw = false; 18 try { 19 e.emit('hello', false); 20 } catch (err) { 21 const frames = err.stack.split('\n'); 22 const [, filename, line, column] = frames[1].match(/\((.+):(\d+):(\d+)\)/); 23 // Spawn a child process to avoid the error having been cached in the assert 24 // module's `errorCache` Map. 25 26 const { output, status, error } = 27 spawnSync(process.execPath, 28 [process.argv[1], 'child', filename, line, column], 29 { cwd: tmpdir.path, env: process.env }); 30 assert.ifError(error); 31 assert.strictEqual(status, 0, `Exit code: ${status}\n${output}`); 32 threw = true; 33 } 34 assert.ok(threw); 35} else { 36 const { writeFileSync } = require('fs'); 37 const [, , , filename, line, column] = process.argv; 38 const data = `${'\n'.repeat(line - 1)}${' '.repeat(column - 1)}` + 39 'ok(failed(badly));'; 40 41 writeFileSync(filename, data); 42 assert.throws( 43 () => e.emit('hello', false), 44 { 45 message: 'false == true' 46 } 47 ); 48} 49