Skip to content
Draft
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: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## main

### Fixes

- `[jest-diff]` Fix `diffStrings()` to not break apart surrogate pairs ([#15812](https://github.com/jestjs/jest/pull/15812))

## 30.1.3

### Fixes
Expand Down
36 changes: 36 additions & 0 deletions packages/jest-diff/src/__tests__/diffStringsRaw.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,40 @@ describe('diffStringsRaw', () => {

expect(received).toEqual(expected);
});

describe('unicode', () => {
test('surrogate pairs', () => {
const expected: Array<Diff> = [
new Diff(DIFF_DELETE, '😞'),
new Diff(DIFF_INSERT, '😄'),
];
const received = diffStringsRaw('😞', '😄', false);

expect(received).toEqual(expected);
});
test('grapheme clusters', () => {
const expected: Array<Diff> = [
new Diff(DIFF_DELETE, '👩‍👩‍'),
new Diff(DIFF_EQUAL, '👧'),
new Diff(DIFF_DELETE, '‍👦'),
new Diff(DIFF_EQUAL, ' 🇺'),
new Diff(DIFF_DELETE, '🇸'),
new Diff(DIFF_INSERT, '🇦'),
];
const received = diffStringsRaw('👩‍👩‍👧‍👦 🇺🇸', '👧 🇺🇦', false);

expect(received).toEqual(expected);
});
test('normalization', () => {
const expected: Array<Diff> = [
new Diff(DIFF_EQUAL, 'ma'),
new Diff(DIFF_DELETE, 'n\u0303'),
new Diff(DIFF_INSERT, 'ñ'),
new Diff(DIFF_EQUAL, 'ana'),
];
const received = diffStringsRaw('man\u0303ana', 'mañana', false);

expect(received).toEqual(expected);
});
});
});
33 changes: 24 additions & 9 deletions packages/jest-diff/src/diffStrings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import diffSequences from '@jest/diff-sequences';
import {DIFF_DELETE, DIFF_EQUAL, DIFF_INSERT, Diff} from './cleanupSemantic';

const diffStrings = (a: string, b: string): Array<Diff> => {
const isCommon = (aIndex: number, bIndex: number) => a[aIndex] === b[bIndex];
// Split strings into code points to handle surrogate pairs.
const aCodepoints = [...a];
const bCodepoints = [...b];
const isCommon = (aIndex: number, bIndex: number) =>
aCodepoints[aIndex] === bCodepoints[bIndex];

let aIndex = 0;
let bIndex = 0;
Expand All @@ -21,25 +25,36 @@ const diffStrings = (a: string, b: string): Array<Diff> => {
bCommon: number,
) => {
if (aIndex !== aCommon) {
diffs.push(new Diff(DIFF_DELETE, a.slice(aIndex, aCommon)));
diffs.push(
new Diff(DIFF_DELETE, aCodepoints.slice(aIndex, aCommon).join('')),
);
}
if (bIndex !== bCommon) {
diffs.push(new Diff(DIFF_INSERT, b.slice(bIndex, bCommon)));
diffs.push(
new Diff(DIFF_INSERT, bCodepoints.slice(bIndex, bCommon).join('')),
);
}

aIndex = aCommon + nCommon; // number of characters compared in a
bIndex = bCommon + nCommon; // number of characters compared in b
diffs.push(new Diff(DIFF_EQUAL, b.slice(bCommon, bIndex)));
diffs.push(
new Diff(DIFF_EQUAL, bCodepoints.slice(bCommon, bIndex).join('')),
);
};

diffSequences(a.length, b.length, isCommon, foundSubsequence);
diffSequences(
aCodepoints.length,
bCodepoints.length,
isCommon,
foundSubsequence,
);

// After the last common subsequence, push remaining change items.
if (aIndex !== a.length) {
diffs.push(new Diff(DIFF_DELETE, a.slice(aIndex)));
if (aIndex !== aCodepoints.length) {
diffs.push(new Diff(DIFF_DELETE, aCodepoints.slice(aIndex).join('')));
}
if (bIndex !== b.length) {
diffs.push(new Diff(DIFF_INSERT, b.slice(bIndex)));
if (bIndex !== bCodepoints.length) {
diffs.push(new Diff(DIFF_INSERT, bCodepoints.slice(bIndex).join('')));
}

return diffs;
Expand Down
Loading