Skip to content

Commit 284f652

Browse files
authored
fix(ext/node): fix assertion error message of assert.ok (denoland#29803)
1 parent 36ed70f commit 284f652

File tree

5 files changed

+69
-9
lines changed

5 files changed

+69
-9
lines changed

ext/node/polyfills/assert.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,10 @@ function toNode(
6565
expected: unknown;
6666
message?: string | Error;
6767
operator?: string;
68+
stackStartFn?: Function;
6869
},
6970
) {
70-
const { operator, message, actual, expected } = opts || {};
71+
const { operator, message, actual, expected, stackStartFn } = opts || {};
7172
try {
7273
fn();
7374
} catch (e) {
@@ -78,6 +79,7 @@ function toNode(
7879
message,
7980
actual,
8081
expected,
82+
stackStartFn,
8183
});
8284
} else if (message instanceof Error) {
8385
throw message;
@@ -87,6 +89,7 @@ function toNode(
8789
message: e.message,
8890
actual,
8991
expected,
92+
stackStartFn,
9093
});
9194
}
9295
}
@@ -100,10 +103,11 @@ function assert(actual: unknown, message?: string | Error): asserts actual {
100103
message: "No value argument passed to `assert.ok()`",
101104
});
102105
}
103-
toNode(
104-
() => asserts.assert(actual),
105-
{ message, actual, expected: true },
106-
);
106+
if (actual) {
107+
return;
108+
}
109+
110+
equal(actual, true, message);
107111
}
108112
const ok = assert;
109113

@@ -272,6 +276,7 @@ function equal(
272276
operator: "==",
273277
actual,
274278
expected,
279+
stackStartFn: equal,
275280
},
276281
);
277282
}

tests/node_compat/config.jsonc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@
244244
"parallel": [
245245
"test-arm-math-illegal-instruction.js",
246246
"test-assert-async.js",
247+
"test-assert-builtins-not-read-from-filesystem.js",
247248
"test-assert-fail.js",
248249
"test-assert.js",
249250
"test-async-hooks-run-in-async-scope-caught-exception.js",

tests/node_compat/runner/TODO.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,6 @@ NOTE: This file should not be manually edited. Please edit `tests/node_compat/co
220220
- [parallel/test-aborted-util.js](https://github.com/nodejs/node/tree/v23.9.0/test/parallel/test-aborted-util.js)
221221
- [parallel/test-abortsignal-cloneable.js](https://github.com/nodejs/node/tree/v23.9.0/test/parallel/test-abortsignal-cloneable.js)
222222
- [parallel/test-accessor-properties.js](https://github.com/nodejs/node/tree/v23.9.0/test/parallel/test-accessor-properties.js)
223-
- [parallel/test-assert-builtins-not-read-from-filesystem.js](https://github.com/nodejs/node/tree/v23.9.0/test/parallel/test-assert-builtins-not-read-from-filesystem.js)
224223
- [parallel/test-assert-calltracker-calls.js](https://github.com/nodejs/node/tree/v23.9.0/test/parallel/test-assert-calltracker-calls.js)
225224
- [parallel/test-assert-calltracker-getCalls.js](https://github.com/nodejs/node/tree/v23.9.0/test/parallel/test-assert-calltracker-getCalls.js)
226225
- [parallel/test-assert-calltracker-report.js](https://github.com/nodejs/node/tree/v23.9.0/test/parallel/test-assert-calltracker-report.js)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// deno-fmt-ignore-file
2+
// deno-lint-ignore-file
3+
4+
// Copyright Joyent and Node contributors. All rights reserved. MIT license.
5+
// Taken from Node 23.9.0
6+
// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually.
7+
8+
'use strict';
9+
10+
// Do not read filesystem when creating AssertionError messages for code in
11+
// builtin modules.
12+
13+
require('../common');
14+
const assert = require('assert');
15+
const EventEmitter = require('events');
16+
const e = new EventEmitter();
17+
e.on('hello', assert);
18+
19+
if (process.argv[2] !== 'child') {
20+
const tmpdir = require('../common/tmpdir');
21+
tmpdir.refresh();
22+
const { spawnSync } = require('child_process');
23+
24+
let threw = false;
25+
try {
26+
e.emit('hello', false);
27+
} catch (err) {
28+
const frames = err.stack.split('\n');
29+
const [, filename, line, column] = frames[1].match(/\((.+):(\d+):(\d+)\)/);
30+
// Spawn a child process to avoid the error having been cached in the assert
31+
// module's `errorCache` Map.
32+
33+
const { output, status, error } =
34+
spawnSync(process.execPath,
35+
[process.argv[1], 'child', filename, line, column],
36+
{ cwd: tmpdir.path, env: process.env });
37+
assert.ifError(error);
38+
assert.strictEqual(status, 0, `Exit code: ${status}\n${output}`);
39+
threw = true;
40+
}
41+
assert.ok(threw);
42+
} else {
43+
const { writeFileSync } = require('fs');
44+
const [, , , filename, line, column] = process.argv;
45+
const data = `${'\n'.repeat(line - 1)}${' '.repeat(column - 1)}` +
46+
'ok(failed(badly));';
47+
48+
writeFileSync(filename, data);
49+
assert.throws(
50+
() => e.emit('hello', false),
51+
{
52+
message: 'false == true'
53+
}
54+
);
55+
}

tests/node_compat/test/parallel/test-assert.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ assert.throws(
457457
testBlockTypeError(assert.doesNotThrow, undefined);
458458
}
459459

460-
// https://github.com/nodejs/node/issues/3275
460+
// https://github.com/nodejs/node/issues/3277765
461461
assert.throws(() => { throw 'error'; }, (err) => err === 'error');
462462
assert.throws(() => { throw new Error(); }, (err) => err instanceof Error);
463463

@@ -778,8 +778,8 @@ assert.throws(
778778
{
779779
code: 'ERR_ASSERTION',
780780
constructor: assert.AssertionError,
781-
generatedMessage: true,
782781
/* TODO(kt3k): Enable this assertion
782+
generatedMessage: true,
783783
message: 'The expression evaluated to a falsy value:\n\n ' +
784784
'assert.ok(null)\n'
785785
*/
@@ -790,8 +790,8 @@ assert.throws(
790790
{
791791
code: 'ERR_ASSERTION',
792792
constructor: assert.AssertionError,
793-
generatedMessage: true,
794793
/* TODO(kt3k): Enable this assertion
794+
generatedMessage: true,
795795
message: 'The expression evaluated to a falsy value:\n\n ' +
796796
"assert(typeof 123n === 'string')\n"
797797
*/

0 commit comments

Comments
 (0)