Skip to content

Commit ad49a97

Browse files
Álvaro FerreiraJAdshead
Álvaro Ferreira
andauthored
feat: allow folders on snapshot identifier (#267)
* feat: allow folders on snapshot identifier * docs(readme): added path support to identifier option Co-authored-by: Jonny Adshead <JAdshead@users.noreply.github.com>
1 parent ca3fd6d commit ad49a97

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ See [the examples](./examples/README.md) for more detailed usage or read about a
109109
* `comparisonMethod`: (default: `pixelmatch`) (options `pixelmatch` or `ssim`) The method by which images are compared. `pixelmatch` does a pixel by pixel comparison, whereas `ssim` does a structural similarity comparison. `ssim` is a new experimental feature for jest-image-snapshot, but may become the default comparison method in the future. For a better understanding of how to use SSIM, see [Recommendations when using SSIM Comparison](#recommendations-when-using-ssim-comparison).
110110
* `customSnapshotsDir`: A custom absolute path of a directory to keep this snapshot in
111111
* `customDiffDir`: A custom absolute path of a directory to keep this diff in
112-
* `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.
112+
* `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.
113113
* `diffDirection`: (default: `horizontal`) (options `horizontal` or `vertical`) Changes diff image layout direction
114114
* `noColors`: Removes coloring from console output, useful if storing the results in a file
115115
* `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.

__tests__/diff-snapshot.spec.js

+35
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,25 @@ describe('diff-snapshot', () => {
458458
expect(mockMkdirpSync).toHaveBeenCalledWith(path.join(mockSnapshotsDir, '__diff_output__'));
459459
});
460460

461+
it('should create diff output sub-directory if there is not one already and test is failing', () => {
462+
const diffImageToSnapshot = setupTest({
463+
snapshotExists: true,
464+
outputDirExists: false,
465+
pixelmatchResult: 100,
466+
});
467+
468+
diffImageToSnapshot({
469+
receivedImageBuffer: mockFailImageBuffer,
470+
snapshotIdentifier: path.join('parent', mockSnapshotIdentifier),
471+
snapshotsDir: mockSnapshotsDir,
472+
diffDir: mockDiffDir,
473+
failureThreshold: 0,
474+
failureThresholdType: 'pixel',
475+
});
476+
477+
expect(mockMkdirpSync).toHaveBeenCalledWith(path.join(mockSnapshotsDir, '__diff_output__', 'parent'));
478+
});
479+
461480
it('should not create diff output directory if test passed', () => {
462481
const diffImageToSnapshot = setupTest({ snapshotExists: true, outputDirExists: false });
463482
diffImageToSnapshot({
@@ -504,6 +523,22 @@ describe('diff-snapshot', () => {
504523
expect(mockMkdirpSync).toHaveBeenCalledWith(mockSnapshotsDir);
505524
});
506525

526+
it('should create snapshots sub-directory if there is not one already', () => {
527+
const diffImageToSnapshot = setupTest({ snapshotExists: true, snapshotDirExists: false });
528+
diffImageToSnapshot({
529+
receivedImageBuffer: mockImageBuffer,
530+
snapshotIdentifier: path.join('parent', mockSnapshotIdentifier),
531+
snapshotsDir: mockSnapshotsDir,
532+
diffDir: mockDiffDir,
533+
updateSnapshot: true,
534+
updatePassedSnapshot: true,
535+
failureThreshold: 0,
536+
failureThresholdType: 'pixel',
537+
});
538+
539+
expect(mockMkdirpSync).toHaveBeenCalledWith(path.join(mockSnapshotsDir, 'parent'));
540+
});
541+
507542
it('should not create snapshots directory if there already is one', () => {
508543
const diffImageToSnapshot = setupTest({ snapshotExists: true, snapshotDirExists: true });
509544
diffImageToSnapshot({

src/diff-snapshot.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ function diffImageToSnapshot(options) {
205205
let result = {};
206206
const baselineSnapshotPath = path.join(snapshotsDir, `${snapshotIdentifier}-snap.png`);
207207
if (!fs.existsSync(baselineSnapshotPath)) {
208-
mkdirp.sync(snapshotsDir);
208+
mkdirp.sync(path.dirname(baselineSnapshotPath));
209209
fs.writeFileSync(baselineSnapshotPath, receivedImageBuffer);
210210
result = { added: true };
211211
} else {
@@ -269,7 +269,7 @@ function diffImageToSnapshot(options) {
269269
});
270270

271271
if (isFailure({ pass, updateSnapshot })) {
272-
mkdirp.sync(diffDir);
272+
mkdirp.sync(path.dirname(diffOutputPath));
273273
const composer = new ImageComposer({
274274
direction: diffDirection,
275275
});
@@ -307,7 +307,7 @@ function diffImageToSnapshot(options) {
307307
imgSrcString: `data:image/png;base64,${pngBuffer.toString('base64')}`,
308308
};
309309
} else if (shouldUpdate({ pass, updateSnapshot, updatePassedSnapshot })) {
310-
mkdirp.sync(snapshotsDir);
310+
mkdirp.sync(path.dirname(baselineSnapshotPath));
311311
fs.writeFileSync(baselineSnapshotPath, receivedImageBuffer);
312312
result = { updated: true };
313313
} else {

0 commit comments

Comments
 (0)