@front10/danger-plugins 0.11.0-rc.4 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,202 +0,0 @@
1
- import * as fs from "fs";
2
- import * as path from "path";
3
-
4
- import stripANSI from "strip-ansi";
5
-
6
- import { run } from "jest";
7
- import { DangerDSLType } from "danger";
8
-
9
- declare const danger: DangerDSLType;
10
- declare function fail(message?: string): void;
11
- declare function message(message?: string): void;
12
-
13
- interface Snapshot {
14
- added: number;
15
- didUpdate: boolean;
16
- failure: boolean;
17
- filesAdded: number;
18
- filesRemoved: number;
19
- filesUnmatched: number;
20
- filesUpdated: number;
21
- matched: number;
22
- total: number;
23
- unchecked: number;
24
- uncheckedKeysByFile: any[];
25
- unmatched: number;
26
- updated: number;
27
- }
28
-
29
- interface FileTestResult {
30
- endTime: number;
31
- message: string;
32
- name: string;
33
- startTime: number;
34
- status: string;
35
- summary: string;
36
- assertionResults: AssertionResult[];
37
- }
38
-
39
- interface JestTestResults {
40
- numFailedTestSuites: number;
41
- numFailedTests: number;
42
- numPassedTestSuites: number;
43
- numPassedTests: number;
44
- numPendingTestSuites: number;
45
- numPendingTests: number;
46
- numRuntimeErrorTestSuites: number;
47
- numTotalTestSuites: number;
48
- numTotalTests: number;
49
- snapshot: Snapshot;
50
- startTime: number;
51
- success: boolean;
52
- testResults: FileTestResult[];
53
- wasInterrupted: boolean;
54
- }
55
-
56
- interface AssertionResult {
57
- ancestorTitles: string[];
58
- failureMessages: string[];
59
- fullName: string;
60
- location: string | null;
61
- status: string;
62
- title: string;
63
- }
64
-
65
- const jestSuccessFeedback = (
66
- jsonResults: JestTestResults,
67
- showSuccessMessage: boolean
68
- ): void => {
69
- if (!showSuccessMessage) {
70
- // tslint:disable-next-line:no-console
71
- console.log(":+1: Jest tests passed");
72
- } else {
73
- message(
74
- `:+1: Jest tests passed: ${jsonResults.numPassedTests}/${jsonResults.numTotalTests} (${jsonResults.numPendingTests} skipped)`
75
- );
76
- }
77
- };
78
-
79
- function getUrl(file: string, line: number | null) {
80
- const gitlab = danger.gitlab;
81
- if (gitlab) {
82
- const headSha = danger.gitlab.mr.diff_refs.head_sha;
83
- const repoSlug = gitlab.metadata.repoSlug;
84
- const baseUrl = `https://gitlab.com/${repoSlug}/-/blob`;
85
- return `${baseUrl}/${headSha}/${file}${line ? `#L${line}` : ""}`;
86
- }
87
- return "#";
88
- }
89
-
90
- const lineOfError = (msg: string, filePath: string): number | null => {
91
- const filename = path.basename(filePath);
92
- const restOfTrace = msg.split(filename, 2)[1];
93
- return restOfTrace ? parseInt(restOfTrace.split(":")[1], 10) : null;
94
- };
95
-
96
- const sanitizeShortErrorMessage = (msg: string): string => {
97
- if (msg.includes("does not match stored snapshot")) {
98
- return "Snapshot has changed";
99
- }
100
-
101
- return msg
102
- .split(" at", 1)[0]
103
- .trim()
104
- .split("\n")
105
- .splice(2)
106
- .join("")
107
- .replace(/\s\s+/g, " ")
108
- .replace("Received:", ", received:")
109
- .replace("., received", ", received")
110
- .split("Difference:")[0];
111
- };
112
-
113
- // e.g. https://gitlab.com/carburo/danger/-/blob/802af5c0b057c4439ef465a75009f017423fbf98/simple.test.js#L6
114
- const linkToTest = (file: string, msg: string, title: string) => {
115
- const line = lineOfError(msg, file);
116
-
117
- return `<a href='${getUrl(file, line)}'>${title}</a>`;
118
- };
119
-
120
- const assertionFailString = (file: string, status: AssertionResult): string => `
121
- <li>
122
- ${linkToTest(
123
- file,
124
- status.failureMessages && status.failureMessages[0],
125
- status.title
126
- )}
127
- <br/>
128
- ${sanitizeShortErrorMessage(
129
- status.failureMessages && stripANSI(status.failureMessages[0])
130
- )}
131
-
132
- <details>
133
- <summary>Full message</summary>
134
- <pre><code>
135
- ${status.failureMessages && stripANSI(status.failureMessages.join("\n"))}
136
- </code></pre>
137
- </details>
138
- </li>
139
- <br/>
140
- `;
141
-
142
- const fileToFailString = (
143
- // tslint:disable-next-line:no-shadowed-variable
144
- path: string,
145
- failedAssertions: AssertionResult[]
146
- ): string => `
147
- <b>:black_joker: FAIL</b> in ${path}
148
-
149
- ${failedAssertions.map((a) => assertionFailString(path, a)).join("\n\n")}
150
- `;
151
-
152
- const presentErrors = (jsonResults: JestTestResults) => {
153
- const failing = jsonResults.testResults.filter(
154
- (tr) => tr.status === "failed"
155
- );
156
-
157
- failing.forEach((results) => {
158
- const relativeFilePath = path.relative(process.cwd(), results.name);
159
- const failedAssertions = results.assertionResults.filter(
160
- (r) => r.status === "failed"
161
- );
162
- const failMessage = fileToFailString(relativeFilePath, failedAssertions);
163
- fail(failMessage);
164
- });
165
- };
166
-
167
- interface PluginConfig {
168
- testResultsJsonPath: string;
169
- showSuccessMessage: boolean;
170
- runJest: boolean;
171
- }
172
-
173
- export async function jest(config: Partial<PluginConfig> = {}) {
174
- const {
175
- testResultsJsonPath = "test-results.json",
176
- showSuccessMessage = true,
177
- runJest = true,
178
- } = config;
179
- if (runJest) {
180
- try {
181
- await run(["--outputFile", "test-results.json", "--json"]);
182
- } catch (e) {
183
- console.error(e);
184
- }
185
- }
186
- try {
187
- const jsonFileContents = fs.readFileSync(testResultsJsonPath, "utf8");
188
- const jsonResults: JestTestResults = JSON.parse(jsonFileContents);
189
- if (jsonResults.success) {
190
- jestSuccessFeedback(jsonResults, showSuccessMessage);
191
- return;
192
- }
193
-
194
- presentErrors(jsonResults);
195
- } catch (e) {
196
- // tslint:disable-next-line:no-console
197
- console.error(e);
198
- fail(
199
- "[danger-plugin-jest] Could not read test results. Danger cannot pass or fail the build."
200
- );
201
- }
202
- }