Skip to content

feat: add stablePatchmarks option #72

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

Merged
merged 6 commits into from
Mar 15, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add omitPatchMarks option
  • Loading branch information
ericbiewener committed Mar 14, 2019
commit da0826c657c63a64be91fbfe73d9185935bb46cc
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ expect(valueA: any).toMatchDiffSnapshot(valueB: any, options?: Options, testName
- `expand: boolean` (default: `false`) – expand the diff, so the whole information is preserved
- `colors: boolean` (default: `false`) – preserve color information from Jest diff
- `contextLines: number` (default: 5) - number of context lines to be shown at the beginning and at the end of a snapshot
- `omitPatchMarks: boolean` (default: `false`) - prevent line number patch marks from appearing in
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about stablePatchmarks? I'm still not sure how to best me it. @SimenB ideas?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stable is better than omit. Other possible verbs:

normalize
scrub

normalize doesn't communicate a whole lot. scrub communicates what we are doing to the patch mark, while stable communicates the purpose behind it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'd still go with stable

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all set

diffs. This can be helpful when diffs are breaking only because of the patch marks.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would also be cool to show an example like: changes @@ -1,1 +1,2 @@ to @@ --- --- @

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

- `aAnnotation: string` (default: `'First Value'`) - the annotation indicating from which serialization the `-` lines are
- `bAnnotation: string` (default: `'Second Value'`) - the annotation indicating from which serialization the `+` lines are

Expand Down
31 changes: 31 additions & 0 deletions __tests__/__snapshots__/snapshotDiff.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,37 @@ exports[`can expand diff 1`] = `
"
`;

exports[`can omitPatchMarks on diff 1`] = `
"Snapshot Diff:
- First value
+ Second value

-------------
some
some
some
some
some
- foo
+ bar
some
some
some
some
some
-------------
some
some
some
some
not
+ so
very
long
script
"
`;

exports[`can use contextLines on diff 1`] = `
"Snapshot Diff:
- First value
Expand Down
49 changes: 49 additions & 0 deletions __tests__/snapshotDiff.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,51 @@ const b = `
long
script
`;
const noIndentA = `
some
some
some
some
some
foo
some
some
some
some
some
some
some
some
some
some
not
very
long
script
`;
const noIndentB = `
some
some
some
some
some
bar
some
some
some
some
some
some
some
some
some
some
not
so
very
long
script
`;

type Props = {
test: string,
Expand Down Expand Up @@ -154,6 +199,10 @@ test('can use contextLines with React components', () => {
).toMatchSnapshot();
});

test('can omitPatchMarks on diff', () => {
expect(snapshotDiff(noIndentA, noIndentB, { omitPatchMarks: true })).toMatchSnapshot();
});

describe('failed optional deps', () => {
beforeEach(() => {
jest.mock('react-test-renderer', () => {
Expand Down
5 changes: 5 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const defaultOptions = {
expand: false,
colors: false,
contextLines: -1, // Forces to use default from Jest
omitPatchMarks: false,
aAnnotation: 'First value',
bAnnotation: 'Second value',
};
Expand All @@ -43,6 +44,10 @@ const snapshotDiff = (valueA: any, valueB: any, options?: Options): string => {
difference = stripAnsi(difference);
}

if (mergedOptions.omitPatchMarks) {
difference = difference.replace(/^@.*/gm, '-------------')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can make this regex better and take into account that we have two @@ and numbers with commas between. Also I'm leaning towards replacing them with:
@@ --- --- @@

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also we only need to check it only if expand === false

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I considered a more rigorous regex, but since jest-diff seems to always pad the start of the line with whitespace, I went the lazy route. But I agree this should be improved. Here's an example of the new regex: https://regex101.com/r/KLDkC0/1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ha, left out the 0s in the number range. Updated regex: https://regex101.com/r/KLDkC0/2

}

return SNAPSHOT_TITLE + difference;
};

Expand Down