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

feat: add onlyDiff in options #317

Merged
merged 4 commits into from
Dec 2, 2022
Merged

feat: add onlyDiff in options #317

merged 4 commits into from
Dec 2, 2022

Conversation

Ayc0
Copy link
Contributor

@Ayc0 Ayc0 commented Nov 8, 2022

Description

Add a new option onlyDiff in toMatchImageSnapshot

Motivation and Context

When working with complex rules, we may want to handle the 3 images independently: the received image, the diff image (with only the diff), and the expected image.

How Has This Been Tested?

  • I added a unit test
  • And also tested it with this screenshot:
Without onlyDiff (or with onlyDiff = false) With onlyDiff = true
local-image-spec-js-works-reading-an-image-from-the-local-file-system-1-snap-diff local-image-spec-js-works-reading-an-image-from-the-local-file-system-1-snap-diff

Types of Changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation (adding or updating documentation)
  • Dependency update

Checklist:

  • My change requires a change to the documentation and I have updated the documentation accordingly.
  • My changes are in sync with the code style of this project.
  • There aren't any other open Pull Requests for the same issue/update.
  • These changes should be applied to a maintenance branch.
  • I have added the Apache 2.0 license header to any new files created.

What is the Impact to Developers Using Jest-Image-Snapshot?

@CLAassistant
Copy link

CLAassistant commented Nov 8, 2022

CLA assistant check
All committers have signed the CLA.

@Ayc0 Ayc0 marked this pull request as ready for review November 8, 2022 12:28
@Ayc0 Ayc0 requested a review from a team as a code owner November 8, 2022 12:28
Comment on lines 187 to 202
function composeDiff(options) {
const {
diffDirection, baselineImage, diffImage, receivedImage, imageWidth, imageHeight, onlyDiff,
} = options;
const composer = new ImageComposer({
direction: diffDirection,
});

if (!onlyDiff) {
composer.addImage(baselineImage, imageWidth, imageHeight);
}
composer.addImage(diffImage, imageWidth, imageHeight);
if (!onlyDiff) {
composer.addImage(receivedImage, imageWidth, imageHeight);
}
return composer;
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 created a new function, otherwise the complexity of diffImageToSnapshot was too high

__tests__/__snapshots__/index.spec.js.snap Outdated Show resolved Hide resolved
src/index.js Outdated Show resolved Hide resolved
@code-forger code-forger requested a review from a team November 8, 2022 12:46
@Ayc0
Copy link
Contributor Author

Ayc0 commented Nov 8, 2022

@code-forger I pushed an update

@Ayc0 Ayc0 requested a review from code-forger November 8, 2022 14:24
@Ayc0
Copy link
Contributor Author

Ayc0 commented Nov 14, 2022

@code-forger can you have another look? 🙏

@Ayc0
Copy link
Contributor Author

Ayc0 commented Nov 23, 2022

@10xLaCroixDrinker can you have a look? 🙏

src/diff-snapshot.js Outdated Show resolved Hide resolved
Comment on lines 712 to 715
expect(diffResult).toHaveProperty('pass', false);
// White image without the baseline, nor the received (white because we mock pixelmatch)
expect(diffResult).toHaveProperty('imgSrcString', 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAA30lEQVR4Ae3BIQEAAACDMAT9M78G4ptcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcilyKXIpcygA9UAGR3m7UvwAAAABJRU5ErkJggg==');
expect(diffResult).toHaveProperty('diffOutputPath', path.join(mockSnapshotsDir, '__diff_output__', `${mockSnapshotIdentifier}-diff.png`));
Copy link
Member

Choose a reason for hiding this comment

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

These assertions don't give me any confidence. Can we add something that clearly checks that only the diff is included?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What if I add to the stubs folder the expected result of test instead of doing a manual data:image/png;base64,…?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Edit: I removed this test and I moved it to an integration test, so that we can have a real check and a real diff image

@Ayc0 Ayc0 requested review from 10xLaCroixDrinker and code-forger and removed request for code-forger and 10xLaCroixDrinker November 30, 2022 08:54
@Ayc0 Ayc0 requested a review from code-forger November 30, 2022 15:00
@Ayc0
Copy link
Contributor Author

Ayc0 commented Dec 2, 2022

@10xLaCroixDrinker do you know if we also need @code-forger's approval (as a "request changes" was applied)?

@10xLaCroixDrinker 10xLaCroixDrinker merged commit 4bad752 into americanexpress:main Dec 2, 2022
oneamexbot added a commit that referenced this pull request Dec 2, 2022
# [6.1.0](v6.0.0...v6.1.0) (2022-12-02)

### Features

* add onlyDiff in options ([#317](#317)) ([4bad752](4bad752))
@oneamexbot
Copy link
Contributor

🎉 This PR is included in version 6.1.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@Ayc0
Copy link
Contributor Author

Ayc0 commented Dec 2, 2022

Thanks! 🎉

@Ayc0
Copy link
Contributor Author

Ayc0 commented Dec 6, 2022

FYI I'm also changing the types for this new option in DefinitelyTyped/DefinitelyTyped#63501

typescript-bot pushed a commit to DefinitelyTyped/DefinitelyTyped that referenced this pull request Dec 6, 2022
…n version 6.1.0 by @Ayc0

* [jest-image-snapshot] add `onlyDiff` option

Add changes introduced in americanexpress/jest-image-snapshot#317 to the types

* [jest-image-snapshot] update version reference

* [jest-image-snapshot] add reference to author Ayc0

* [jest-image-snapshot] add tests for onlyDiff
goverdhan07 pushed a commit to goverdhan07/jest-image-snapshot that referenced this pull request Jul 23, 2023
goverdhan07 pushed a commit to goverdhan07/jest-image-snapshot that referenced this pull request Jul 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants