-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.ts
161 lines (143 loc) · 4.52 KB
/
index.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import * as fs from 'fs'
import * as path from 'path'
import * as stripANSI from 'strip-ansi'
import { DangerDSLType } from '../node_modules/danger/distribution/dsl/DangerDSL'
import {
IInsideFileTestResults,
IJestTestOldResults,
IJestTestResults,
} from './types'
declare var danger: DangerDSLType
declare function fail(message?: string): void
declare function message(message?: string): void
export interface IPluginConfig {
testResultsJsonPath: string
showSuccessMessage: boolean
}
export default function jest(config: Partial<IPluginConfig> = {}) {
const {
testResultsJsonPath = 'test-results.json',
showSuccessMessage = false,
} = config
try {
const jsonFileContents = fs.readFileSync(testResultsJsonPath, 'utf8')
const jsonResults: IJestTestResults = JSON.parse(jsonFileContents)
if (jsonResults.success) {
jestSuccessFeedback(jsonResults, showSuccessMessage)
return
}
const isModernFormatResults = jsonResults.testResults[0].testResults
if (isModernFormatResults) {
presentErrorsForNewStyleResults(jsonResults)
} else {
presentErrorsForOldStyleResults(jsonResults as any)
}
} catch (e) {
// tslint:disable-next-line:no-console
console.error(e)
fail(
'[danger-plugin-jest] Could not read test results. Danger cannot pass or fail the build.'
)
}
}
const jestSuccessFeedback = (
jsonResults: IJestTestResults,
showSuccessMessage: boolean
): void => {
if (!showSuccessMessage) {
// tslint:disable-next-line:no-console
console.log(':+1: Jest tests passed')
} else {
message(
`:+1: Jest tests passed: ${jsonResults.numPassedTests}/${jsonResults.numTotalTests} (${jsonResults.numPendingTests} skipped)`
)
}
}
const presentErrorsForOldStyleResults = (jsonResults: IJestTestOldResults) => {
const failing = jsonResults.testResults.filter(tr => tr.status === 'failed')
failing.forEach(results => {
const relativeFilePath = path.relative(process.cwd(), results.name)
const failedAssertions = results.assertionResults.filter(
r => r.status === 'failed'
)
const failMessage = fileToFailString(
relativeFilePath,
failedAssertions as any
)
fail(failMessage)
})
}
const presentErrorsForNewStyleResults = (jsonResults: IJestTestResults) => {
const failing = jsonResults.testResults.filter(tr => tr.numFailingTests > 0)
failing.forEach(results => {
const relativeFilePath = path.relative(process.cwd(), results.testFilePath)
const failedAssertions = results.testResults.filter(
r => r.status === 'failed'
)
const failMessage = fileToFailString(relativeFilePath, failedAssertions)
fail(failMessage)
})
}
// e.g. https://github.com/orta/danger-plugin-jest/blob/master/src/__tests__/fails.test.ts
const linkToTest = (file: string, msg: string, title: string) => {
const line = lineOfError(msg, file)
const githubRoot = danger.github.pr.head.repo.html_url.split(
danger.github.pr.head.repo.owner.login
)[0]
const repo = danger.github.pr.head.repo
const url = `${githubRoot}${repo.full_name}/blob/${
danger.github.pr.head.ref
}/${file}${line ? `#L${line}` : ''}`
return `<a href='${url}'>${title}</a>`
}
const assertionFailString = (
file: string,
status: IInsideFileTestResults
): string => `
<li>
${linkToTest(
file,
status.failureMessages && status.failureMessages[0],
status.title
)}
<br/>
${sanitizeShortErrorMessage(
status.failureMessages && stripANSI(status.failureMessages[0])
)}
<details>
<summary>Full message</summary>
<pre><code>
${status.failureMessages && stripANSI(status.failureMessages.join('\n'))}
</code></pre>
</details>
</li>
<br/>
`
const fileToFailString = (
// tslint:disable-next-line:no-shadowed-variable
path: string,
failedAssertions: IInsideFileTestResults[]
): string => `
<b>🃏 FAIL</b> in ${danger.github.utils.fileLinks([path])}
${failedAssertions.map(a => assertionFailString(path, a)).join('\n\n')}
`
const lineOfError = (msg: string, filePath: string): number | null => {
const filename = path.basename(filePath)
const restOfTrace = msg.split(filename, 2)[1]
return restOfTrace ? parseInt(restOfTrace.split(':')[1], 10) : null
}
const sanitizeShortErrorMessage = (msg: string): string => {
if (msg.includes('does not match stored snapshot')) {
return 'Snapshot has changed'
}
return msg
.split(' at', 1)[0]
.trim()
.split('\n')
.splice(2)
.join('')
.replace(/\s\s+/g, ' ')
.replace('Received:', ', received:')
.replace('., received', ', received')
.split('Difference:')[0]
}