nodejs/test/parallel/test-diff.js
Giovanni Bucci 6b42554342
util: expose diff function used by the assertion errors
fix: https://github.com/nodejs/node/issues/51740
PR-URL: https://github.com/nodejs/node/pull/57462
Fixes: https://github.com/nodejs/node/issues/51740
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Pietro Marchini <pietro.marchini94@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
2025-03-19 23:59:07 +00:00

81 lines
1.9 KiB
JavaScript

'use strict';
require('../common');
const { describe, it } = require('node:test');
const { deepStrictEqual, throws } = require('node:assert');
const { diff } = require('util');
describe('diff', () => {
it('throws because actual is nor an array nor a string', () => {
const actual = {};
const expected = 'foo';
throws(() => diff(actual, expected), {
message: 'The "actual" argument must be of type string. Received an instance of Object'
});
});
it('throws because expected is nor an array nor a string', () => {
const actual = 'foo';
const expected = {};
throws(() => diff(actual, expected), {
message: 'The "expected" argument must be of type string. Received an instance of Object'
});
});
it('throws because the actual array does not only contain string', () => {
const actual = ['1', { b: 2 }];
const expected = ['1', '2'];
throws(() => diff(actual, expected), {
message: 'The "actual[1]" argument must be of type string. Received an instance of Object'
});
});
it('returns an empty array because actual and expected are the same', () => {
const actual = 'foo';
const expected = 'foo';
const result = diff(actual, expected);
deepStrictEqual(result, []);
});
it('returns the diff for strings', () => {
const actual = '12345678';
const expected = '12!!5!7!';
const result = diff(actual, expected);
deepStrictEqual(result, [
[0, '1'],
[0, '2'],
[1, '3'],
[1, '4'],
[-1, '!'],
[-1, '!'],
[0, '5'],
[1, '6'],
[-1, '!'],
[0, '7'],
[1, '8'],
[-1, '!'],
]);
});
it('returns the diff for arrays', () => {
const actual = ['1', '2', '3'];
const expected = ['1', '3', '4'];
const result = diff(actual, expected);
deepStrictEqual(result, [
[0, '1'],
[1, '2'],
[0, '3'],
[-1, '4'],
]
);
});
});