forked from stoplightio/spectral
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatchers.ts
47 lines (41 loc) · 1.04 KB
/
matchers.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
import AggregateError = require('es-aggregate-error');
declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace jest {
interface Matchers<R> {
toThrowAggregateError(error: AggregateError): R;
}
}
}
function toPlainObject(ex: unknown): Record<string, unknown> {
if (ex instanceof Error) {
return {
...ex,
message: ex.message,
name: ex.name,
};
}
return Object(ex);
}
expect.extend({
toThrowAggregateError(received, expected) {
let error: unknown;
if (typeof received === 'function') {
expect(received).toThrow(AggregateError);
try {
received();
} catch (e) {
error = e;
}
} else {
error = received;
}
expect(error).toEqual(expected);
expect((error as AggregateError).errors).toEqual(expected.errors);
expect((error as AggregateError).errors.map(toPlainObject)).toEqual(expected.errors.map(toPlainObject));
return {
message: (): string => 'All errors matched!',
pass: true,
};
},
});