-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
67 lines (59 loc) · 1.81 KB
/
index.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
const fs = require('fs');
const jsdiff = require('diff');
// Extend the yeoman-assert module
const assert = module.exports = require('yeoman-assert');
/**
* Assert that a file matches a snapshot file
* @param {String} filePath - path to a file
* @param {String} snapshotFilePath - path to a snapshot file
* @example
* assert.snapshotContent(
* 'swagger.yaml',
* path.join(__dirname, 'snapshots', 'swagger.yaml')
* );
*/
assert.snapshotContent = (filePath, snapshotFilePath) => {
// Ensure generated file exists before proceeding
assert.file(filePath);
assert.file(snapshotFilePath);
// Get content of generated file and snapshot file
const generatedFile = fs.readFileSync(filePath);
const snapshotFile = fs.readFileSync(snapshotFilePath);
// Use diff to check for differences between files
const diff = jsdiff.diffTrimmedLines(
generatedFile.toString(),
snapshotFile.toString()
);
// Construct an object that can be used for processing
const differences = diff
.reduce((accumulator, currentValue, currentIndex, array) => {
let line = accumulator.line;
if (currentValue.added === undefined) {
accumulator.line += currentValue.count;
}
if (currentValue.removed) {
return Object.assign(accumulator, {
line,
error: accumulator.error.concat({
line,
expected: currentValue.value,
actual: array[currentIndex + 1].value,
}),
});
}
return accumulator;
}, {
line: 1,
error: [],
});
// Throw an AssertionError for each different line found
if (differences.error.length !== 0) {
const testCase = differences.error.shift();
assert.fail(
testCase.actual,
testCase.expected,
`Contents of ${filePath}:${testCase.line} does not match snapshot`,
'!=='
);
}
};