InlineSnapshots.js
9.89 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
exports.saveInlineSnapshots = saveInlineSnapshots;
var path = _interopRequireWildcard(require('path'));
var _types = require('@babel/types');
var fs = _interopRequireWildcard(require('graceful-fs'));
var _semver = _interopRequireDefault(require('semver'));
var _utils = require('./utils');
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : {default: obj};
}
function _getRequireWildcardCache() {
if (typeof WeakMap !== 'function') return null;
var cache = new WeakMap();
_getRequireWildcardCache = function () {
return cache;
};
return cache;
}
function _interopRequireWildcard(obj) {
if (obj && obj.__esModule) {
return obj;
}
if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
return {default: obj};
}
var cache = _getRequireWildcardCache();
if (cache && cache.has(obj)) {
return cache.get(obj);
}
var newObj = {};
var hasPropertyDescriptor =
Object.defineProperty && Object.getOwnPropertyDescriptor;
for (var key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
var desc = hasPropertyDescriptor
? Object.getOwnPropertyDescriptor(obj, key)
: null;
if (desc && (desc.get || desc.set)) {
Object.defineProperty(newObj, key, desc);
} else {
newObj[key] = obj[key];
}
}
}
newObj.default = obj;
if (cache) {
cache.set(obj, newObj);
}
return newObj;
}
var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;
var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;
var jestWriteFile =
global[Symbol.for('jest-native-write-file')] || fs.writeFileSync;
var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;
var jestReadFile =
global[Symbol.for('jest-native-read-file')] || fs.readFileSync;
function saveInlineSnapshots(snapshots, prettier, babelTraverse) {
if (!prettier) {
throw new Error(
`Jest: Inline Snapshots requires Prettier.\n` +
`Please ensure "prettier" is installed in your project.`
);
} // Custom parser API was added in 1.5.0
if (_semver.default.lt(prettier.version, '1.5.0')) {
throw new Error(
`Jest: Inline Snapshots require prettier>=1.5.0.\n` +
`Please upgrade "prettier".`
);
}
const snapshotsByFile = groupSnapshotsByFile(snapshots);
for (const sourceFilePath of Object.keys(snapshotsByFile)) {
saveSnapshotsForFile(
snapshotsByFile[sourceFilePath],
sourceFilePath,
prettier,
babelTraverse
);
}
}
const saveSnapshotsForFile = (
snapshots,
sourceFilePath,
prettier,
babelTraverse
) => {
const sourceFile = jestReadFile(sourceFilePath, 'utf8'); // Resolve project configuration.
// For older versions of Prettier, do not load configuration.
const config = prettier.resolveConfig
? prettier.resolveConfig.sync(sourceFilePath, {
editorconfig: true
})
: null; // Detect the parser for the test file.
// For older versions of Prettier, fallback to a simple parser detection.
// @ts-expect-error
const inferredParser = prettier.getFileInfo
? prettier.getFileInfo.sync(sourceFilePath).inferredParser
: (config && config.parser) || simpleDetectParser(sourceFilePath); // Record the matcher names seen in insertion parser and pass them down one
// by one to formatting parser.
const snapshotMatcherNames = []; // Insert snapshots using the custom parser API. After insertion, the code is
// formatted, except snapshot indentation. Snapshots cannot be formatted until
// after the initial format because we don't know where the call expression
// will be placed (specifically its indentation).
const newSourceFile = prettier.format(sourceFile, {
...config,
filepath: sourceFilePath,
parser: createInsertionParser(
snapshots,
snapshotMatcherNames,
inferredParser,
babelTraverse
)
}); // Format the snapshots using the custom parser API.
const formattedNewSourceFile = prettier.format(newSourceFile, {
...config,
filepath: sourceFilePath,
parser: createFormattingParser(
snapshotMatcherNames,
inferredParser,
babelTraverse
)
});
if (formattedNewSourceFile !== sourceFile) {
jestWriteFile(sourceFilePath, formattedNewSourceFile);
}
};
const groupSnapshotsBy = createKey => snapshots =>
snapshots.reduce((object, inlineSnapshot) => {
const key = createKey(inlineSnapshot);
return {...object, [key]: (object[key] || []).concat(inlineSnapshot)};
}, {});
const groupSnapshotsByFrame = groupSnapshotsBy(({frame: {line, column}}) =>
typeof line === 'number' && typeof column === 'number'
? `${line}:${column - 1}`
: ''
);
const groupSnapshotsByFile = groupSnapshotsBy(({frame: {file}}) => file);
const indent = (snapshot, numIndents, indentation) => {
const lines = snapshot.split('\n'); // Prevent re-indentation of inline snapshots.
if (
lines.length >= 2 &&
lines[1].startsWith(indentation.repeat(numIndents + 1))
) {
return snapshot;
}
return lines
.map((line, index) => {
if (index === 0) {
// First line is either a 1-line snapshot or a blank line.
return line;
} else if (index !== lines.length - 1) {
// Do not indent empty lines.
if (line === '') {
return line;
} // Not last line, indent one level deeper than expect call.
return indentation.repeat(numIndents + 1) + line;
} else {
// The last line should be placed on the same level as the expect call.
return indentation.repeat(numIndents) + line;
}
})
.join('\n');
};
const getAst = (parsers, inferredParser, text) => {
// Flow uses a 'Program' parent node, babel expects a 'File'.
let ast = parsers[inferredParser](text);
if (ast.type !== 'File') {
ast = (0, _types.file)(ast, ast.comments, ast.tokens);
delete ast.program.comments;
}
return ast;
}; // This parser inserts snapshots into the AST.
const createInsertionParser = (
snapshots,
snapshotMatcherNames,
inferredParser,
babelTraverse
) => (text, parsers, options) => {
// Workaround for https://github.com/prettier/prettier/issues/3150
options.parser = inferredParser;
const groupedSnapshots = groupSnapshotsByFrame(snapshots);
const remainingSnapshots = new Set(snapshots.map(({snapshot}) => snapshot));
const ast = getAst(parsers, inferredParser, text);
babelTraverse(ast, {
CallExpression({node: {arguments: args, callee}}) {
if (
callee.type !== 'MemberExpression' ||
callee.property.type !== 'Identifier' ||
callee.property.loc == null
) {
return;
}
const {line, column} = callee.property.loc.start;
const snapshotsForFrame = groupedSnapshots[`${line}:${column}`];
if (!snapshotsForFrame) {
return;
}
if (snapshotsForFrame.length > 1) {
throw new Error(
'Jest: Multiple inline snapshots for the same call are not supported.'
);
}
snapshotMatcherNames.push(callee.property.name);
const snapshotIndex = args.findIndex(
({type}) => type === 'TemplateLiteral'
);
const values = snapshotsForFrame.map(({snapshot}) => {
remainingSnapshots.delete(snapshot);
return (0, _types.templateLiteral)(
[
(0, _types.templateElement)({
raw: (0, _utils.escapeBacktickString)(snapshot)
})
],
[]
);
});
const replacementNode = values[0];
if (snapshotIndex > -1) {
args[snapshotIndex] = replacementNode;
} else {
args.push(replacementNode);
}
}
});
if (remainingSnapshots.size) {
throw new Error(`Jest: Couldn't locate all inline snapshots.`);
}
return ast;
}; // This parser formats snapshots to the correct indentation.
const createFormattingParser = (
snapshotMatcherNames,
inferredParser,
babelTraverse
) => (text, parsers, options) => {
// Workaround for https://github.com/prettier/prettier/issues/3150
options.parser = inferredParser;
const ast = getAst(parsers, inferredParser, text);
babelTraverse(ast, {
CallExpression({node: {arguments: args, callee}}) {
var _options$tabWidth, _options$tabWidth2;
if (
callee.type !== 'MemberExpression' ||
callee.property.type !== 'Identifier' ||
!snapshotMatcherNames.includes(callee.property.name) ||
!callee.loc ||
callee.computed
) {
return;
}
let snapshotIndex;
let snapshot;
for (let i = 0; i < args.length; i++) {
const node = args[i];
if (node.type === 'TemplateLiteral') {
snapshotIndex = i;
snapshot = node.quasis[0].value.raw;
}
}
if (snapshot === undefined || snapshotIndex === undefined) {
return;
}
const useSpaces = !options.useTabs;
snapshot = indent(
snapshot,
Math.ceil(
useSpaces
? callee.loc.start.column /
((_options$tabWidth = options.tabWidth) !== null &&
_options$tabWidth !== void 0
? _options$tabWidth
: 1)
: callee.loc.start.column / 2 // Each tab is 2 characters.
),
useSpaces
? ' '.repeat(
(_options$tabWidth2 = options.tabWidth) !== null &&
_options$tabWidth2 !== void 0
? _options$tabWidth2
: 1
)
: '\t'
);
const replacementNode = (0, _types.templateLiteral)(
[
(0, _types.templateElement)({
raw: snapshot
})
],
[]
);
args[snapshotIndex] = replacementNode;
}
});
return ast;
};
const simpleDetectParser = filePath => {
const extname = path.extname(filePath);
if (/tsx?$/.test(extname)) {
return 'typescript';
}
return 'babel';
};