Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add note about zero width space #14002

Closed
wants to merge 1 commit into from
Closed
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
10 changes: 7 additions & 3 deletions packages/expect/src/matchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
MatcherHintOptions,
RECEIVED_COLOR,
SUGGEST_TO_CONTAIN_EQUAL,
checkZeroWidthSpace,
ensureExpectedIsNonNegativeInteger,
ensureNoExpected,
ensureNumbers,
Expand Down Expand Up @@ -131,7 +132,8 @@ const matchers: MatchersObject = {
EXPECTED_LABEL,
RECEIVED_LABEL,
isExpand(this.expand),
)
) +
checkZeroWidthSpace(received, expected)
);
};

Expand Down Expand Up @@ -641,7 +643,8 @@ const matchers: MatchersObject = {
EXPECTED_LABEL,
RECEIVED_LABEL,
isExpand(this.expand),
);
) +
checkZeroWidthSpace(received, expected);

// Passing the actual and expected objects so that a custom reporter
// could access them, for example in order to display a custom visual diff,
Expand Down Expand Up @@ -978,7 +981,8 @@ const matchers: MatchersObject = {
EXPECTED_LABEL,
RECEIVED_LABEL,
isExpand(this.expand),
);
) +
checkZeroWidthSpace(received, expected);

// Passing the actual and expected objects so that a custom reporter
// could access them, for example in order to display a custom visual diff,
Expand Down
30 changes: 30 additions & 0 deletions packages/jest-matcher-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -582,3 +582,33 @@ export const matcherHint = (

return hint;
};

export const checkZeroWidthSpace = (
received: unknown,
expected: unknown,
): string => {
const createNote = (str: string, isReceived: boolean): string | undefined => {
// eslint-disable-next-line no-misleading-character-class
const zwspRegExp = RegExp(
'[\\u001c-\\u001f\\u11a3-\\u11a7\\u180e\\u200b-\\u200f\\u2060\\u3164\\u034f\\u202a-\\u202e\\u2061-\\u2063\\ufeff]',
);

const pos = str.search(zwspRegExp);
if (pos !== -1) {
return `Note: A zero-width space exists at index ${pos - 1} of ${
isReceived ? 'Received' : 'Expected'
}.`;
}
return undefined;
};

const note =
createNote(stringify(received), true) ||
createNote(stringify(expected), false);

if (note) {
return `\n${chalk.yellow(note)}`;
} else {
return '';
}
};