@provino/dialect-jest 0.0.0-stage → 0.1.0-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +8 -2
- package/THIRD_PARTY_NOTICES.md +266 -0
- package/dist/affected/index.d.ts +13 -0
- package/dist/affected/index.js +424 -0
- package/dist/failure-message.d.ts +245 -0
- package/dist/failure-message.js +125 -0
- package/dist/global-setup/index.d.ts +11 -0
- package/dist/global-setup/index.js +40 -0
- package/dist/index.d.ts +41 -0
- package/dist/index.js +528 -0
- package/dist/inline-snapshots/index.d.ts +6 -0
- package/dist/inline-snapshots/index.js +263 -0
- package/dist/jest-require.js +42 -0
- package/dist/node/index.d.ts +1716 -0
- package/dist/node/index.js +7307 -0
- package/dist/results/index.d.ts +248 -0
- package/dist/results/index.js +844 -0
- package/dist/runtime/index.d.ts +450 -0
- package/dist/runtime/index.js +3455 -0
- package/package.json +97 -3
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { readFileSync, writeFileSync } from "node:fs";
|
|
4
|
+
|
|
5
|
+
// src/inline-snapshots/babel.ts
|
|
6
|
+
const own = createRequire(import.meta.url);
|
|
7
|
+
/** `name` from the project's root first, then from this package. */
|
|
8
|
+
function load(name, rootDir) {
|
|
9
|
+
let path;
|
|
10
|
+
try {
|
|
11
|
+
path = createRequire(join(rootDir, "package.json")).resolve(name);
|
|
12
|
+
} catch {
|
|
13
|
+
path = own.resolve(name);
|
|
14
|
+
}
|
|
15
|
+
return own(path);
|
|
16
|
+
}
|
|
17
|
+
function isRecord(value) {
|
|
18
|
+
return typeof value === "object" && value !== null || typeof value === "function";
|
|
19
|
+
}
|
|
20
|
+
/** A function member, or an error that names the module that lacks it. */
|
|
21
|
+
function member(module, key, name) {
|
|
22
|
+
const value = isRecord(module) ? module[key] : void 0;
|
|
23
|
+
if (typeof value !== "function") throw new TypeError(`${name} has no ${key}: it is not a Babel 7 the inline snapshot writer can use`);
|
|
24
|
+
return value;
|
|
25
|
+
}
|
|
26
|
+
const cache = /* @__PURE__ */ new Map();
|
|
27
|
+
/** Babel for a project, loaded once per root. */
|
|
28
|
+
function babelFor(rootDir) {
|
|
29
|
+
const cached = cache.get(rootDir);
|
|
30
|
+
if (cached !== void 0) return cached;
|
|
31
|
+
const core = load("@babel/core", rootDir);
|
|
32
|
+
const generator = load("@babel/generator", rootDir);
|
|
33
|
+
const types = isRecord(core) ? core.types : void 0;
|
|
34
|
+
const generate = isRecord(generator) && "default" in generator ? generator.default : generator;
|
|
35
|
+
if (typeof generate !== "function") throw new TypeError("@babel/generator exports no function: it is not a Babel 7 generator");
|
|
36
|
+
const babel = {
|
|
37
|
+
parseSync: member(core, "parseSync", "@babel/core"),
|
|
38
|
+
generate,
|
|
39
|
+
traverseFast: member(types, "traverseFast", "@babel/core types"),
|
|
40
|
+
traverse: member(types, "traverse", "@babel/core types"),
|
|
41
|
+
isAwaitExpression: member(types, "isAwaitExpression", "@babel/core types"),
|
|
42
|
+
templateLiteral: member(types, "templateLiteral", "@babel/core types"),
|
|
43
|
+
templateElement: member(types, "templateElement", "@babel/core types"),
|
|
44
|
+
preset: own.resolve("babel-preset-current-node-syntax"),
|
|
45
|
+
typescriptPlugin: own.resolve("@babel/plugin-syntax-typescript"),
|
|
46
|
+
jsxPlugin: own.resolve("@babel/plugin-syntax-jsx")
|
|
47
|
+
};
|
|
48
|
+
cache.set(rootDir, babel);
|
|
49
|
+
return babel;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// src/inline-snapshots/index.ts
|
|
53
|
+
/** `@jest/snapshot-utils`' `escapeBacktickString`. */
|
|
54
|
+
function escapeBacktickString(text) {
|
|
55
|
+
return text.replaceAll(/`|\\|\$\{/g, "\\$&");
|
|
56
|
+
}
|
|
57
|
+
/** jest-snapshot's `indent` (`utils.ts` L137-L169). */
|
|
58
|
+
function indent(snapshot, numIndents, indentation) {
|
|
59
|
+
const lines = snapshot.split("\n");
|
|
60
|
+
if (lines.length >= 2 && lines[1]?.startsWith(indentation.repeat(numIndents + 1))) return snapshot;
|
|
61
|
+
return lines.map((line, index) => {
|
|
62
|
+
if (index === 0) return line;
|
|
63
|
+
if (index === lines.length - 1) return indentation.repeat(numIndents) + line;
|
|
64
|
+
if (line === "") return line;
|
|
65
|
+
return indentation.repeat(numIndents + 1) + line;
|
|
66
|
+
}).join("\n");
|
|
67
|
+
}
|
|
68
|
+
/** Parses a source file as jest-snapshot does (`utils.ts` L221-L262). */
|
|
69
|
+
function parse(babel, code, file, rootDir) {
|
|
70
|
+
const presets = [babel.preset];
|
|
71
|
+
const plugins = [];
|
|
72
|
+
if (/\.([cm]?ts|tsx)$/.test(file)) plugins.push([
|
|
73
|
+
babel.typescriptPlugin,
|
|
74
|
+
{ isTSX: file.endsWith("x") },
|
|
75
|
+
"TypeScript syntax plugin added by Jest snapshot"
|
|
76
|
+
]);
|
|
77
|
+
let ast;
|
|
78
|
+
try {
|
|
79
|
+
ast = babel.parseSync(code, {
|
|
80
|
+
filename: file,
|
|
81
|
+
plugins,
|
|
82
|
+
presets,
|
|
83
|
+
root: rootDir
|
|
84
|
+
});
|
|
85
|
+
} catch (error) {
|
|
86
|
+
if (!(error instanceof Error) || !error.message.includes("@babel/plugin-syntax-jsx")) throw error;
|
|
87
|
+
const jsx = [
|
|
88
|
+
babel.jsxPlugin,
|
|
89
|
+
{},
|
|
90
|
+
"JSX syntax plugin added by Jest snapshot"
|
|
91
|
+
];
|
|
92
|
+
try {
|
|
93
|
+
ast = babel.parseSync(code, {
|
|
94
|
+
filename: file,
|
|
95
|
+
plugins: [...plugins, jsx],
|
|
96
|
+
presets,
|
|
97
|
+
root: rootDir
|
|
98
|
+
});
|
|
99
|
+
} catch {
|
|
100
|
+
throw error;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
if (ast === null) throw new Error(`jest-snapshot: Failed to parse ${file}`);
|
|
104
|
+
return ast;
|
|
105
|
+
}
|
|
106
|
+
/** The edits grouped by `line:column` of the matcher's name (`groupSnapshotsByFrame`). */
|
|
107
|
+
function groupByFrame(edits) {
|
|
108
|
+
const groups = /* @__PURE__ */ new Map();
|
|
109
|
+
for (const edit of edits) {
|
|
110
|
+
const key = `${edit.line}:${edit.column}`;
|
|
111
|
+
groups.set(key, [...groups.get(key) ?? [], edit]);
|
|
112
|
+
}
|
|
113
|
+
return groups;
|
|
114
|
+
}
|
|
115
|
+
/** Whether a call's callee is `x.name` with `name` an identifier and not computed. */
|
|
116
|
+
function memberCallee(node) {
|
|
117
|
+
if (node.type !== "CallExpression") return void 0;
|
|
118
|
+
const { callee } = node;
|
|
119
|
+
if (callee?.type !== "MemberExpression") return void 0;
|
|
120
|
+
const { property } = callee;
|
|
121
|
+
if (property?.type !== "Identifier") return void 0;
|
|
122
|
+
return {
|
|
123
|
+
property,
|
|
124
|
+
callee
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* jest-snapshot's `traverseAst` (`utils.ts` L367-L418): finds each edit's call by its frame, puts
|
|
129
|
+
* the snapshot in as a template literal (replacing the first template or string argument, or
|
|
130
|
+
* appending), and returns the call node of each edit and the matcher names in order.
|
|
131
|
+
*/
|
|
132
|
+
function placeEdits(babel, edits, ast) {
|
|
133
|
+
const grouped = groupByFrame(edits);
|
|
134
|
+
const remaining = new Set(edits.map(({ snapshot }) => snapshot));
|
|
135
|
+
const nodes = /* @__PURE__ */ new Map();
|
|
136
|
+
const names = [];
|
|
137
|
+
babel.traverseFast(ast, (node) => {
|
|
138
|
+
const member = memberCallee(node);
|
|
139
|
+
if (member === void 0 || member.property.loc == null) return;
|
|
140
|
+
const { line, column } = member.property.loc.start;
|
|
141
|
+
const forFrame = grouped.get(`${line}:${column}`);
|
|
142
|
+
if (forFrame === void 0) return;
|
|
143
|
+
if (forFrame.length > 1) throw new Error("Jest: Multiple inline snapshots for the same call are not supported.");
|
|
144
|
+
const [edit] = forFrame;
|
|
145
|
+
if (edit === void 0) return;
|
|
146
|
+
nodes.set(edit, node);
|
|
147
|
+
names.push(member.property.name ?? "");
|
|
148
|
+
const args = node.arguments ?? [];
|
|
149
|
+
const index = args.findIndex(({ type }) => type === "TemplateLiteral" || type === "StringLiteral");
|
|
150
|
+
remaining.delete(edit.snapshot);
|
|
151
|
+
const replacement = babel.templateLiteral([babel.templateElement({ raw: escapeBacktickString(edit.snapshot) })], []);
|
|
152
|
+
if (index === -1) args.push(replacement);
|
|
153
|
+
else args[index] = replacement;
|
|
154
|
+
});
|
|
155
|
+
if (remaining.size > 0) throw new Error("Jest: Couldn't locate all inline snapshots.");
|
|
156
|
+
return {
|
|
157
|
+
nodes,
|
|
158
|
+
names
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* `processInlineSnapshotsWithBabel`: the source with every edit's call reprinted, substituted from
|
|
163
|
+
* the end of the source backwards so the offsets stay true. Jest substitutes in the order its
|
|
164
|
+
* edits were recorded (`reduceRight` over them): the same when they were recorded top to bottom,
|
|
165
|
+
* and a corrupted file when they weren't (a helper defined above its callers records its call
|
|
166
|
+
* after theirs). Here the position decides, never the recording order.
|
|
167
|
+
*/
|
|
168
|
+
function withSnapshots(babel, edits, file, source, rootDir) {
|
|
169
|
+
const { nodes, names } = placeEdits(babel, edits, parse(babel, source, file, rootDir));
|
|
170
|
+
const startOf = (edit) => nodes.get(edit)?.start ?? -1;
|
|
171
|
+
return {
|
|
172
|
+
text: [...edits].sort((a, b) => startOf(b) - startOf(a)).reduce((sofar, edit) => {
|
|
173
|
+
const node = nodes.get(edit);
|
|
174
|
+
if (node === void 0 || typeof node.start !== "number" || typeof node.end !== "number") throw new Error("Jest: no snapshot insert location found");
|
|
175
|
+
if (node.loc != null) node.loc.end.line = node.loc.start.line;
|
|
176
|
+
return sofar.slice(0, node.start) + babel.generate(node, { retainLines: true }).code.trim() + sofar.slice(node.end);
|
|
177
|
+
}, source),
|
|
178
|
+
names
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
function isPrettier(value) {
|
|
182
|
+
return typeof value === "object" && value !== null && typeof Reflect.get(value, "version") === "string" && typeof Reflect.get(value, "format") === "function";
|
|
183
|
+
}
|
|
184
|
+
/** The project's Prettier, or `undefined` when it isn't there (Jest ignores `MODULE_NOT_FOUND`). */
|
|
185
|
+
function loadPrettier(prettierPath) {
|
|
186
|
+
let module;
|
|
187
|
+
try {
|
|
188
|
+
module = createRequire(import.meta.url)(prettierPath);
|
|
189
|
+
} catch (error) {
|
|
190
|
+
if (error.code === "MODULE_NOT_FOUND") return void 0;
|
|
191
|
+
throw error;
|
|
192
|
+
}
|
|
193
|
+
return isPrettier(module) ? module : void 0;
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Reindents each matcher's template to its call, as `processPrettierAst` does on Prettier's AST
|
|
197
|
+
* (`utils.ts` L287-L345, with `keepNode`): the column of an `await` parent, else the callee's, in
|
|
198
|
+
* units of `tabWidth` spaces (1 when the config has none) or of tabs (2 columns each).
|
|
199
|
+
*/
|
|
200
|
+
function reindented(babel, text, file, rootDir, config, names) {
|
|
201
|
+
const ast = parse(babel, text, file, rootDir);
|
|
202
|
+
const splices = [];
|
|
203
|
+
babel.traverse(ast, (node, ancestors) => {
|
|
204
|
+
const member = memberCallee(node);
|
|
205
|
+
if (member === void 0 || !names.includes(member.property.name ?? "") || member.callee.loc == null || member.callee.computed === true) return;
|
|
206
|
+
let template;
|
|
207
|
+
for (const arg of node.arguments ?? []) if (arg.type === "TemplateLiteral") template = arg;
|
|
208
|
+
const quasi = template?.quasis?.[0];
|
|
209
|
+
if (quasi === void 0) return;
|
|
210
|
+
const parent = ancestors.at(-1)?.node;
|
|
211
|
+
const startColumn = parent !== void 0 && babel.isAwaitExpression(parent) && parent.loc != null ? parent.loc.start.column : member.callee.loc.start.column;
|
|
212
|
+
const useSpaces = config?.useTabs !== true;
|
|
213
|
+
const tabWidth = typeof config?.tabWidth === "number" ? config.tabWidth : 1;
|
|
214
|
+
const raw = indent(quasi.value.raw, Math.ceil(useSpaces ? startColumn / tabWidth : startColumn / 2), useSpaces ? " ".repeat(tabWidth) : " ");
|
|
215
|
+
if (typeof quasi.start === "number" && typeof quasi.end === "number") splices.push({
|
|
216
|
+
start: quasi.start,
|
|
217
|
+
end: quasi.end,
|
|
218
|
+
raw
|
|
219
|
+
});
|
|
220
|
+
});
|
|
221
|
+
return splices.sort((a, b) => b.start - a.start).reduce((sofar, { start, end, raw }) => sofar.slice(0, start) + raw + sofar.slice(end), text);
|
|
222
|
+
}
|
|
223
|
+
/** Jest's Prettier 3 path (`worker.ts`), through `format` twice. */
|
|
224
|
+
async function formatted(prettier, babel, file, text, names, rootDir) {
|
|
225
|
+
const config = await prettier.resolveConfig(file, { editorconfig: true });
|
|
226
|
+
const parser = typeof config?.parser === "string" ? config.parser : (await prettier.getFileInfo(file)).inferredParser;
|
|
227
|
+
if (!parser) throw new Error(`Could not infer Prettier parser for file ${file}`);
|
|
228
|
+
const options = {
|
|
229
|
+
...config,
|
|
230
|
+
filepath: file,
|
|
231
|
+
parser
|
|
232
|
+
};
|
|
233
|
+
const once = await prettier.format(text, options);
|
|
234
|
+
return await prettier.format(reindented(babel, once, file, rootDir, config, names), options);
|
|
235
|
+
}
|
|
236
|
+
/** The major version of a `x.y.z` string. */
|
|
237
|
+
function major(version) {
|
|
238
|
+
return Number.parseInt(version, 10);
|
|
239
|
+
}
|
|
240
|
+
/** `saveInlineSnapshots`: every file's edits, one file at a time. */
|
|
241
|
+
async function writeInlineSnapshots(edits, options) {
|
|
242
|
+
const warnings = [];
|
|
243
|
+
const prettier = options.prettierPath === null ? void 0 : loadPrettier(options.prettierPath);
|
|
244
|
+
const byFile = /* @__PURE__ */ new Map();
|
|
245
|
+
for (const edit of edits) byFile.set(edit.file, [...byFile.get(edit.file) ?? [], edit]);
|
|
246
|
+
for (const [file, fileEdits] of byFile) {
|
|
247
|
+
const babel = babelFor(options.rootDir);
|
|
248
|
+
const source = readFileSync(file, "utf8");
|
|
249
|
+
const { text, names } = withSnapshots(babel, fileEdits, file, source, options.rootDir);
|
|
250
|
+
let next = text;
|
|
251
|
+
if (prettier !== void 0 && major(prettier.version) >= 3) next = await formatted(prettier, babel, file, text, names, options.rootDir);
|
|
252
|
+
else if (prettier !== void 0) warnings.push(`Inline snapshots in ${file} were written without formatting: Provino formats them with Prettier 3 only, and the project's Prettier is ${prettier.version}.`);
|
|
253
|
+
if (next !== source) writeFileSync(file, next);
|
|
254
|
+
}
|
|
255
|
+
return warnings;
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* The Jest dialect's inline snapshot writer (`snapshot.inline.writer`).
|
|
259
|
+
* @public
|
|
260
|
+
*/
|
|
261
|
+
const jestInlineSnapshotWriter = () => writeInlineSnapshots;
|
|
262
|
+
|
|
263
|
+
export { jestInlineSnapshotWriter };
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { realpathSync } from "node:fs";
|
|
4
|
+
|
|
5
|
+
// src/jest-require.ts
|
|
6
|
+
function tryRequireResolve(from, specifier) {
|
|
7
|
+
try {
|
|
8
|
+
return createRequire(from).resolve(specifier);
|
|
9
|
+
} catch {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Finds the `jest-config` a project would run with, from its root: `jest-config` itself, else
|
|
15
|
+
* through `jest`, `jest-cli` (and `@jest/core`). Returns a `require.resolve` from there.
|
|
16
|
+
* @public
|
|
17
|
+
*/
|
|
18
|
+
function projectJestRequireResolve(rootDir) {
|
|
19
|
+
let located;
|
|
20
|
+
const locate = () => {
|
|
21
|
+
if (located !== void 0) return located;
|
|
22
|
+
const start = join(rootDir, "__provino_jest_config__.js");
|
|
23
|
+
const direct = tryRequireResolve(start, "jest-config/package.json");
|
|
24
|
+
if (direct !== void 0) {
|
|
25
|
+
located = direct;
|
|
26
|
+
return located;
|
|
27
|
+
}
|
|
28
|
+
const jest = tryRequireResolve(start, "jest/package.json");
|
|
29
|
+
const real = (path) => path === void 0 ? void 0 : realpathSync(path);
|
|
30
|
+
const cli = jest === void 0 ? void 0 : real(tryRequireResolve(real(jest) ?? jest, "jest-cli/package.json"));
|
|
31
|
+
const viaCli = cli === void 0 ? void 0 : tryRequireResolve(cli, "jest-config/package.json");
|
|
32
|
+
const core = viaCli === void 0 && cli !== void 0 ? real(tryRequireResolve(cli, "@jest/core/package.json")) : void 0;
|
|
33
|
+
located = viaCli ?? (core === void 0 ? void 0 : tryRequireResolve(core, "jest-config/package.json")) ?? null;
|
|
34
|
+
return located;
|
|
35
|
+
};
|
|
36
|
+
return (specifier) => {
|
|
37
|
+
const from = locate();
|
|
38
|
+
return from === null ? void 0 : tryRequireResolve(realpathSync(from), specifier);
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export { projectJestRequireResolve as t };
|