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

Replace match with test and omit redundant String conversion in jest-diff #4311

Merged
merged 1 commit into from
Aug 23, 2017
Merged
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
7 changes: 5 additions & 2 deletions packages/jest-diff/src/__tests__/diff.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,12 @@ describe('no visual difference', () => {

test('oneline strings', () => {
// oneline strings don't produce a diff currently.
expect(stripAnsi(diff('ab', 'aa'))).toBe(null);
expect(diff('ab', 'aa')).toBe(null);
expect(diff('a', 'a')).toMatch(/no visual difference/);
expect(stripAnsi(diff('123456789', '234567890'))).toBe(null);
expect(diff('123456789', '234567890')).toBe(null);
// if either string is oneline
expect(diff('oneline', 'multi\nline')).toBe(null);
expect(diff('multi\nline', 'oneline')).toBe(null);
});

test('falls back to not call toJSON if objects look identical', () => {
Expand Down
6 changes: 4 additions & 2 deletions packages/jest-diff/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ const FALLBACK_FORMAT_OPTIONS = {
plugins: PLUGINS,
};

const MULTILINE_REGEXP = /[\r\n]/;

// Generate a string that will highlight the difference between two values
// with green and red. (similar to how github does code diffing)
function diff(a: any, b: any, options: ?DiffOptions): ?string {
Expand Down Expand Up @@ -79,9 +81,9 @@ function diff(a: any, b: any, options: ?DiffOptions): ?string {

switch (aType) {
case 'string':
const multiline = a.match(/[\r\n]/) !== -1 && b.indexOf('\n') !== -1;
const multiline = MULTILINE_REGEXP.test(a) && b.indexOf('\n') !== -1;
if (multiline) {
return diffStrings(String(a), String(b), options);
return diffStrings(a, b, options);
}
return null;
case 'number':
Expand Down