-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcheck-snapshot.js
42 lines (34 loc) · 1.22 KB
/
check-snapshot.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
const fs = require('fs');
const path = require('path');
const EC = require('eight-colors');
const { getSnapshot, diffSnapshot } = require('../lib/');
module.exports = function(coverageResults) {
const snapDir = path.resolve(__dirname, 'snapshot');
if (!fs.existsSync(snapDir)) {
fs.mkdirSync(snapDir, {
recursive: true
});
}
const newSnapshot = getSnapshot(coverageResults);
const id = coverageResults.reportPath.split('/')[1];
const snapPath = path.resolve(snapDir, `${id}.snapshot.json`);
if (!fs.existsSync(snapPath) || process.env.TEST_SNAPSHOT) {
fs.writeFileSync(snapPath, JSON.stringify(newSnapshot, null, 4));
return;
}
const oldSnapshot = JSON.parse(fs.readFileSync(snapPath).toString('utf-8'));
const diff = diffSnapshot(oldSnapshot, newSnapshot, {
// skipEqual: false,
// showSummary: false,
maxCols: 30,
metrics: []
});
const snapName = path.basename(snapPath);
if (diff.change) {
EC.logRed(`❌ ERROR: Snapshot does not match reference: ${snapName}`);
console.log(diff.message);
process.exit(1);
} else {
EC.logGreen(`✅ Snapshot matched: ${snapName}`);
}
};