forked from jestjs/jest
-
Notifications
You must be signed in to change notification settings - Fork 3
/
isError.test.ts
52 lines (45 loc) 路 1.48 KB
/
isError.test.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
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @jest-environment jsdom
*/
/* eslint-env browser */
import {isError} from '../utils';
// Copied from https://github.com/graingert/angular.js/blob/a43574052e9775cbc1d7dd8a086752c979b0f020/test/AngularSpec.js#L1883
describe('isError', () => {
function testErrorFromDifferentContext(
createError: (win: Window | null) => Error,
) {
const iframe = document.createElement('iframe');
document.body.appendChild(iframe);
try {
const error = createError(iframe.contentWindow);
expect(isError(error)).toBe(true);
} finally {
iframe.parentElement!.removeChild(iframe);
}
}
it('should not assume objects are errors', () => {
const fakeError = {message: 'A fake error', stack: 'no stack here'};
expect(isError(fakeError)).toBe(false);
});
it('should detect simple error instances', () => {
expect(isError(new Error())).toBe(true);
});
it('should detect errors from another context', () => {
testErrorFromDifferentContext((win: Window) => new win.Error());
});
it('should detect DOMException errors from another context', () => {
testErrorFromDifferentContext((win: Window) => {
try {
win.document.querySelectorAll('');
} catch (e: unknown) {
return e;
}
return null;
});
});
});