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

Ssim integration #220

Merged
merged 4 commits into from
Jul 23, 2020
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
1 change: 1 addition & 0 deletions __tests__/__snapshots__/index.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ exports[`toMatchImageSnapshot passes diffImageToSnapshot everything it needs to
Object {
"allowSizeMismatch": false,
"blur": 0,
"comparisonMethod": "pixelmatch",
"customDiffConfig": Object {},
"diffDir": "path/to/__image_snapshots__/__diff_output__",
"diffDirection": "horizontal",
Expand Down
4 changes: 4 additions & 0 deletions __tests__/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ describe('toMatchImageSnapshot', () => {
const customDiffConfig = { perceptual: true };
const customSnapshotIdentifier = ({ defaultIdentifier }) =>
`custom-${defaultIdentifier}`;
const comparisonMethod = 'ssim';
const toMatchImageSnapshot = configureToMatchImageSnapshot({
customDiffConfig,
customSnapshotIdentifier,
Expand All @@ -453,6 +454,7 @@ describe('toMatchImageSnapshot', () => {
failureThresholdType: 'percent',
updatePassedSnapshot: true,
blur: 1,
comparisonMethod,
});
expect.extend({ toMatchImageSnapshot });
const matcherAtTest = toMatchImageSnapshot.bind(mockTestContext);
Expand All @@ -473,6 +475,7 @@ describe('toMatchImageSnapshot', () => {
updatePassedSnapshot: true,
failureThreshold: 1,
failureThresholdType: 'percent',
comparisonMethod,
});
expect(Chalk).toHaveBeenCalledWith({
enabled: false,
Expand Down Expand Up @@ -530,6 +533,7 @@ describe('toMatchImageSnapshot', () => {
updatePassedSnapshot: false,
failureThreshold: 0,
failureThresholdType: 'pixel',
comparisonMethod: 'pixelmatch',
});
expect(Chalk).toHaveBeenCalledWith({
enabled: false,
Expand Down
245 changes: 205 additions & 40 deletions __tests__/integration.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,20 @@ describe('toMatchImageSnapshot', () => {

expect(diffExists(customSnapshotIdentifier)).toBe(false);
});

it('does not write a result image for passing tests (ssim)', () => {
const customSnapshotIdentifier = 'integration-6';

// First we need to write a new snapshot image
expect(
() => expect(imageData).toMatchImageSnapshot({
customSnapshotIdentifier,
comparisonMethod: 'ssim',
})
).not.toThrowError();

expect(diffExists(customSnapshotIdentifier)).toBe(false);
});
});

describe('updates', () => {
Expand Down Expand Up @@ -130,6 +144,23 @@ describe('toMatchImageSnapshot', () => {
expect(fs.readFileSync(updateImageSnapshotPath)).not.toEqual(updateImageData);
});

it('writes a result image for passing test in update mode with updatePassedSnapshots: true (ssim)', () => {
const updateModeMatcher = toMatchImageSnapshot.bind({
snapshotState: new SnapshotState(__filename, {
updateSnapshot: 'all',
}),
testPath: __filename,
});
updateModeMatcher(updateImageData, {
customSnapshotIdentifier,
updatePassedSnapshots: true,
failureThreshold: 2,
failureThresholdType: 'pixel',
comparisonMode: 'ssim',
});
expect(fs.readFileSync(updateImageSnapshotPath)).not.toEqual(updateImageData);
});

it('writes a result image for failing test in update mode by default', () => {
const updateModeMatcher = toMatchImageSnapshot.bind({
snapshotState: new SnapshotState(__filename, {
Expand Down Expand Up @@ -160,6 +191,23 @@ describe('toMatchImageSnapshot', () => {
});
expect(fs.readFileSync(updateImageSnapshotPath)).toEqual(updateImageData);
});

it('writes a result image for failing test in update mode with updatePassedSnapshots: false (ssim)', () => {
const updateModeMatcher = toMatchImageSnapshot.bind({
snapshotState: new SnapshotState(__filename, {
updateSnapshot: 'all',
}),
testPath: __filename,
});
updateModeMatcher(updateImageData, {
customSnapshotIdentifier,
updatePassedSnapshots: false,
failureThreshold: 0,
failureThresholdType: 'pixel',
comparisonMode: 'ssim',
});
expect(fs.readFileSync(updateImageSnapshotPath)).toEqual(updateImageData);
});
});

describe('failures', () => {
Expand Down Expand Up @@ -195,7 +243,8 @@ describe('toMatchImageSnapshot', () => {
() => expect(oversizeImageData).toMatchImageSnapshot({ customSnapshotIdentifier })
).toThrowError(/Expected image to be the same size as the snapshot \(100x100\), but was different \(153x145\)/);

expect(diffExists(customSnapshotIdentifier)).toBe(true);
expect(diffExists(customSnapshotIdentifier))
.toBe(true);
});

it('fails with images without diff pixels after being resized', () => {
Expand All @@ -217,81 +266,134 @@ describe('toMatchImageSnapshot', () => {
const pathToResultImage = path.join(__dirname, diffOutputDir(), `${customSnapshotIdentifier}-diff.png`);
// First we need to write a new snapshot image
expect(
() => expect(imageData).toMatchImageSnapshot({ customSnapshotIdentifier })
() => expect(imageData)
.toMatchImageSnapshot({ customSnapshotIdentifier })
).not.toThrowError();

// then test against a different image
expect(
() => expect(failImageData).toMatchImageSnapshot({ customSnapshotIdentifier })
() => expect(failImageData)
.toMatchImageSnapshot({ customSnapshotIdentifier })
).toThrow();

expect(fs.existsSync(pathToResultImage)).toBe(true);
expect(fs.existsSync(pathToResultImage))
.toBe(true);

// just because file was written does not mean it is a png image
expect(sizeOf(pathToResultImage)).toHaveProperty('type', 'png');
});

it('writes a result image for failing tests with horizontal layout', () => {
it('writes a result image for failing tests (ssim)', () => {
const largeImageData = fs.readFileSync(fromStubs('LargeTestImage.png'));
const largeFailureImageData = fs.readFileSync(fromStubs('LargeTestImageFailure.png'));
const largeImageFailureDiffData =
fs.readFileSync(fromStubs('LargeTestImage-LargeTestImageFailure-ssim-diff.png'));
const customSnapshotIdentifier = getIdentifierIndicatingCleanupIsRequired();
const pathToResultImage = path.join(__dirname, diffOutputDir(), `${customSnapshotIdentifier}-diff.png`);
// First we need to write a new snapshot image
expect(
() => expect(imageData).toMatchImageSnapshot({
customSnapshotIdentifier,
diffDirection: 'horizontal',
})
).not.toThrowError();
() => expect(largeImageData)
.toMatchImageSnapshot({
customSnapshotIdentifier, comparisonMethod: 'ssim',
})
)
.not
.toThrowError();

// then test against a different image
expect(
() => expect(failImageData).toMatchImageSnapshot({
customSnapshotIdentifier,
diffDirection: 'horizontal',
})
).toThrow();
() => expect(largeFailureImageData)
.toMatchImageSnapshot({
customSnapshotIdentifier, comparisonMethod: 'ssim',
})
)
.toThrow();

expect(fs.existsSync(pathToResultImage)).toBe(true);
expect(fs.existsSync(pathToResultImage))
.toBe(true);

expect(sizeOf(pathToResultImage)).toMatchObject({
width: 300,
height: 100,
type: 'png',
});
expect(fs.readFileSync(pathToResultImage)).toEqual(largeImageFailureDiffData);
// just because file was written does not mean it is a png image
expect(sizeOf(pathToResultImage))
.toHaveProperty('type', 'png');
});

it('writes a result image for failing tests with vertical layout', () => {
it('writes a result image for failing tests with horizontal layout', () => {
const customSnapshotIdentifier = getIdentifierIndicatingCleanupIsRequired();
const pathToResultImage = path.join(__dirname, diffOutputDir(), `${customSnapshotIdentifier}-diff.png`);
// First we need to write a new snapshot image
expect(
() => expect(imageData).toMatchImageSnapshot({
customSnapshotIdentifier,
diffDirection: 'vertical',
})
).not.toThrowError();
() => expect(imageData)
.toMatchImageSnapshot({
customSnapshotIdentifier,
diffDirection: 'horizontal',
})
)
.not
.toThrowError();

// then test against a different image
expect(
() => expect(failImageData).toMatchImageSnapshot({
customSnapshotIdentifier,
diffDirection: 'vertical',
})
).toThrow();
() => expect(failImageData)
.toMatchImageSnapshot({
customSnapshotIdentifier,
diffDirection: 'horizontal',
})
)
.toThrow();

expect(fs.existsSync(pathToResultImage))
.toBe(true);

expect(sizeOf(pathToResultImage))
.toMatchObject({
width: 300,
height: 100,
type: 'png',
});
});

expect(fs.existsSync(pathToResultImage)).toBe(true);
it('writes a result image for failing tests with vertical layout', () => {
const customSnapshotIdentifier = getIdentifierIndicatingCleanupIsRequired();
const pathToResultImage = path.join(__dirname, diffOutputDir(), `${customSnapshotIdentifier}-diff.png`);
// First we need to write a new snapshot image
expect(
() => expect(imageData)
.toMatchImageSnapshot({
customSnapshotIdentifier,
diffDirection: 'vertical',
})
)
.not
.toThrowError();

expect(sizeOf(pathToResultImage)).toMatchObject({
width: 100,
height: 300,
type: 'png',
});
// then test against a different image
expect(
() => expect(failImageData)
.toMatchImageSnapshot({
customSnapshotIdentifier,
diffDirection: 'vertical',
})
)
.toThrow();

expect(fs.existsSync(pathToResultImage))
.toBe(true);

expect(sizeOf(pathToResultImage))
.toMatchObject({
width: 100,
height: 300,
type: 'png',
});
});

it('removes result image from previous test runs for the same snapshot', () => {
const customSnapshotIdentifier = getIdentifierIndicatingCleanupIsRequired();
// First we need to write a new snapshot image
expect(
() => expect(imageData).toMatchImageSnapshot({ customSnapshotIdentifier })
() => expect(imageData)
.toMatchImageSnapshot({ customSnapshotIdentifier })
).not.toThrowError();

// then test against a different image (to generate a results image)
Expand All @@ -304,7 +406,8 @@ describe('toMatchImageSnapshot', () => {
() => expect(imageData).toMatchImageSnapshot({ customSnapshotIdentifier })
).not.toThrowError();

expect(diffExists(customSnapshotIdentifier)).toBe(false);
expect(diffExists(customSnapshotIdentifier))
.toBe(false);
});

it('handles diffs for large images', () => {
Expand All @@ -321,5 +424,67 @@ describe('toMatchImageSnapshot', () => {
() => expect(largeFailureImageData).toMatchImageSnapshot({ customSnapshotIdentifier })
).toThrow(/Expected image to match or be a close match/);
});

describe('Desktop Images Test', () => {
it('not to throw at 6pct with pixelmatch with', () => {
const largeImageData = fs.readFileSync(fromStubs('Desktop 1_082.png'));
const largeFailureImageData = fs.readFileSync(fromStubs('Desktop 1_083.png'));
const customSnapshotIdentifier = getIdentifierIndicatingCleanupIsRequired();
// First we need to write a new snapshot image
expect(
() => expect(largeImageData)
.toMatchImageSnapshot({
failureThreshold: 0.06,
failureThresholdType: 'percent',
customSnapshotIdentifier,
})
)
.not
.toThrowError();

// then test against a different image
expect(
() => expect(largeFailureImageData)
.toMatchImageSnapshot({
failureThreshold: 0.06,
failureThresholdType: 'percent',
customSnapshotIdentifier,
})
)
.not
.toThrowError();
});
it('to throw at 1pct with SSIM', () => {
const largeImageData = fs.readFileSync(fromStubs('Desktop 1_082.png'));
const largeFailureImageData = fs.readFileSync(fromStubs('Desktop 1_083.png'));
const customSnapshotIdentifier = getIdentifierIndicatingCleanupIsRequired();
// First we need to write a new snapshot image
expect(
() => expect(largeImageData)
.toMatchImageSnapshot({
comparisonMethod: 'ssim',
failureThreshold: 0.01,
failureThresholdType: 'percent',
customSnapshotIdentifier,
})
)
.not
.toThrowError();

// then test against a different image
expect(
() => expect(largeFailureImageData)
.toMatchImageSnapshot({
comparisonMethod: 'ssim',
failureThreshold: 0.01,
failureThresholdType: 'percent',
customSnapshotIdentifier,
// required for coverage
runInProcess: true,
})
)
.toThrow(/Expected image to match or be a close match/);
});
});
});
});
Binary file added __tests__/stubs/Desktop 1_082.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added __tests__/stubs/Desktop 1_083.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@
"mkdirp": "^0.5.1",
"pixelmatch": "^5.1.0",
"pngjs": "^3.4.0",
"rimraf": "^2.6.2"
"rimraf": "^2.6.2",
"ssim.js": "^3.1.1"
},
"peerDependencies": {
"jest": ">=20 <=26"
Expand Down
Loading