-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTestError.ts
More file actions
53 lines (44 loc) · 1.38 KB
/
TestError.ts
File metadata and controls
53 lines (44 loc) · 1.38 KB
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
export interface JsError extends Error {
toString?: () => string;
}
export interface AssertionError extends JsError {
expected: any;
actual: any;
operator: string;
showDiff?: boolean;
}
export interface HtmlDiffAssertionError extends JsError {
diff: {
expected: string;
actual: string;
comparison: string;
};
label: string;
}
export interface PprintAssertionError extends JsError {
diff: {
expected: string;
actual: string;
};
toString: () => string;
}
export type TestError = JsError | AssertionError | PprintAssertionError | HtmlDiffAssertionError;
export const pprintAssertionError = (message: string, expected: string, actual: string): PprintAssertionError => {
const e: Partial<PprintAssertionError> = new Error(message);
e.name = 'PprintAssertionError';
e.diff = {
actual,
expected
};
e.toString = (): string => {
// Break circular dependency by inlining basic formatting
return `${message}\nExpected: ${expected}\nActual: ${actual}`;
};
return e as PprintAssertionError;
};
export const isPprintAssertionError = (err: TestError): err is PprintAssertionError =>
err.name === 'PprintAssertionError';
export const isHTMLDiffError = (err: TestError): err is HtmlDiffAssertionError =>
err.name === 'HtmlAssertion';
export const isAssertionError = (err: TestError): err is AssertionError =>
err.name === 'AssertionError';