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

Allow preprocessing images before comparison #44

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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ See [the examples](./examples/README.md) for more detailed usage
* `noColors`: (default `false`) Removes colouring from console output, useful if storing the results in a file
* `failureThreshold`: (default `0`) Sets the threshold that would trigger a test failure based on the `failureThresholdType` selected. This is different to the `customDiffConfig.threshold` above, that is the per pixel failure threshold, this is the failure threshold for the entire comparison.
* `failureThresholdType`: (default `pixel`) (options `percent` or `pixel`) Sets the type of threshold that would trigger a failure.
* `beforeComparison`: (default `(received, baseline) => ({ received, baseline })`) Optional function for preprocessing images before comparison. Could be used to crop or align images in size before passing to pixelmatch. Should return a `PNG` instance from `pngjs` module.

```javascript
it('should demonstrate this matcher`s usage with a custom pixelmatch config', () => {
Expand Down
1 change: 1 addition & 0 deletions __tests__/__snapshots__/index.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

exports[`toMatchImageSnapshot passes diffImageToSnapshot everything it needs to create a snapshot and compare if needed 1`] = `
Object {
"beforeComparison": [Function],
"customDiffConfig": Object {},
"failureThreshold": 0,
"failureThresholdType": "pixel",
Expand Down
20 changes: 20 additions & 0 deletions __tests__/diff-snapshot.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,5 +412,25 @@ describe('diff-snapshot', () => {
});
}).toThrowErrorMatchingSnapshot();
});

it('should call custom preprocessing function', () => {
const diffImageToSnapshot = setupTest({ snapshotExists: true });
const beforeComparison = jest.fn((received, baseline) => ({ received, baseline }));

diffImageToSnapshot({
receivedImageBuffer: mockImageBuffer,
snapshotIdentifier: mockSnapshotIdentifier,
snapshotsDir: mockSnapshotsDir,
updateSnapshot: false,
failureThreshold: 0,
failureThresholdType: 'pixel',
beforeComparison,
});

expect(beforeComparison).toHaveBeenCalledWith(
expect.any(Object),
expect.any(Object)
);
});
});
});
1 change: 1 addition & 0 deletions __tests__/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ describe('toMatchImageSnapshot', () => {
updateSnapshot: false,
failureThreshold: 0,
failureThresholdType: 'pixel',
beforeComparison: expect.any(Function),
});
expect(Chalk).toHaveBeenCalledWith({
enabled: false,
Expand Down
8 changes: 6 additions & 2 deletions src/diff-snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ function diffImageToSnapshot(options) {
customDiffConfig = {},
failureThreshold,
failureThresholdType,
beforeComparison = (received, baseline) => ({ received, baseline }),
} = options;

let result = {};
Expand All @@ -41,8 +42,11 @@ function diffImageToSnapshot(options) {

const diffConfig = Object.assign({}, defaultDiffConfig, customDiffConfig);

const receivedImage = PNG.sync.read(receivedImageBuffer);
const baselineImage = PNG.sync.read(fs.readFileSync(baselineSnapshotPath));
const rawReceivedImage = PNG.sync.read(receivedImageBuffer);
const rawBaselineImage = PNG.sync.read(fs.readFileSync(baselineSnapshotPath));
const processedResult = beforeComparison(rawReceivedImage, rawBaselineImage);
const receivedImage = processedResult.received;
const baselineImage = processedResult.baseline;

if (
receivedImage.height !== baselineImage.height || receivedImage.width !== baselineImage.width
Expand Down
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ function configureToMatchImageSnapshot({
noColors: commonNoColors = false,
failureThreshold: commonFailureThreshold = 0,
failureThresholdType: commonFailureThresholdType = 'pixel',
beforeComparison: commonBeforeComparison = (received, baseline) => ({ received, baseline }),
} = {}) {
return function toMatchImageSnapshot(received, {
customSnapshotIdentifier = '',
Expand All @@ -37,6 +38,7 @@ function configureToMatchImageSnapshot({
noColors = commonNoColors,
failureThreshold = commonFailureThreshold,
failureThresholdType = commonFailureThresholdType,
beforeComparison = commonBeforeComparison,
} = {}) {
const { testPath, currentTestName, isNot } = this;
const chalk = new Chalk({ enabled: !noColors });
Expand All @@ -55,6 +57,7 @@ function configureToMatchImageSnapshot({
customDiffConfig: Object.assign({}, commonCustomDiffConfig, customDiffConfig),
failureThreshold,
failureThresholdType,
beforeComparison,
});

let pass = true;
Expand Down
Loading