Skip to content

Commit 592a763

Browse files
test: use files for receiving Jest JSON output
1 parent 616c498 commit 592a763

File tree

3 files changed

+34
-16
lines changed

3 files changed

+34
-16
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ node_modules
1010
# build artifacts
1111
/dist
1212
tsconfig.tsbuildinfo
13+
e2e/**/jest-output.json
1314
e2e/**/yarn.lock
1415
e2e/**/.pnp.*
1516
e2e/**/.vscode-test

clean-e2e.mjs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@ import url from 'url'
55
import glob from 'fast-glob'
66
import fs from 'fs-extra'
77

8-
const items = await glob(['e2e/*/(yarn.lock|.vscode-test|.pnp.cjs)'], {
9-
cwd: path.dirname(url.fileURLToPath(import.meta.url)),
10-
dot: true,
11-
onlyFiles: false,
12-
absolute: true,
13-
followSymbolicLinks: false,
14-
})
8+
const items = await glob(
9+
['e2e/*/(yarn.lock|.vscode-test|.pnp.cjs|jest-output.json)'],
10+
{
11+
cwd: path.dirname(url.fileURLToPath(import.meta.url)),
12+
dot: true,
13+
onlyFiles: false,
14+
absolute: true,
15+
followSymbolicLinks: false,
16+
}
17+
)
1518

1619
for (const item of items) {
1720
await fs.remove(item)

e2e/utils.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,26 +94,40 @@ export async function prepareDir(cwd: string): Promise<void> {
9494
)
9595
}
9696

97+
async function gracefulReadFile(file: string): Promise<string> {
98+
try {
99+
return await fs.readFile(file, 'utf8')
100+
} catch (e) {
101+
return ''
102+
}
103+
}
104+
97105
export async function runJest(
98106
cwd: string,
99107
args: string[] = []
100108
): Promise<execa.ExecaChildProcess & Promise<{ json: unknown }>> {
101-
const results = await execa('jest', ['--json', ...args], {
102-
cwd,
103-
preferLocal: true,
104-
timeout: 30000,
105-
reject: false,
106-
})
109+
const results = await execa(
110+
'jest',
111+
['--json', '--outputFile', 'jest-output.json', ...args],
112+
{
113+
cwd,
114+
preferLocal: true,
115+
timeout: 30000,
116+
reject: false,
117+
}
118+
)
119+
const outputFile = path.resolve(cwd, 'jest-output.json')
120+
const output = await gracefulReadFile(outputFile)
107121

108-
if (results.stdout) {
122+
if (output.trim()) {
109123
try {
110-
const json = JSON.parse(results.stdout) as ActualAggregatedResult
124+
const json = JSON.parse(output) as ActualAggregatedResult
111125
return {
112126
...results,
113127
json: normalizeResults(json),
114128
}
115129
} catch (error) {
116-
console.error(`Failed to parse jest output: ${results.stdout}`)
130+
console.error(`Failed to parse jest output: ${output}`)
117131
throw error
118132
}
119133
}

0 commit comments

Comments
 (0)