Skip to content

Commit 0febe44

Browse files
pedrottimarkcpojer
authored andcommitted
Replace match with test and omit redundant String conversion (#4311)
1 parent 27c5a97 commit 0febe44

2 files changed

Lines changed: 9 additions & 4 deletions

File tree

packages/jest-diff/src/__tests__/diff.test.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,12 @@ describe('no visual difference', () => {
8080

8181
test('oneline strings', () => {
8282
// oneline strings don't produce a diff currently.
83-
expect(stripAnsi(diff('ab', 'aa'))).toBe(null);
83+
expect(diff('ab', 'aa')).toBe(null);
8484
expect(diff('a', 'a')).toMatch(/no visual difference/);
85-
expect(stripAnsi(diff('123456789', '234567890'))).toBe(null);
85+
expect(diff('123456789', '234567890')).toBe(null);
86+
// if either string is oneline
87+
expect(diff('oneline', 'multi\nline')).toBe(null);
88+
expect(diff('multi\nline', 'oneline')).toBe(null);
8689
});
8790

8891
test('falls back to not call toJSON if objects look identical', () => {

packages/jest-diff/src/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ const FALLBACK_FORMAT_OPTIONS = {
4040
plugins: PLUGINS,
4141
};
4242

43+
const MULTILINE_REGEXP = /[\r\n]/;
44+
4345
// Generate a string that will highlight the difference between two values
4446
// with green and red. (similar to how github does code diffing)
4547
function diff(a: any, b: any, options: ?DiffOptions): ?string {
@@ -79,9 +81,9 @@ function diff(a: any, b: any, options: ?DiffOptions): ?string {
7981

8082
switch (aType) {
8183
case 'string':
82-
const multiline = a.match(/[\r\n]/) !== -1 && b.indexOf('\n') !== -1;
84+
const multiline = MULTILINE_REGEXP.test(a) && b.indexOf('\n') !== -1;
8385
if (multiline) {
84-
return diffStrings(String(a), String(b), options);
86+
return diffStrings(a, b, options);
8587
}
8688
return null;
8789
case 'number':

0 commit comments

Comments
 (0)