/
plugin.js
138 lines (115 loc) · 3.53 KB
/
plugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* Copyright (c) 2018-present The Palmer Group
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import fs from 'fs-extra';
import { diffImageToSnapshot } from 'jest-image-snapshot/src/diff-snapshot';
import path from 'path';
import pkgDir from 'pkg-dir';
import { MATCH, RECORD } from './constants';
let snapshotOptions = {};
let snapshotResult = {};
let snapshotRunning = false;
const kebabSnap = '-snap.png';
const dotSnap = '.snap.png';
const dotDiff = '.diff.png';
export const cachePath = path.join(
pkgDir.sync(process.cwd()),
'cypress',
'.snapshot-report'
);
export function matchImageSnapshotOptions() {
return (options = {}) => {
snapshotOptions = options;
snapshotRunning = true;
return null;
};
}
export function matchImageSnapshotResult() {
return () => {
snapshotRunning = false;
const { pass, added, updated } = snapshotResult;
// @todo is there a less expensive way to share state between test and reporter?
if (!pass && !added && !updated && fs.existsSync(cachePath)) {
const cache = JSON.parse(fs.readFileSync(cachePath, 'utf8'));
cache.push(snapshotResult);
fs.writeFileSync(cachePath, JSON.stringify(cache), 'utf8');
}
return snapshotResult;
};
}
export function matchImageSnapshotPlugin({ path: screenshotPath }) {
if (!snapshotRunning) {
return null;
}
const {
screenshotsFolder,
updateSnapshots,
options: {
failureThreshold = 0,
failureThresholdType = 'pixel',
customSnapshotsDir,
customDiffDir,
...options
} = {},
} = snapshotOptions;
const receivedImageBuffer = fs.readFileSync(screenshotPath);
fs.removeSync(screenshotPath);
const { dir: screenshotDir, name: snapshotIdentifier } = path.parse(
screenshotPath
);
const relativePath = path.relative(screenshotsFolder, screenshotDir);
const snapshotsDir = customSnapshotsDir
? path.join(process.cwd(), customSnapshotsDir, relativePath)
: path.join(screenshotsFolder, '..', 'snapshots', relativePath);
const snapshotKebabPath = path.join(
snapshotsDir,
`${snapshotIdentifier}${kebabSnap}`
);
const snapshotDotPath = path.join(
snapshotsDir,
`${snapshotIdentifier}${dotSnap}`
);
const diffDir = customDiffDir
? path.join(process.cwd(), customDiffDir, relativePath)
: path.join(snapshotsDir, '__diff_output__');
const diffDotPath = path.join(diffDir, `${snapshotIdentifier}${dotDiff}`);
if (fs.pathExistsSync(snapshotDotPath)) {
fs.copySync(snapshotDotPath, snapshotKebabPath);
}
snapshotResult = diffImageToSnapshot({
snapshotsDir,
diffDir,
receivedImageBuffer,
snapshotIdentifier,
failureThreshold,
failureThresholdType,
updateSnapshot: updateSnapshots,
...options,
});
const { pass, added, updated, diffOutputPath } = snapshotResult;
if (!pass && !added && !updated) {
fs.copySync(diffOutputPath, diffDotPath);
fs.removeSync(diffOutputPath);
fs.removeSync(snapshotKebabPath);
snapshotResult.diffOutputPath = diffDotPath;
return {
path: diffDotPath,
};
}
fs.copySync(snapshotKebabPath, snapshotDotPath);
fs.removeSync(snapshotKebabPath);
snapshotResult.diffOutputPath = snapshotDotPath;
return {
path: snapshotDotPath,
};
}
export function addMatchImageSnapshotPlugin(on, config) {
on('task', {
[MATCH]: matchImageSnapshotOptions(config),
[RECORD]: matchImageSnapshotResult(config),
});
on('after:screenshot', matchImageSnapshotPlugin);
}