Skip to content
Closed
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ const { codes: {
ERR_INVALID_ARG_VALUE,
ERR_INVALID_RETURN_VALUE
} } = require('internal/errors');
const { AssertionError, errorCache } = require('internal/assert');
const { AssertionError } = require('internal/assert');
const { openSync, closeSync, readSync } = require('fs');
const { inspect, types: { isPromise, isRegExp } } = require('util');
const { EOL } = require('internal/constants');
const { NativeModule } = require('internal/bootstrap/loaders');

const errorCache = new Map();

let isDeepEqual;
let isDeepStrictEqual;
let parseExpressionAt;
Expand Down
3 changes: 1 addition & 2 deletions lib/internal/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,5 @@ class AssertionError extends Error {
}

module.exports = {
AssertionError,
errorCache: new Map()
AssertionError
};
53 changes: 25 additions & 28 deletions test/parallel/test-assert-builtins-not-read-from-filesystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,44 @@

require('../common');
const assert = require('assert');
const EventEmitter = require('events');
const e = new EventEmitter();
e.on('hello', assert);

if (process.argv[2] !== 'child') {
const tmpdir = require('../common/tmpdir');
tmpdir.refresh();
const { spawnSync } = require('child_process');
const { output, status, error } =
spawnSync(process.execPath,
['--expose-internals', process.argv[1], 'child'],
{ cwd: tmpdir.path, env: process.env });
assert.ifError(error);
assert.strictEqual(status, 0, `Exit code: ${status}\n${output}`);
} else {
const EventEmitter = require('events');
const { errorCache } = require('internal/assert');
const { writeFileSync } = require('fs');
const e = new EventEmitter();

e.on('hello', assert);

let threw = false;
try {
e.emit('hello', false);
} catch (err) {
const frames = err.stack.split('\n');
const [, filename, line, column] = frames[1].match(/\((.+):(\d+):(\d+)\)/);
// Reset the cache to check again
const size = errorCache.size;
errorCache.delete(`${filename}${line - 1}${column - 1}`);
assert.strictEqual(errorCache.size, size - 1);
const data = `${'\n'.repeat(line - 1)}${' '.repeat(column - 1)}` +
'ok(failed(badly));';
// Spawn a child process to avoid the error having been cached in the assert
// module's `errorCache` Map.

writeFileSync(filename, data);
assert.throws(
() => e.emit('hello', false),
{
message: 'false == true'
}
);
const { output, status, error } =
spawnSync(process.execPath,
[process.argv[1], 'child', filename, line, column],
{ cwd: tmpdir.path, env: process.env });
assert.ifError(error);
assert.strictEqual(status, 0, `Exit code: ${status}\n${output}`);
threw = true;

}
assert(threw);
assert.ok(threw);
} else {
const { writeFileSync } = require('fs');
const [, , , filename, line, column] = process.argv;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I somehow fail to see how the line and column is passed through to the child.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ugh! That's because it's not! Fixed! Thanks.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(See line 28. I had line, column missing before.)

const data = `${'\n'.repeat(line - 1)}${' '.repeat(column - 1)}` +
'ok(failed(badly));';

writeFileSync(filename, data);
assert.throws(
() => e.emit('hello', false),
{
message: 'false == true'
}
);
}