-
Notifications
You must be signed in to change notification settings - Fork 116
/
utils.ts
137 lines (108 loc) · 4.15 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
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
import type { FixtureResult, Label, StatusDetails, StepResult, TestResult } from "../model.js";
import { LabelName, Status } from "../model.js";
import type { RuntimeMessage } from "./types.js";
export const getStatusFromError = (error: Error): Status => {
switch (true) {
/**
* Native `node:assert` and `chai` (`vitest` uses it under the hood) throw `AssertionError`
* `jest` throws `JestAssertionError` instance
* `jasmine` throws `ExpectationFailed` instance
*/
case /assert/gi.test(error.constructor.name):
case /expectation/gi.test(error.constructor.name):
case /assert/gi.test(error.name):
case /assert/gi.test(error.message):
return Status.FAILED;
default:
return Status.BROKEN;
}
};
/**
* Source: https://github.com/chalk/ansi-regex
*/
const ansiRegex = ({ onlyFirst = false } = {}) => {
const pattern = [
"[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)",
"(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]))",
].join("|");
return new RegExp(pattern, onlyFirst ? undefined : "g");
};
/**
* https://github.com/chalk/strip-ansi
*/
export const stripAnsi = (str: string): string => {
const regex = ansiRegex();
return str.replace(regex, "");
};
export const getMessageAndTraceFromError = (
error: Error | { message?: string; stack?: string },
): Pick<StatusDetails, "message" | "trace"> => {
const { message, stack } = error;
return {
message: message ? stripAnsi(message) : undefined,
trace: stack ? stripAnsi(stack) : undefined,
};
};
export const allureIdRegexp = /@?allure.id[:=](?<id>[^\s]+)/;
export const allureIdRegexpGlobal = new RegExp(allureIdRegexp, "g");
export const allureLabelRegexp = /@?allure.label.(?<name>[^\s]+?)[:=](?<value>[^\s]+)/;
export const allureLabelRegexpGlobal = new RegExp(allureLabelRegexp, "g");
export const extractMetadataFromString = (
title: string,
): {
labels: Label[];
cleanTitle: string;
} => {
const labels = [] as Label[];
title.split(" ").forEach((val) => {
const idValue = val.match(allureIdRegexp)?.groups?.id;
if (idValue) {
labels.push({ name: LabelName.ALLURE_ID, value: idValue });
}
const labelMatch = val.match(allureLabelRegexp);
const { name, value } = labelMatch?.groups || {};
if (name && value) {
labels?.push({ name, value });
}
});
const cleanTitle = title.replace(allureLabelRegexpGlobal, "").replace(allureIdRegexpGlobal, "").trim();
return { labels, cleanTitle };
};
export const isAnyStepFailed = (item: StepResult | TestResult | FixtureResult): boolean => {
const isFailed = item.status === Status.FAILED;
if (isFailed || item.steps.length === 0) {
return isFailed;
}
return !!item.steps.find((step) => isAnyStepFailed(step));
};
export const isAllStepsEnded = (item: StepResult | TestResult | FixtureResult): boolean => {
return item.steps.every((val) => val.stop && isAllStepsEnded(val));
};
export const hasLabel = (testResult: TestResult, labelName: LabelName | string): boolean => {
return !!testResult.labels.find((l) => l.name === labelName);
};
export const hasStepMessage = (messages: RuntimeMessage[]) => {
return messages.some((message) => message.type === "step_start" || message.type === "step_stop");
};
export const getStepsMessagesPair = (messages: RuntimeMessage[]) =>
messages.reduce((acc, message) => {
if (message.type !== "step_start" && message.type !== "step_stop") {
return acc;
}
if (message.type === "step_start") {
acc.push([message]);
return acc;
}
const unfinishedStepIdx = acc.findLastIndex((step) => step.length === 1);
if (unfinishedStepIdx === -1) {
return acc;
}
acc[unfinishedStepIdx].push(message);
return acc;
}, [] as RuntimeMessage[][]);
export const getUnfinishedStepsMessages = (messages: RuntimeMessage[]) => {
const grouppedStepsMessage = getStepsMessagesPair(messages);
return grouppedStepsMessage.filter((step) => step.length === 1);
};
export const isPromise = (obj: any): boolean =>
!!obj && (typeof obj === "object" || typeof obj === "function") && typeof obj.then === "function";