-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsnapshots.mjs
More file actions
174 lines (145 loc) · 5.04 KB
/
snapshots.mjs
File metadata and controls
174 lines (145 loc) · 5.04 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
import fs from 'fs';
import glob from 'glob';
import { join as pathJoin } from 'path';
import { remark } from 'remark';
import remarkMdx from 'remark-mdx';
import remarkGfm from 'remark-gfm';
import remarkFrontmatter from 'remark-frontmatter';
import { spawnSync } from 'child_process';
import { diffLines } from 'diff';
import prettier from 'prettier';
import chalk from 'chalk';
const DOCS_TEST_TIMEOUT_MS = 60000;
const docsDir = new URL('.', import.meta.url).pathname;
const snapshotSeparator = '\n==> ';
const isUpdating =
process.argv.includes('--update') || process.argv.includes('--update=true');
let failures = 0;
// HACK: This function is equivalent to `return await runCode(source)`
// But I can't import the language and I've already spent hours trying.
const runCode = (sources) => {
if (sources.size === 0) {
return new Map();
}
// NOTE: this is very slow even when batching all examples in each .md file
// But we can load all markdown ASTs into memory and run this subprocess only once.
const { stdout, stderr, error } = spawnSync(
pathJoin(docsDir, '../../node_modules/.bin/babel-node'),
[
'--extensions=.js,.jsx,.ts,.tsx',
pathJoin(docsDir, '../../libs/language/src/evaluate-docs-examples.ts'),
],
{
input: JSON.stringify(Object.fromEntries(sources.entries())),
timeout: DOCS_TEST_TIMEOUT_MS,
}
);
if (stderr && stderr.length) {
// eslint-disable-next-line no-console
console.error(`STDERR: ${stderr.toString('utf-8')}`);
}
if (error) {
throw error;
} else {
const out = stdout.toString('utf-8');
try {
return new Map(Object.entries(JSON.parse(out)));
} catch (err) {
// eslint-disable-next-line no-console
console.error(`Error parsing output ${out}`);
throw err;
}
}
};
const getCodeNodes = (document) => {
return document.children
.filter(
({ type, lang, meta }) =>
type === 'code' && lang === 'deci' && meta === 'live'
)
.map((node) => {
const [code, snapshot = '(no snapshot)'] =
node.value.split(snapshotSeparator);
return { node, code, snapshot };
});
};
const getRemark = () =>
remark().use(remarkMdx).use(remarkGfm).use(remarkFrontmatter);
async function collectOneFile(codeNodesPerFile, fileName) {
const theTests = fs.readFileSync(fileName, { encoding: 'utf-8' });
await getRemark()
.use(() => (document) => {
codeNodesPerFile.set(
fileName,
getCodeNodes(document).map((node) => node.code)
);
})
.process(theTests);
}
const snapshotTestingPlugin =
(resultsContext, fileName, relativeFileName) => () => async (document) => {
const nodes = getCodeNodes(document);
const results = resultsContext.get(fileName);
for (let i = 0; i < nodes.length; i += 1) {
const { node, code, snapshot } = nodes[i];
const actualValue = results[i];
if (actualValue?.crash) {
// eslint-disable-next-line no-console
console.error(chalk.red(actualValue.crash));
} else if (snapshot !== actualValue) {
failures += 1;
// eslint-disable-next-line no-console
console.error(`--\ndifferent result in ${relativeFileName}`);
diffLines(snapshot, actualValue).forEach((part) => {
const colorizeLine = part.added
? (s) => chalk.green(`+ ${s}`)
: part.removed
? (s) => chalk.red(`- ${s}`)
: (s) => ` ${s}`;
const lines = part.value.split('\n').map(colorizeLine).join('\n');
// eslint-disable-next-line no-console
console.error(lines);
});
}
node.value = code + snapshotSeparator + actualValue;
}
};
async function testOneFile(resultsContext, fileName, relativeFileName) {
const theTests = fs.readFileSync(fileName, { encoding: 'utf-8' });
const file = await getRemark()
.use(snapshotTestingPlugin(resultsContext, fileName, relativeFileName))
.process(theTests);
if (isUpdating) {
const prettierConfig = await prettier.resolveConfig(fileName);
const newContents = await prettier.format(file.toString(), {
...prettierConfig,
filepath: fileName,
});
fs.writeFileSync(fileName, newContents);
}
}
async function runDocTests() {
const codeNodesPerFile = new Map();
for (const file of glob.sync('docs/**/*.md', { cwd: docsDir })) {
// eslint-disable-next-line no-await-in-loop
await collectOneFile(codeNodesPerFile, pathJoin(docsDir, file));
}
// Run all the tests on all the code!
const resultsContext = await runCode(codeNodesPerFile);
for (const file of glob.sync('docs/**/*.md', { cwd: docsDir })) {
// eslint-disable-next-line no-await-in-loop
await testOneFile(resultsContext, pathJoin(docsDir, file), file);
}
if (failures > 0 && !isUpdating) {
// eslint-disable-next-line no-console
console.error(
'There are failed doctests. Add --update to this command to update the snapshots.'
);
process.exit(1);
}
}
runDocTests().catch((e) => {
// eslint-disable-next-line no-console
console.error(e);
process.exit(1);
});