jest-image-snapshot 3.0.1 → 4.0.2

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.
@@ -0,0 +1,23 @@
1
+ name: Mark stale issues and pull requests
2
+
3
+ on:
4
+ schedule:
5
+ - cron: "0 0 * * *"
6
+
7
+ jobs:
8
+ stale:
9
+
10
+ runs-on: ubuntu-latest
11
+
12
+ steps:
13
+ - uses: actions/stale@v3
14
+ with:
15
+ repo-token: ${{ secrets.GITHUB_TOKEN }}
16
+ stale-issue-message: 'This issue is stale because it has been open 30 days with no activity.'
17
+ stale-pr-message: 'This pull request is stale because it has been open 30 days with no activity.'
18
+ stale-issue-label: 'stale-issue'
19
+ exempt-issue-labels: 'enhancement,documentation,good-first-issue,question'
20
+ stale-pr-label: 'stale-pr'
21
+ exempt-pr-labels: 'work-in-progress'
22
+ days-before-stale: 30
23
+ days-before-close: -1
package/.travis.yml CHANGED
@@ -1,6 +1,5 @@
1
1
  language: node_js
2
2
  node_js:
3
- - 8
4
3
  - 10
5
4
  - 12
6
5
  before_install:
@@ -20,4 +19,4 @@ jobs:
20
19
  provider: script
21
20
  skip_cleanup: true
22
21
  script:
