-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
160 lines (140 loc) · 4.57 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/* istanbul ignore file */
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
const { diff } = require('jest-diff');
const mkdirp = require('mkdirp');
const filenamify = require('filenamify');
/**
* Check if 2 strings or buffer are equal
* @param {string | Buffer} a
* @param {string | Buffer} b
*/
const isEqual = (a, b) => {
// @ts-ignore: TypeScript gives error if we pass string to buffer.equals
return Buffer.isBuffer(a) ? a.equals(b) : a === b;
};
/**
* Match given content against content of the specified file.
*
* @param {string | Buffer} content Output content to match
* @param {string} [filepath] Path to the file to match against
* @param {{
* diff?: import('jest-diff').DiffOptions,
* diffMethod?: (a: string, b: string, options: import('jest-diff').DiffOptions) => string,
* fileExtension?: string,
* }} options
* @this {{ testPath: string, currentTestName: string, assertionCalls: number, isNot: boolean, snapshotState: { added: number, updated: number, unmatched: number, _updateSnapshot: 'none' | 'new' | 'all' } }}
*/
exports.toMatchFile = function toMatchFile(content, filepath, options = {}) {
const { isNot, snapshotState } = this;
const fileExtension = options.fileExtension || '';
const filename =
filepath === undefined
? // If file name is not specified, generate one from the test title
path.join(
path.dirname(this.testPath),
'__file_snapshots__',
`${filenamify(
this.currentTestName,
{
replacement: '-',
},
{ maxLength: Infinity }
).replace(/\s/g, '-')}-${this.assertionCalls}`
) + fileExtension
: filepath;
options = {
// Options for jest-diff
diff: Object.assign(
{
expand: false,
contextLines: 5,
aAnnotation: 'Snapshot',
},
options.diff
),
};
if (snapshotState._updateSnapshot === 'none' && !fs.existsSync(filename)) {
// We're probably running in CI environment
snapshotState.unmatched++;
return {
pass: isNot,
message: () =>
`New output file ${chalk.blue(
path.basename(filename)
)} was ${chalk.bold.red('not written')}.\n\n` +
'The update flag must be explicitly passed to write a new snapshot.\n\n' +
`This is likely because this test is run in a ${chalk.blue(
'continuous integration (CI) environment'
)} in which snapshots are not written by default.\n\n`,
};
}
if (fs.existsSync(filename)) {
const output = fs.readFileSync(
filename,
Buffer.isBuffer(content) ? null : 'utf8'
);
if (isNot) {
// The matcher is being used with `.not`
if (!isEqual(content, output)) {
// The value of `pass` is reversed when used with `.not`
return { pass: false, message: () => '' };
} else {
snapshotState.unmatched++;
return {
pass: true,
message: () =>
`Expected received content ${chalk.red(
'to not match'
)} the file ${chalk.blue(path.basename(filename))}.`,
};
}
} else {
if (isEqual(content, output)) {
return { pass: true, message: () => '' };
} else {
if (snapshotState._updateSnapshot === 'all') {
mkdirp.sync(path.dirname(filename));
fs.writeFileSync(filename, content);
snapshotState.updated++;
return { pass: true, message: () => '' };
} else {
snapshotState.unmatched++;
const diffMethod = options.diffMethod || diff;
const difference =
Buffer.isBuffer(content) || Buffer.isBuffer(output)
? ''
: `\n\n${diffMethod(output, content, options.diff)}`;
return {
pass: false,
message: () =>
`Received content ${chalk.red(
"doesn't match"
)} the file ${chalk.blue(path.basename(filename))}.${difference}`,
};
}
}
}
} else {
if (
!isNot &&
(snapshotState._updateSnapshot === 'new' ||
snapshotState._updateSnapshot === 'all')
) {
mkdirp.sync(path.dirname(filename));
fs.writeFileSync(filename, content);
snapshotState.added++;
return { pass: true, message: () => '' };
} else {
snapshotState.unmatched++;
return {
pass: true,
message: () =>
`The output file ${chalk.blue(
path.basename(filename)
)} ${chalk.bold.red("doesn't exist")}.`,
};
}
}
};