|
| 1 | +'use strict'; |
| 2 | +require('../common'); |
| 3 | +const assert = require('assert'); |
| 4 | +const { test } = require('node:test'); |
| 5 | + |
| 6 | +// Disable colored output to prevent color codes from breaking assertion |
| 7 | +// message comparisons. This should only be an issue when process.stdout |
| 8 | +// is a TTY. |
| 9 | +if (process.stdout.isTTY) { |
| 10 | + process.env.NODE_DISABLE_COLORS = '1'; |
| 11 | +} |
| 12 | + |
| 13 | +// Regression tests for https://github.com/nodejs/node/issues/50397. |
| 14 | +// |
| 15 | +// When `assert.deepStrictEqual` fails solely because the two values have |
| 16 | +// different prototypes, the diff used to render as `{} !== {}` (or similar), |
| 17 | +// giving the user no clue about why the assertion failed. The assertion |
| 18 | +// error formatter now surfaces the prototype mismatch explicitly. |
| 19 | + |
| 20 | +test('deepStrictEqual surfaces anonymous-class prototype mismatch', () => { |
| 21 | + const A = (() => class {})(); |
| 22 | + const B = (() => class {})(); |
| 23 | + assert.throws( |
| 24 | + () => assert.deepStrictEqual(new A(), new B()), |
| 25 | + (err) => { |
| 26 | + assert.strictEqual(err.code, 'ERR_ASSERTION'); |
| 27 | + assert.match(err.message, /Object prototypes differ:/); |
| 28 | + // The previous "Values have same structure but are not reference-equal" |
| 29 | + // message must no longer be used for prototype-only mismatches because |
| 30 | + // it is misleading: the values do not even have the same prototype. |
| 31 | + assert.doesNotMatch(err.message, /same structure but are not reference-equal/); |
| 32 | + return true; |
| 33 | + } |
| 34 | + ); |
| 35 | +}); |
| 36 | + |
| 37 | +test('deepStrictEqual surfaces named-class prototype mismatch', () => { |
| 38 | + class Foo {} |
| 39 | + class Bar {} |
| 40 | + assert.throws( |
| 41 | + () => assert.deepStrictEqual(new Foo(), new Bar()), |
| 42 | + (err) => { |
| 43 | + assert.strictEqual(err.code, 'ERR_ASSERTION'); |
| 44 | + // Both class names should appear somewhere in the rendered message: |
| 45 | + // either in the existing inspect-based diff (`+ Foo {}` / `- Bar {}`) |
| 46 | + // or in the new "Object prototypes differ:" line. The important |
| 47 | + // guarantee is that the user can identify both prototypes from the |
| 48 | + // error message alone. |
| 49 | + assert.match(err.message, /Foo/); |
| 50 | + assert.match(err.message, /Bar/); |
| 51 | + return true; |
| 52 | + } |
| 53 | + ); |
| 54 | +}); |
| 55 | + |
| 56 | +test('deepStrictEqual surfaces null-prototype mismatch', () => { |
| 57 | + const a = { __proto__: null }; |
| 58 | + const b = {}; |
| 59 | + assert.throws( |
| 60 | + () => assert.deepStrictEqual(a, b), |
| 61 | + (err) => { |
| 62 | + assert.strictEqual(err.code, 'ERR_ASSERTION'); |
| 63 | + assert.match(err.message, /null prototype/); |
| 64 | + return true; |
| 65 | + } |
| 66 | + ); |
| 67 | +}); |
| 68 | + |
| 69 | +test('deepStrictEqual prototype-mismatch message is helpful for empty objects', () => { |
| 70 | + // This is the most pathological case: both sides inspect identically as |
| 71 | + // `{}`, so without the prototype-mismatch information the diff body alone |
| 72 | + // is useless. The fix must produce an explanatory line. |
| 73 | + const A = (() => class {})(); |
| 74 | + const B = (() => class {})(); |
| 75 | + let captured; |
| 76 | + try { |
| 77 | + assert.deepStrictEqual(new A(), new B()); |
| 78 | + } catch (err) { |
| 79 | + captured = err; |
| 80 | + } |
| 81 | + assert.ok(captured, 'deepStrictEqual should have thrown'); |
| 82 | + assert.match(captured.message, /Object prototypes differ:/); |
| 83 | +}); |
| 84 | + |
| 85 | +test('strictEqual on structurally-equal arrays still uses notIdentical message', () => { |
| 86 | + // Sanity check that the new code path does not regress the existing |
| 87 | + // behavior of `strictEqual([], [])`. That comparison continues to use |
| 88 | + // the "Values have same structure but are not reference-equal" message |
| 89 | + // because the prototypes do match (both are Array.prototype). |
| 90 | + assert.throws( |
| 91 | + () => assert.strictEqual([], []), |
| 92 | + { |
| 93 | + code: 'ERR_ASSERTION', |
| 94 | + message: 'Values have same structure but are not reference-equal:\n\n[]\n', |
| 95 | + } |
| 96 | + ); |
| 97 | +}); |
| 98 | + |
| 99 | +test('deepStrictEqual on structurally-equal values with same prototype still fails clearly', () => { |
| 100 | + // When the prototypes match but the values are not reference-equal, the |
| 101 | + // existing notIdentical fallback should still apply (deepStrictEqual on |
| 102 | + // two equal-shape objects with the same prototype should normally pass; |
| 103 | + // here we use objects whose enumerable properties differ to exercise the |
| 104 | + // ordinary diff path and confirm it is unaffected). |
| 105 | + assert.throws( |
| 106 | + () => assert.deepStrictEqual({ a: 1 }, { a: 2 }), |
| 107 | + (err) => { |
| 108 | + assert.strictEqual(err.code, 'ERR_ASSERTION'); |
| 109 | + // The ordinary diff path must NOT mention prototype differences |
| 110 | + // because the prototypes are identical. |
| 111 | + assert.doesNotMatch(err.message, /Object prototypes differ:/); |
| 112 | + return true; |
| 113 | + } |
| 114 | + ); |
| 115 | +}); |
0 commit comments