23
- - npx semantic-release
22
+ - npx semantic-release
package/CHANGELOG.md CHANGED
@@ -1,3 +1,36 @@
1
+ ## [4.0.2](https://github.com/americanexpress/jest-image-snapshot/compare/v4.0.1...v4.0.2) (2020-05-27)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * **options:** auto-detect colors if noColors option is not specified ([d90298c](https://github.com/americanexpress/jest-image-snapshot/commit/d90298c3f102734107a7574ddf0516c19a349c66))
7
+
8
+ ## [4.0.1](https://github.com/americanexpress/jest-image-snapshot/compare/v4.0.0...v4.0.1) (2020-05-27)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * **options:** add allowSizeMismatch arg ([6529ff4](https://github.com/americanexpress/jest-image-snapshot/commit/6529ff4b2bd9a20abe33d4b68d9d793198931f18))
14
+
15
+ # [4.0.0](https://github.com/americanexpress/jest-image-snapshot/compare/v3.1.0...v4.0.0) (2020-05-14)
16
+
17
+
18
+ ### chore
19
+
20
+ * upgrade to jest 26 + drop node 8 support ([#205](https://github.com/americanexpress/jest-image-snapshot/issues/205)) ([4834533](https://github.com/americanexpress/jest-image-snapshot/commit/4834533369dae1533c93ad883e3f66617d7d9c3f))
21
+
22
+
23
+ ### BREAKING CHANGES
24
+
25
+ * drop node 8 support
26
+
27
+ # [3.1.0](https://github.com/americanexpress/jest-image-snapshot/compare/v3.0.1...v3.1.0) (2020-04-17)
28
+
29
+
30
+ ### Features
31
+
32
+ * **options:** add option to pass on size missmatch ([#174](https://github.com/americanexpress/jest-image-snapshot/issues/174)) ([cee46b1](https://github.com/americanexpress/jest-image-snapshot/commit/cee46b1fc94f962c34900a8b655d22665cea2854)), closes [#83](https://github.com/americanexpress/jest-image-snapshot/issues/83) [#85](https://github.com/americanexpress/jest-image-snapshot/issues/85)
33
+
1
34
  ## [3.0.1](https://github.com/americanexpress/jest-image-snapshot/compare/v3.0.0...v3.0.1) (2020-03-25)
2
35
 
3
36
 
package/README.md CHANGED
@@ -22,7 +22,7 @@ Want to get paid for your contributions to `jest-image-snapshot`?
22
22
 
23
23
  ## ✨ Features
24
24
 
25
- * Take image snapshots of your React components
25
+ * Take image snapshots of your application
26
26
  * Ability to compare snapshots from a baseline
27
27
  * Update snapshots when you're good with changes
28
28
  * Customize a difference threshold
@@ -107,13 +107,14 @@ See [the examples](./examples/README.md) for more detailed usage or read about a
107
107
  * `customDiffDir`: A custom absolute path of a directory to keep this diff in
108
108
  * `customSnapshotIdentifier`: A custom name to give this snapshot. If not provided one is computed automatically. When a function is provided it is called with an object containing `testPath`, `currentTestName`, `counter` and `defaultIdentifier` as its first argument. The function must return an identifier to use for the snapshot.
109
109
  * `diffDirection`: (default: `horizontal`) (options `horizontal` or `vertical`) Changes diff image layout direction
110
- * `noColors`: (default `false`) Removes coloring from console output, useful if storing the results in a file
110
+ * `noColors`: Removes coloring from console output, useful if storing the results in a file
111
111
  * `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.
112
112
  * `failureThresholdType`: (default `pixel`) (options `percent` or `pixel`) Sets the type of threshold that would trigger a failure.
113
113
  * `updatePassedSnapshot`: (default `false`) Updates a snapshot even if it passed the threshold against the existing one.
114
- * `blur`: (default `0`) Applies Gaussian Blur on compared images, accepts radius in pixels as value. Useful when you have noise after scaling images per different resolutions on your target website, usually setting it's value to 1-2 should be enough to solve that problem.
114
+ * `blur`: (default `0`) Applies Gaussian Blur on compared images, accepts radius in pixels as value. Useful when you have noise after scaling images per different resolutions on your target website, usually setting its value to 1-2 should be enough to solve that problem.
115
115
  * `runInProcess`: (default `false`) Runs the diff in process without spawning a child process.
116
116
  * `dumpDiffToConsole`: (default `false`) Will output base64 string of a diff image to console in case of failed tests (in addition to creating a diff image). This string can be copy-pasted to a browser address string to preview the diff for a failed test.
117
+ * `allowSizeMismatch`: (default `false`) If set to true, the build will not fail when the screenshots to compare have different sizes.
117
118
 
118
119
  ```javascript
119
120
  it('should demonstrate this matcher`s usage with a custom pixelmatch config', () => {
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "jest-image-snapshot",
3
- "version": "3.0.1",
3
+ "version": "4.0.2",
4
4
  "description": "Jest matcher for image comparisons. Most commonly used for visual regression testing.",
5
5
  "main": "src/index.js",
6
6
  "engines": {
7
- "node": ">=8.0.0"
7
+ "node": ">= 10.14.2"
8
8
  },
9
9
  "scripts": {
10
10
  "lint": "eslint ./ --ignore-path .gitignore --ext .js",
@@ -53,8 +53,8 @@
53
53
  "eslint-config-amex": "^7.0.0",
54
54
  "husky": "^4.2.1",
55
55
  "image-size": "^0.8.3",
56
- "jest": "^25.1.0",
57
- "jest-snapshot": "^25.1.0",
56
+ "jest": "^26.0.1",
57
+ "jest-snapshot": "^26.0.1",
58
58
  "semantic-release": "^17.0.4",
59
59
  "lockfile-lint": "^4.0.0",
60
60
  "mock-spawn": "^0.2.6"
@@ -70,7 +70,7 @@
70
70
  "rimraf": "^2.6.2"
71
71
  },
72
72
  "peerDependencies": {
73
- "jest": ">=20 <=25"
73
+ "jest": ">=20 <=26"
74
74
  },
75
75
  "husky": {
76
76
  "hooks": {
@@ -83,9 +83,9 @@
83
83
  "@semantic-release/commit-analyzer",
84
84
  "@semantic-release/release-notes-generator",
85
85
  "@semantic-release/changelog",
86
+ "@semantic-release/npm",
86
87
  "@semantic-release/git",
87
- "@semantic-release/github",
88
- "@semantic-release/npm"
88
+ "@semantic-release/github"
89
89
  ],
90
90
  "branch": "master"
91
91
  }
@@ -83,6 +83,38 @@ const shouldUpdate = ({ pass, updateSnapshot, updatePassedSnapshot }) => (
83
83
  (!pass && updateSnapshot) || (pass && updatePassedSnapshot)
84
84
  );
85
85
 
86
+ const shouldFail = ({
87
+ totalPixels,
88
+ diffPixelCount,
89
+ hasSizeMismatch,
90
+ allowSizeMismatch,
91
+ failureThresholdType,
92
+ failureThreshold,
93
+ }) => {
94
+ let pass = false;
95
+ let diffSize = false;
96
+ const diffRatio = diffPixelCount / totalPixels;
97
+ if (hasSizeMismatch) {
98
+ // do not fail if allowSizeMismatch is set
99
+ pass = allowSizeMismatch;
100
+ diffSize = true;
101
+ }
102
+ if (!diffSize || pass === true) {
103
+ if (failureThresholdType === 'pixel') {
104
+ pass = diffPixelCount <= failureThreshold;
105
+ } else if (failureThresholdType === 'percent') {
106
+ pass = diffRatio <= failureThreshold;
107
+ } else {
108
+ throw new Error(`Unknown failureThresholdType: ${failureThresholdType}. Valid options are "pixel" or "percent".`);
109
+ }
110
+ }
111
+ return {
112
+ pass,
113
+ diffSize,
114
+ diffRatio,
115
+ };
116
+ };
117
+
86
118
  function diffImageToSnapshot(options) {
87
119
  const {
88
120
  receivedImageBuffer,
@@ -96,6 +128,7 @@ function diffImageToSnapshot(options) {
96
128
  failureThreshold,
97
129
  failureThresholdType,
98
130
  blur,
131
+ allowSizeMismatch = false,
99
132
  } = options;
100
133
 
101
134
  let result = {};
@@ -140,9 +173,6 @@ function diffImageToSnapshot(options) {
140
173
 
141
174
  const diffImage = new PNG({ width: imageWidth, height: imageHeight });
142
175
 
143
- let pass = false;
144
- let diffSize = false;
145
- let diffRatio = 0;
146
176
  let diffPixelCount = 0;
147
177
 
148
178
  diffPixelCount = pixelmatch(
@@ -155,18 +185,19 @@ function diffImageToSnapshot(options) {
155
185
  );
156
186
 
157
187
  const totalPixels = imageWidth * imageHeight;
158
- diffRatio = diffPixelCount / totalPixels;
159
- // Always fail test on image size mismatch
160
- if (hasSizeMismatch) {
161
- pass = false;
162
- diffSize = true;
163
- } else if (failureThresholdType === 'pixel') {
164
- pass = diffPixelCount <= failureThreshold;
165
- } else if (failureThresholdType === 'percent') {
166
- pass = diffRatio <= failureThreshold;
167
- } else {
168
- throw new Error(`Unknown failureThresholdType: ${failureThresholdType}. Valid options are "pixel" or "percent".`);
169
- }
188
+
189
+ const {
190
+ pass,
191
+ diffSize,
192
+ diffRatio,
193
+ } = shouldFail({
194
+ totalPixels,
195
+ diffPixelCount,
196
+ hasSizeMismatch,
197
+ allowSizeMismatch,
198
+ failureThresholdType,
199
+ failureThreshold,
200
+ });
170
201
 
171
202
  if (isFailure({ pass, updateSnapshot })) {
172
203
  mkdirp.sync(diffDir);
@@ -213,6 +244,7 @@ function diffImageToSnapshot(options) {
213
244
  } else {
214
245
  result = {
215
246
  pass,
247
+ diffSize,
216
248
  diffRatio,
217
249
  diffPixelCount,
218
250
  diffOutputPath,
package/src/index.js CHANGED
@@ -37,6 +37,7 @@ function checkResult({
37
37
  snapshotIdentifier,
38
38
  chalk,
39
39
  dumpDiffToConsole,
40
+ allowSizeMismatch,
40
41
  }) {
41
42
  let pass = true;
42
43
  /*
@@ -65,7 +66,7 @@ function checkResult({
65
66
  const differencePercentage = result.diffRatio * 100;
66
67
  message = () => {
67
68
  let failure;
68
- if (result.diffSize) {
69
+ if (result.diffSize && !allowSizeMismatch) {
69
70
  failure = `Expected image to be the same size as the snapshot (${result.imageDimensions.baselineWidth}x${result.imageDimensions.baselineHeight}), but was different (${result.imageDimensions.receivedWidth}x${result.imageDimensions.receivedHeight}).\n`;
70
71
  } else {
71
72
  failure = `Expected image to match or be a close match to snapshot but was ${differencePercentage}% different from snapshot (${result.diffPixelCount} differing pixels).\n`;
@@ -127,13 +128,14 @@ function configureToMatchImageSnapshot({
127
128
  customSnapshotsDir: commonCustomSnapshotsDir,
128
129
  customDiffDir: commonCustomDiffDir,
129
130
  diffDirection: commonDiffDirection = 'horizontal',
130
- noColors: commonNoColors = false,
131
+ noColors: commonNoColors,
131
132
  failureThreshold: commonFailureThreshold = 0,
132
133
  failureThresholdType: commonFailureThresholdType = 'pixel',
133
134
  updatePassedSnapshot: commonUpdatePassedSnapshot = false,
134
135
  blur: commonBlur = 0,
135
136
  runInProcess: commonRunInProcess = false,
136
137
  dumpDiffToConsole: commonDumpDiffToConsole = false,
138
+ allowSizeMismatch: commonAllowSizeMismatch = false,
137
139
  } = {}) {
138
140
  return function toMatchImageSnapshot(received, {
139
141
  customSnapshotIdentifier = commonCustomSnapshotIdentifier,
@@ -148,11 +150,16 @@ function configureToMatchImageSnapshot({
148
150
  blur = commonBlur,
149
151
  runInProcess = commonRunInProcess,
150
152
  dumpDiffToConsole = commonDumpDiffToConsole,
153
+ allowSizeMismatch = commonAllowSizeMismatch,
151
154
  } = {}) {
152
155
  const {
153
156
  testPath, currentTestName, isNot, snapshotState,
154
157
  } = this;
155
- const chalk = new Chalk({ enabled: !noColors });
158
+ const chalkOptions = {};
159
+ if (typeof noColors !== 'undefined') {
160
+ chalkOptions.enabled = !noColors;
161
+ }
162
+ const chalk = new Chalk(chalkOptions);
156
163
 
157
164
  const retryTimes = parseInt(global[Symbol.for('RETRY_TIMES')], 10) || 0;
158
165
 
@@ -196,6 +203,7 @@ function configureToMatchImageSnapshot({
196
203
  failureThresholdType,
197
204
  updatePassedSnapshot,
198
205
  blur,
206
+ allowSizeMismatch,
199
207
  });
200
208
 
201
209
  return checkResult({
@@ -205,6 +213,7 @@ function configureToMatchImageSnapshot({
205
213
  snapshotIdentifier,
206
214
  chalk,
207
215
  dumpDiffToConsole,
216
+ allowSizeMismatch,
208
217
  });
209
218
  };
210
219
  }
@@ -1,13 +0,0 @@
1
- name: Greetings
2
-
3
- on: [pull_request, issues]
4
-
5
- jobs:
6
- greeting:
7
- runs-on: ubuntu-latest
8
- steps:
9
- - uses: actions/first-interaction@v1
10
- with:
11
- repo-token: ${{ secrets.GITHUB_TOKEN }}
12
- issue-message: 'Thanks for opening your first issue. Pull requests are always welcome!'
13
- pr-message: 'Thank you for your contribution to jest-image-snapshot. We will be reviewing your PR. In the meantime, please sign the CLA and make sure all status checks have passed.'