Skip to content

Commit

Permalink
test: use files for receiving Jest JSON output
Browse files Browse the repository at this point in the history
  • Loading branch information
adalinesimonian committed Dec 8, 2021
1 parent 616c498 commit 592a763
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ node_modules
# build artifacts
/dist
tsconfig.tsbuildinfo
e2e/**/jest-output.json
e2e/**/yarn.lock
e2e/**/.pnp.*
e2e/**/.vscode-test
Expand Down
17 changes: 10 additions & 7 deletions clean-e2e.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import url from 'url'
import glob from 'fast-glob'
import fs from 'fs-extra'

const items = await glob(['e2e/*/(yarn.lock|.vscode-test|.pnp.cjs)'], {
cwd: path.dirname(url.fileURLToPath(import.meta.url)),
dot: true,
onlyFiles: false,
absolute: true,
followSymbolicLinks: false,
})
const items = await glob(
['e2e/*/(yarn.lock|.vscode-test|.pnp.cjs|jest-output.json)'],
{
cwd: path.dirname(url.fileURLToPath(import.meta.url)),
dot: true,
onlyFiles: false,
absolute: true,
followSymbolicLinks: false,
}
)

for (const item of items) {
await fs.remove(item)
Expand Down
32 changes: 23 additions & 9 deletions e2e/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,26 +94,40 @@ export async function prepareDir(cwd: string): Promise<void> {
)
}

async function gracefulReadFile(file: string): Promise<string> {
try {
return await fs.readFile(file, 'utf8')
} catch (e) {
return ''
}
}

export async function runJest(
cwd: string,
args: string[] = []
): Promise<execa.ExecaChildProcess & Promise<{ json: unknown }>> {
const results = await execa('jest', ['--json', ...args], {
cwd,
preferLocal: true,
timeout: 30000,
reject: false,
})
const results = await execa(
'jest',
['--json', '--outputFile', 'jest-output.json', ...args],
{
cwd,
preferLocal: true,
timeout: 30000,
reject: false,
}
)
const outputFile = path.resolve(cwd, 'jest-output.json')
const output = await gracefulReadFile(outputFile)

if (results.stdout) {
if (output.trim()) {
try {
const json = JSON.parse(results.stdout) as ActualAggregatedResult
const json = JSON.parse(output) as ActualAggregatedResult
return {
...results,
json: normalizeResults(json),
}
} catch (error) {
console.error(`Failed to parse jest output: ${results.stdout}`)
console.error(`Failed to parse jest output: ${output}`)
throw error
}
}
Expand Down

0 comments on commit 592a763

Please sign in to comment.