Skip to content

Commit bade294

Browse files
authored
feat: allow configuration of postfix for received screenshots filename (#328)
* docs: fix typo in contributing instructions * feat: allow configuration of postfix for received screenshots filename no changes if the option is not used, it defaults to the previously hardcoded string: '-received'
1 parent 96878dc commit bade294

File tree

6 files changed

+46
-2
lines changed

6 files changed

+46
-2
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Verifies that your code matches the American Express code style defined in [`esl
4747

4848
Runs unit tests **and** verifies the format of all commit messages on the current branch.
4949

50-
- **`npm posttest`**
50+
- **`npm run posttest`**
5151

5252
Runs linting on the current branch, checks that the commits follow [conventional commits](https://www.conventionalcommits.org/) and verifies that the `package-lock.json` file includes public NPM registry URLs.
5353

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ See [the examples](./examples/README.md) for more detailed usage or read about a
111111
* `customDiffDir`: A custom absolute path of a directory to keep this diff in
112112
* `storeReceivedOnFailure`: (default: `false`) Store the received images seperately from the composed diff images on failure. This can be useful when updating baseline images from CI.
113113
* `customReceivedDir`: A custom absolute path of a directory to keep this received image in
114+
* `customReceivedPostfix`: A custom postfix which is added to the snapshot name of the received image, defaults to `-received`
114115
* `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. If a path is given, the path will be created inside the snapshot/diff directories.
115116
* `diffDirection`: (default: `horizontal`) (options `horizontal` or `vertical`) Changes diff image layout direction
116117
* `onlyDiff`: (default: `false`) Either only include the difference between the baseline and the received image in the diff image, or include the 3 images (following the direction set by `diffDirection`).

__tests__/__snapshots__/index.spec.js.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ exports[`toMatchImageSnapshot passes diffImageToSnapshot everything it needs to
5050
"onlyDiff": false,
5151
"receivedDir": undefined,
5252
"receivedImageBuffer": "pretendthisisanimagebuffer",
53+
"receivedPostfix": undefined,
5354
"snapshotIdentifier": "test-spec-js-test-1-snap",
5455
"snapshotsDir": "path/to/__image_snapshots__",
5556
"storeReceivedOnFailure": false,

__tests__/diff-snapshot.spec.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,43 @@ describe('diff-snapshot', () => {
246246
expect(mockWriteFileSync).toHaveBeenCalledTimes(2);
247247
});
248248

249+
it('should write a received image with custom postfix if customReceivedPostfix is set', () => {
250+
const diffImageToSnapshot = setupTest({
251+
snapshotExists: true,
252+
pixelmatchResult: 5000,
253+
});
254+
const result = diffImageToSnapshot({
255+
receivedImageBuffer: mockFailImageBuffer,
256+
snapshotIdentifier: mockSnapshotIdentifier,
257+
snapshotsDir: mockSnapshotsDir,
258+
storeReceivedOnFailure: true,
259+
receivedDir: mockReceivedDir,
260+
receivedPostfix: '-new',
261+
diffDir: mockDiffDir,
262+
updateSnapshot: false,
263+
failureThreshold: 0,
264+
failureThresholdType: 'pixel',
265+
});
266+
267+
expect(result).toMatchObject({
268+
diffOutputPath: path.join(
269+
mockSnapshotsDir,
270+
'__diff_output__',
271+
'id1-diff.png'
272+
),
273+
receivedSnapshotPath: path.join(
274+
mockSnapshotsDir,
275+
'__received_output__',
276+
'id1-new.png'
277+
),
278+
diffRatio: 0.5,
279+
diffPixelCount: 5000,
280+
pass: false,
281+
});
282+
283+
expect(mockWriteFileSync).toHaveBeenCalledTimes(2);
284+
});
285+
249286
it('should not write a received image if the test fails and storeReceivedOnFailure = false', () => {
250287
const diffImageToSnapshot = setupTest({ snapshotExists: true, pixelmatchResult: 5000 });
251288
const result = diffImageToSnapshot({

src/diff-snapshot.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ function diffImageToSnapshot(options) {
206206
snapshotIdentifier,
207207
snapshotsDir,
208208
storeReceivedOnFailure,
209+
receivedPostfix = '-received',
209210
receivedDir = path.join(options.snapshotsDir, '__received_output__'),
210211
diffDir = path.join(options.snapshotsDir, '__diff_output__'),
211212
diffDirection,
@@ -228,7 +229,7 @@ function diffImageToSnapshot(options) {
228229
fs.writeFileSync(baselineSnapshotPath, receivedImageBuffer);
229230
result = { added: true };
230231
} else {
231-
const receivedSnapshotPath = path.join(receivedDir, `${snapshotIdentifier}-received.png`);
232+
const receivedSnapshotPath = path.join(receivedDir, `${snapshotIdentifier}${receivedPostfix}.png`);
232233
rimraf.sync(receivedSnapshotPath);
233234

234235
const diffOutputPath = path.join(diffDir, `${snapshotIdentifier}-diff.png`);

src/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ function configureToMatchImageSnapshot({
137137
customSnapshotsDir: commonCustomSnapshotsDir,
138138
storeReceivedOnFailure: commonStoreReceivedOnFailure = false,
139139
customReceivedDir: commonCustomReceivedDir,
140+
customReceivedPostfix: commonCustomReceivedPostfix,
140141
customDiffDir: commonCustomDiffDir,
141142
onlyDiff: commonOnlyDiff = false,
142143
diffDirection: commonDiffDirection = 'horizontal',
@@ -156,6 +157,7 @@ function configureToMatchImageSnapshot({
156157
customSnapshotsDir = commonCustomSnapshotsDir,
157158
storeReceivedOnFailure = commonStoreReceivedOnFailure,
158159
customReceivedDir = commonCustomReceivedDir,
160+
customReceivedPostfix = commonCustomReceivedPostfix,
159161
customDiffDir = commonCustomDiffDir,
160162
onlyDiff = commonOnlyDiff,
161163
diffDirection = commonDiffDirection,
@@ -197,6 +199,7 @@ function configureToMatchImageSnapshot({
197199

198200
const snapshotsDir = customSnapshotsDir || path.join(path.dirname(testPath), SNAPSHOTS_DIR);
199201
const receivedDir = customReceivedDir;
202+
const receivedPostfix = customReceivedPostfix;
200203
const diffDir = customDiffDir;
201204
const baselineSnapshotPath = path.join(snapshotsDir, `${snapshotIdentifier}.png`);
202205
OutdatedSnapshotReporter.markTouchedFile(baselineSnapshotPath);
@@ -218,6 +221,7 @@ function configureToMatchImageSnapshot({
218221
snapshotsDir,
219222
storeReceivedOnFailure,
220223
receivedDir,
224+
receivedPostfix,
221225
diffDir,
222226
diffDirection,
223227
onlyDiff,

0 commit comments

Comments
 (0)