/
utils.ts
54 lines (48 loc) · 1.87 KB
/
utils.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
import { existsSync } from 'fs-extra'
import { join } from 'path'
// from https://stackoverflow.com/questions/25245716/remove-all-ansi-colors-styles-from-strings
export function stripAnsiColors(stringToStrip: string): string {
// eslint-disable-next-line no-control-regex
return stringToStrip.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, '')
}
export function templateNameForPath(path: string): string {
const e2eFile = join(path, '.ts-jest-e2e.json')
if (existsSync(e2eFile)) {
return require(e2eFile).template || 'default'
}
return 'default'
}
// first one of each must be the most compatible one
const PASS_MARKS = ['√', '✓']
const FAIL_MARKS = ['×', '✕']
const normalizeTestMark = (mark: string): string => {
if (PASS_MARKS.includes(mark)) return PASS_MARKS[0]
if (FAIL_MARKS.includes(mark)) return FAIL_MARKS[0]
return '?'
}
export function normalizeJestOutput(output: string): string {
let out: string = output
.trim()
// removes total and estimated times
.replace(/^(\s*Time\s*:\s*)[\d.]+ m?s(?:(,\s*estimated\s+)[\d.]+ m?s)?$/gm, (_, start) => `${start}XXs`)
// remove times after PASS/FAIL path/to/file (xxxs)
.replace(/^\s*((?:PASS|FAIL) .+) \([\d.]+m?s\)$/gm, (_, start) => `${start}`)
// removes each test time values
.replace(
// eslint-disable-next-line no-useless-escape
/^(\s*)(✕|×|✓|√)(\s+[^\(]+)(\s+\([\d.]+ m?s\))?$/gm,
(_, start, mark, mid /* , time */) => `${start}${normalizeTestMark(mark)}${mid}`,
)
// TODO: improves this...
if (process.platform === 'win32') {
out = out.replace(/\\/g, '/')
}
return out
}
export function escapeRegex(s: string) {
// eslint-disable-next-line no-useless-escape
return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')
}
export function enableOptimizations() {
return !!process.env.TS_JEST_E2E_OPTIMIZATIONS
}