-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjest-transform.js
138 lines (121 loc) · 3.84 KB
/
jest-transform.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
// @ts-check
const path = require('path')
const { SourceMapGenerator } = require('source-map')
/** @param {string[]} lines */
const parseMarkdown = (lines) => {
const CODE = 'code'
const HEADING = 'heading'
const NORMAL = 'normal'
let currentNodeType = NORMAL
/** @typedef {{line: number, contents}} Line */
/** @type {{nodeType: string, lines: Line[]}[]} */
const chunks = []
for (const [i, element] of lines.entries()) {
/** @type {Line} */
const line = { line: i + 1, contents: element }
const lastChunk = chunks[chunks.length - 1]
if (currentNodeType === NORMAL) {
if (line.contents.trim() === '') continue
if (line.contents.startsWith('# ')) {
chunks.push({ nodeType: HEADING, lines: [line] })
} else if (line.contents.startsWith('```js')) {
currentNodeType = CODE
chunks.push({ nodeType: CODE, lines: [] })
} else if (lastChunk.nodeType === NORMAL) {
lastChunk.lines.push(line)
} else {
chunks.push({ nodeType: NORMAL, lines: [line] })
}
} else if (currentNodeType === CODE) {
if (line.contents.startsWith('```')) {
currentNodeType = NORMAL
} else {
lastChunk.lines.push(line)
}
}
}
/** @typedef {{input: Line[], expected: Line[], line: number}} Assertion */
/** @typedef {{name: string, assertions: Assertion[], flag?: string}} Test */
/** @type {Test[]} */
const tests = []
/** @type {Test | undefined} */
let currentTest
for (let i = 0; i < chunks.length; i++) {
const prev = chunks[i - 1]
const current = chunks[i]
const next = chunks[i + 1]
if (current.nodeType === HEADING) {
if (currentTest) tests.push(currentTest)
const testName = current.lines[0].contents.replace(/^# /, '')
const SKIP = '(skip)'
const ONLY = '(only)'
const flag = testName.includes(SKIP)
? 'skip'
: testName.includes(ONLY)
? 'only'
: undefined
currentTest = {
name: testName.replace(SKIP, '').replace(ONLY, '').trim(),
assertions: [],
flag,
}
continue
}
// code, then "to", then code is an assertion
if (
currentTest &&
prev &&
next &&
prev.nodeType === CODE &&
next.nodeType === CODE &&
current.nodeType === NORMAL &&
current.lines.length === 1 &&
current.lines[0].contents === 'to'
) {
currentTest.assertions.push({
input: prev.lines,
expected: next.lines,
line: current.lines[0].line,
})
}
}
if (currentTest) tests.push(currentTest)
return tests
}
/** @type {import('@jest/transform').Transformer} */
const transformer = {
process(src, filename, config) {
const lines = src.split('\n')
const tests = parseMarkdown(lines)
const map = new SourceMapGenerator()
const transformPath = JSON.stringify(require.resolve('./test-util.ts'))
let code = `const transform = require(${transformPath}).default`
for (const test of tests) {
const flag = test.flag ? `.${test.flag}` : ''
code += `\ntest${flag}(${JSON.stringify(test.name)}, async () => {\n`
for (const assertion of test.assertions) {
const input = JSON.stringify(
assertion.input.map((l) => l.contents).join('\n'),
)
const expected = JSON.stringify(
assertion.expected.map((l) => l.contents).join('\n') + '\n',
)
code += `\nexpect(await transform(${input})).toEqual(${expected})`
map.addMapping({
generated: {
line: code.split('\n').length,
column: 0,
},
source: path.resolve(config.rootDir, filename),
original: {
line: assertion.line,
column: 0,
},
})
}
code += '\n})'
}
return { code, map: map.toJSON() }
},
}
module.exports = transformer