-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
/
inline_snapshots.ts
185 lines (162 loc) · 5.04 KB
/
inline_snapshots.ts
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
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* 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';
import path from 'path';
import semver from 'semver';
import {
templateElement,
templateLiteral,
file,
CallExpression,
} from '@babel/types';
import {Frame} from 'jest-message-util';
import {Config} from '@jest/types';
import {escapeBacktickString} from './utils';
export type InlineSnapshot = {
snapshot: string;
frame: Frame;
};
export const saveInlineSnapshots = (
snapshots: Array<InlineSnapshot>,
prettier: any,
babelTraverse: Function,
) => {
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.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: Array<InlineSnapshot>,
sourceFilePath: Config.Path,
prettier: any,
babelTraverse: Function,
) => {
const sourceFile = fs.readFileSync(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.
const inferredParser = prettier.getFileInfo
? prettier.getFileInfo.sync(sourceFilePath).inferredParser
: (config && config.parser) || simpleDetectParser(sourceFilePath);
// Format the source code using the custom parser API.
const newSourceFile = prettier.format(sourceFile, {
...config,
filepath: sourceFilePath,
parser: createParser(snapshots, inferredParser, babelTraverse),
});
if (newSourceFile !== sourceFile) {
fs.writeFileSync(sourceFilePath, newSourceFile);
}
};
const groupSnapshotsBy = (
createKey: (inlineSnapshot: InlineSnapshot) => string,
) => (snapshots: Array<InlineSnapshot>) =>
snapshots.reduce<{[key: string]: Array<InlineSnapshot>}>(
(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 createParser = (
snapshots: Array<InlineSnapshot>,
inferredParser: string,
babelTraverse: Function,
) => (
text: string,
parsers: {[key: string]: (text: string) => any},
options: any,
) => {
// Workaround for https://github.com/prettier/prettier/issues/3150
options.parser = inferredParser;
const groupedSnapshots = groupSnapshotsByFrame(snapshots);
const remainingSnapshots = new Set(snapshots.map(({snapshot}) => snapshot));
let ast = parsers[inferredParser](text);
// Flow uses a 'Program' parent node, babel expects a 'File'.
if (ast.type !== 'File') {
ast = file(ast, ast.comments, ast.tokens);
delete ast.program.comments;
}
babelTraverse(ast, {
CallExpression({node: {arguments: args, callee}}: {node: CallExpression}) {
if (
callee.type !== 'MemberExpression' ||
callee.property.type !== 'Identifier'
) {
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.',
);
}
const snapshotIndex = args.findIndex(
({type}) => type === 'TemplateLiteral',
);
const values = snapshotsForFrame.map(({snapshot}) => {
remainingSnapshots.delete(snapshot);
return templateLiteral(
[templateElement({raw: 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;
};
const simpleDetectParser = (filePath: Config.Path) => {
const extname = path.extname(filePath);
if (/tsx?$/.test(extname)) {
return 'typescript';
}
return 'babylon';
};