jest-image-snapshot 4.1.0 → 4.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/README.md +22 -0
- package/package.json +1 -1
- package/src/index.js +2 -0
- package/src/outdated-snapshot-reporter.js +84 -0
package/CHANGELOG.md
CHANGED
@@ -1,3 +1,10 @@
|
|
1
|
+
# [4.2.0](https://github.com/americanexpress/jest-image-snapshot/compare/v4.1.0...v4.2.0) (2020-08-29)
|
2
|
+
|
3
|
+
|
4
|
+
### Features
|
5
|
+
|
6
|
+
* add obsolete snapshot reporting ([#222](https://github.com/americanexpress/jest-image-snapshot/issues/222)) ([47da7c2](https://github.com/americanexpress/jest-image-snapshot/commit/47da7c23495037e869ee68154218e5d73e1e8cd5))
|
7
|
+
|
1
8
|
# [4.1.0](https://github.com/americanexpress/jest-image-snapshot/compare/v4.0.2...v4.1.0) (2020-07-23)
|
2
9
|
|
3
10
|
|
package/README.md
CHANGED
@@ -160,6 +160,28 @@ expect.extend({ toMatchImageSnapshot });
|
|
160
160
|
### jest.retryTimes()
|
161
161
|
Jest supports [automatic retries on test failures](https://jestjs.io/docs/en/jest-object#jestretrytimes). This can be useful for browser screenshot tests which tend to have more frequent false positives. Note that when using jest.retryTimes you'll have to use a unique customSnapshotIdentifier as that's the only way to reliably identify snapshots.
|
162
162
|
|
163
|
+
### Removing Outdated Snapshots
|
164
|
+
|
165
|
+
Unlike jest-managed snapshots, the images created by `jest-image-snapshot` will not be automatically removed by the `-u` flag if they are no longer needed. You can force `jest-image-snapshot` to remove the files by including the `outdated-snapshot-reporter` in your config and running with the environment variable `JEST_IMAGE_SNAPSHOT_TRACK_OBSOLETE`.
|
166
|
+
|
167
|
+
```json
|
168
|
+
{
|
169
|
+
"jest": {
|
170
|
+
"reporters": [
|
171
|
+
"default",
|
172
|
+
"jest-image-snapshot/src/outdated-snapshot-reporter.js"
|
173
|
+
]
|
174
|
+
}
|
175
|
+
}
|
176
|
+
```
|
177
|
+
|
178
|
+
**WARNING: Do not run a *partial* test suite with this flag as it may consider snapshots of tests that weren't run to be obsolete.**
|
179
|
+
|
180
|
+
```bash
|
181
|
+
export JEST_IMAGE_SNAPSHOT_TRACK_OBSOLETE=1
|
182
|
+
jest
|
183
|
+
```
|
184
|
+
|
163
185
|
### Recommendations when using SSIM comparison
|
164
186
|
Since SSIM calculates differences in structural similarity by building a moving 'window' over an images pixels, it does not particularly benefit from pixel count comparisons, especially when you factor in that it has a lot of floating point arithmetic in javascript. However, SSIM gains two key benefits over pixel by pixel comparison:
|
165
187
|
- Reduced false positives (failing tests when the images look the same)
|
package/package.json
CHANGED
package/src/index.js
CHANGED
@@ -18,6 +18,7 @@ const path = require('path');
|
|
18
18
|
const Chalk = require('chalk').constructor;
|
19
19
|
const { diffImageToSnapshot, runDiffImageToSnapshot } = require('./diff-snapshot');
|
20
20
|
const fs = require('fs');
|
21
|
+
const OutdatedSnapshotReporter = require('./outdated-snapshot-reporter');
|
21
22
|
|
22
23
|
const timesCalled = new Map();
|
23
24
|
|
@@ -180,6 +181,7 @@ function configureToMatchImageSnapshot({
|
|
180
181
|
const snapshotsDir = customSnapshotsDir || path.join(path.dirname(testPath), SNAPSHOTS_DIR);
|
181
182
|
const diffDir = customDiffDir || path.join(snapshotsDir, '__diff_output__');
|
182
183
|
const baselineSnapshotPath = path.join(snapshotsDir, `${snapshotIdentifier}-snap.png`);
|
184
|
+
OutdatedSnapshotReporter.markTouchedFile(baselineSnapshotPath);
|
183
185
|
|
184
186
|
if (snapshotState._updateSnapshot === 'none' && !fs.existsSync(baselineSnapshotPath)) {
|
185
187
|
return {
|
@@ -0,0 +1,84 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2020 American Express Travel Related Services Company, Inc.
|
3
|
+
*
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
5
|
+
* in compliance with the License. You may obtain a copy of the License at
|
6
|
+
*
|
7
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
*
|
9
|
+
* Unless required by applicable law or agreed to in writing, software distributed under the License
|
10
|
+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
|
11
|
+
* or implied. See the License for the specific language governing permissions and limitations under
|
12
|
+
* the License.
|
13
|
+
*/
|
14
|
+
|
15
|
+
/* eslint-disable class-methods-use-this */
|
16
|
+
|
17
|
+
const fs = require('fs');
|
18
|
+
const path = require('path');
|
19
|
+
|
20
|
+
const TOUCHED_FILE_LIST_PATH = path.join(
|
21
|
+
process.cwd(),
|
22
|
+
'.jest-image-snapshot-touched-files'
|
23
|
+
);
|
24
|
+
|
25
|
+
const IS_ENABLED = !!process.env.JEST_IMAGE_SNAPSHOT_TRACK_OBSOLETE;
|
26
|
+
|
27
|
+
class OutdatedSnapshotReporter {
|
28
|
+
/* istanbul ignore next - test coverage in child process */
|
29
|
+
static markTouchedFile(filePath) {
|
30
|
+
if (!IS_ENABLED) return;
|
31
|
+
const touchedListFileDescriptor = fs.openSync(TOUCHED_FILE_LIST_PATH, 'as');
|
32
|
+
fs.writeSync(touchedListFileDescriptor, `${filePath}\n`);
|
33
|
+
fs.closeSync(touchedListFileDescriptor);
|
34
|
+
}
|
35
|
+
|
36
|
+
/* istanbul ignore next - test coverage in child process */
|
37
|
+
static readTouchedFileListFromDisk() {
|
38
|
+
if (!fs.existsSync(TOUCHED_FILE_LIST_PATH)) return [];
|
39
|
+
|
40
|
+
return Array.from(
|
41
|
+
new Set(
|
42
|
+
fs
|
43
|
+
.readFileSync(TOUCHED_FILE_LIST_PATH, 'utf-8')
|
44
|
+
.split('\n')
|
45
|
+
.filter(file => file && fs.existsSync(file))
|
46
|
+
)
|
47
|
+
);
|
48
|
+
}
|
49
|
+
|
50
|
+
/* istanbul ignore next - test coverage in child process */
|
51
|
+
onRunStart() {
|
52
|
+
if (!IS_ENABLED) return;
|
53
|
+
if (fs.existsSync(TOUCHED_FILE_LIST_PATH)) {
|
54
|
+
fs.unlinkSync(TOUCHED_FILE_LIST_PATH);
|
55
|
+
}
|
56
|
+
}
|
57
|
+
|
58
|
+
/* istanbul ignore next - test coverage in child process */
|
59
|
+
onRunComplete() {
|
60
|
+
if (!IS_ENABLED) return;
|
61
|
+
const touchedFiles = OutdatedSnapshotReporter.readTouchedFileListFromDisk();
|
62
|
+
const imageSnapshotDirectories = Array.from(
|
63
|
+
new Set(touchedFiles.map(file => path.dirname(file)))
|
64
|
+
);
|
65
|
+
const allFiles = imageSnapshotDirectories
|
66
|
+
.map(dir => fs.readdirSync(dir).map(file => path.join(dir, file)))
|
67
|
+
.reduce((a, b) => a.concat(b), [])
|
68
|
+
.filter(file => file.endsWith('-snap.png'));
|
69
|
+
const obsoleteFiles = allFiles.filter(
|
70
|
+
file => !touchedFiles.includes(file)
|
71
|
+
);
|
72
|
+
|
73
|
+
if (fs.existsSync(TOUCHED_FILE_LIST_PATH)) {
|
74
|
+
fs.unlinkSync(TOUCHED_FILE_LIST_PATH);
|
75
|
+
}
|
76
|
+
|
77
|
+
obsoleteFiles.forEach((file) => {
|
78
|
+
process.stderr.write(`Deleting outdated snapshot "${file}"...\n`);
|
79
|
+
fs.unlinkSync(file);
|
80
|
+
});
|
81
|
+
}
|
82
|
+
}
|
83
|
+
|
84
|
+
module.exports = OutdatedSnapshotReporter;
|