Skip to content

Commit

Permalink
feat(diff-snapshot): clean up test results from previous test runs fo…
Browse files Browse the repository at this point in the history
…r a given snapshot
  • Loading branch information
anescobar1991 committed Mar 4, 2018
1 parent b974a77 commit f796435
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2,888 deletions.
21 changes: 21 additions & 0 deletions __tests__/integration.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,5 +140,26 @@ describe('toMatchImageSnapshot', () => {
// just because file was written does not mean it is a png image
expect(isPng(imageBuffer)).toBe(true);
});

it('removes result image from previous test runs for the same snapshot', () => {
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 })
).not.toThrowError();

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

// then test against image that should not generate results image (as it is passing test)
expect(
() => expect(imageData).toMatchImageSnapshot({ customSnapshotIdentifier })
).not.toThrowError();

expect(fs.existsSync(pathToResultImage)).toBe(false);
});
});
});
16 changes: 10 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,13 @@
"!**/node_modules/**",
"!test-results/**"
],
"testMatch": ["<rootDir>/__tests__/**/*.js"],
"coveragePathIgnorePatterns": ["/node_modules/", "<rootDir>/examples"]
"testMatch": [
"<rootDir>/__tests__/**/*.js"
],
"coveragePathIgnorePatterns": [
"/node_modules/",
"<rootDir>/examples"
]
},
"repository": {
"type": "git",
Expand All @@ -42,17 +47,16 @@
"eslint-config-amex": "^7.0.0",
"is-png": "^1.1.0",
"jest": "^22.0.0",
"lodash": "^4.17.4",
"mock-spawn": "^0.2.6",
"rimraf": "^2.6.2"
"mock-spawn": "^0.2.6"
},
"dependencies": {
"chalk": "^1.1.3",
"get-stdin": "^5.0.1",
"lodash": "^4.17.4",
"mkdirp": "^0.5.1",
"pixelmatch": "^4.0.2",
"pngjs": "^3.3.0"
"pngjs": "^3.3.0",
"rimraf": "^2.6.2"
},
"peerDependencies": {
"jest": ">=20 <=22"
Expand Down
4 changes: 4 additions & 0 deletions src/diff-snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
const fs = require('fs');
const path = require('path');
const childProcess = require('child_process');
const rimraf = require('rimraf');
const pixelmatch = require('pixelmatch');
const mkdirp = require('mkdirp');
const { PNG } = require('pngjs');
Expand All @@ -35,6 +36,9 @@ function diffImageToSnapshot(options) {
if (fs.existsSync(baselineSnapshotPath) && !updateSnapshot) {
const outputDir = path.join(snapshotsDir, '__diff_output__');
const diffOutputPath = path.join(outputDir, `${snapshotIdentifier}-diff.png`);

rimraf.sync(diffOutputPath);

const defaultDiffConfig = {
threshold: 0.01,
};
Expand Down
Loading

0 comments on commit f796435

Please sign in to comment.