Skip to content

Commit df54db6

Browse files
Trotttargos
authored andcommitted
test: remove internal errorCache property
The internal `assert` modules `errorCache` property is exposed only for testing. The one test that used it is rewritten here to not use it. This has the following advantages: * The test now makes sure that there is an empty cache in a more robust way. Instead of relying on the internal implementation of `errorCache`, it simply spawns a separate process. * One less test using the `--expose-internals` flag. PR-URL: #23304 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]>
1 parent 5ff4300 commit df54db6

File tree

1 file changed

+26
-29
lines changed

1 file changed

+26
-29
lines changed

test/parallel/test-assert-builtins-not-read-from-filesystem.js

+26-29
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,44 @@
55

66
require('../common');
77
const assert = require('assert');
8+
const EventEmitter = require('events');
9+
const e = new EventEmitter();
10+
e.on('hello', assert);
811

912
if (process.argv[2] !== 'child') {
1013
const tmpdir = require('../common/tmpdir');
1114
tmpdir.refresh();
1215
const { spawnSync } = require('child_process');
13-
const { output, status, error } =
14-
spawnSync(process.execPath,
15-
['--expose-internals', process.argv[1], 'child'],
16-
{ cwd: tmpdir.path, env: process.env });
17-
assert.ifError(error);
18-
assert.strictEqual(status, 0, `Exit code: ${status}\n${output}`);
19-
} else {
20-
const EventEmitter = require('events');
21-
const { errorCache } = require('internal/assert');
22-
const { writeFileSync } = require('fs');
23-
const e = new EventEmitter();
24-
25-
e.on('hello', assert);
2616

2717
let threw = false;
2818
try {
2919
e.emit('hello', false);
3020
} catch (err) {
3121
const frames = err.stack.split('\n');
32-
const [, filename, line, column] = frames[1].match(/\((.+):(\d+):(\d+)\)/);
33-
// Reset the cache to check again
34-
const size = errorCache.size;
35-
errorCache.delete(`${filename}${line - 1}${column - 1}`);
36-
assert.strictEqual(errorCache.size, size - 1);
37-
const data = `${'\n'.repeat(line - 1)}${' '.repeat(column - 1)}` +
38-
'ok(failed(badly));';
22+
const [, filename, , ] = 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.
3925

40-
writeFileSync(filename, data);
41-
assert.throws(
42-
() => e.emit('hello', false),
43-
{
44-
message: 'false == true'
45-
}
46-
);
26+
const { output, status, error } =
27+
spawnSync(process.execPath,
28+
[process.argv[1], 'child', filename],
29+
{ cwd: tmpdir.path, env: process.env });
30+
assert.ifError(error);
31+
assert.strictEqual(status, 0, `Exit code: ${status}\n${output}`);
4732
threw = true;
48-
4933
}
50-
assert(threw);
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+
);
5148
}

0 commit comments

Comments
 (0)