/
run-result.ts
119 lines (113 loc) · 3.71 KB
/
run-result.ts
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
import { Config } from '@jest/types'
import { LogMessage } from 'bs-logger'
import { SpawnSyncReturns } from 'child_process'
import { readFileSync, realpathSync } from 'fs'
import { tmpdir } from 'os'
import { resolve, sep } from 'path'
import { cacheDir } from '../../../scripts/lib/paths'
import ProcessedFileIo from './processed-file-io'
import { escapeRegex, normalizeJestOutput, stripAnsiColors } from './utils'
// tslint:disable-next-line:no-default-export
export default class RunResult {
constructor(
readonly cwd: string,
readonly result: SpawnSyncReturns<Buffer>,
readonly context: Readonly<{
ioDir?: string | undefined
cmd: string
args: string[]
env: { [key: string]: string }
config: Config.InitialOptions
digest: string
}>,
) {}
get logFilePath() {
return resolve(this.cwd, 'ts-jest.log')
}
get logFileContent() {
return readFileSync(this.logFilePath).toString('utf8')
}
get logFileEntries(): LogMessage[] {
const lines = this.logFileContent.split(/\n/g)
// remove last, empty line
lines.pop()
return lines.map(s => JSON.parse(s))
}
get isPass() {
return this.status === 0
}
get isFail() {
return !this.isPass
}
get status() {
return this.result.status
}
get output() {
return this.normalize(stripAnsiColors(this.result.output ? this.result.output.join('\n\n') : ''))
}
get stderr() {
return this.normalize(stripAnsiColors((this.result.stderr || '').toString()))
}
get normalizedStderr() {
return normalizeJestOutput(this.stderr)
}
get stdout() {
return this.normalize(stripAnsiColors((this.result.stdout || '').toString()))
}
get normalizedStdout() {
return normalizeJestOutput(this.stdout)
}
get cmdLine() {
const args = [this.context.cmd, ...this.context.args].filter(
a => !['-u', '--updateSnapshot', '--runInBand', '--'].includes(a),
)
const configIndex = args.indexOf('--config')
if (configIndex !== -1) {
args.splice(configIndex, 2)
}
return this.normalize(args.join(' '))
}
ioFor(relFilePath: string): ProcessedFileIo {
if (!this.context.ioDir) {
throw new Error('IO not written for test, you must configure the test with `writeIo: true`.')
}
let io: any = { in: ['', '', {}, {}], out: '' }
try {
io = require(`${this.context.ioDir}/${relFilePath}.json`)
} catch (err) {
io.out = `/*\nts-jest after hook has not been called!\n${err}\noutput:\n${this.output}*/`
}
return new ProcessedFileIo(this.cwd, relFilePath, io.in, io.out)
}
normalize(str: string) {
// TODO: clean this!
const cwd = this.cwd
const realCwd = realpathSync(cwd)
const tmp = tmpdir()
const realTmp = realpathSync(tmp)
const map = [
{ from: cwd, to: '<cwd>' },
{ from: tmp, to: '<tmp>' },
{ from: /\b[a-f0-9]{40}\b/g, to: '<hex:40>' },
{ from: cacheDir, to: '<ts-jest-cache>' },
]
if (cwd !== realCwd) map.push({ from: realCwd, to: '<cwd>' })
if (tmp !== realTmp) map.push({ from: realTmp, to: '<tmp>' })
if (sep === '\\') {
map.push({ from: /\\/g, to: '/' })
map.push({ from: cwd.replace(/\\/g, '/'), to: '<cwd>' })
map.push({ from: tmp.replace(/\\/g, '/'), to: '<tmp>' })
if (cwd !== realCwd) {
map.push({ from: realCwd.replace(/\\/g, '/'), to: '<cwd>' })
}
if (tmp !== realTmp) {
map.push({ from: realTmp.replace(/\\/g, '/'), to: '<tmp>' })
}
}
return map
.sort((a, b) => ((b.from as any).length || Infinity) - ((a.from as any).length || Infinity))
.reduce((str, { from, to }) => {
return str.replace(typeof from === 'string' ? new RegExp(`${escapeRegex(from)}`, 'g') : from, to)
}, str)
}
}