-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathis.test.ts
154 lines (138 loc) · 4.87 KB
/
is.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { describe, expect, it, test } from 'vitest';
import {
isDOMError,
isDOMException,
isError,
isErrorEvent,
isInstanceOf,
isPlainObject,
isPrimitive,
isThenable,
isVueViewModel,
} from '../../src/utils-hoist/is';
import { supportsDOMError, supportsDOMException, supportsErrorEvent } from '../../src/utils-hoist/supports';
import { resolvedSyncPromise } from '../../src/utils-hoist/syncpromise';
import { testOnlyIfNodeVersionAtLeast } from './testutils';
if (supportsDOMError()) {
describe('isDOMError()', () => {
test('should work as advertised', () => {
expect(isDOMError(new Error())).toEqual(false);
// @ts-expect-error See: src/supports.ts for details
expect(isDOMError(new DOMError(''))).toEqual(true);
});
});
}
if (supportsDOMException()) {
describe('isDOMException()', () => {
test('should work as advertised', () => {
expect(isDOMException(new Error())).toEqual(false);
expect(isDOMException(new DOMException(''))).toEqual(true);
});
});
}
describe('isError()', () => {
test('should work as advertised', () => {
expect(isError(new Error())).toEqual(true);
expect(isError(new ReferenceError())).toEqual(true);
expect(isError({})).toEqual(false);
expect(
isError({
message: 'A fake error',
stack: 'no stack here',
}),
).toEqual(false);
expect(isError('')).toEqual(false);
expect(isError(true)).toEqual(false);
});
testOnlyIfNodeVersionAtLeast(18)('should detect WebAssembly.Exceptions', () => {
// https://developer.mozilla.org/en-US/docs/WebAssembly/JavaScript_interface/Exception/Exception#examples
// @ts-expect-error - WebAssembly.Tag is a valid constructor
const tag = new WebAssembly.Tag({ parameters: ['i32', 'f32'] });
// @ts-expect-error - WebAssembly.Exception is a valid constructor
expect(isError(new WebAssembly.Exception(tag, [42, 42.3]))).toBe(true);
});
});
if (supportsErrorEvent()) {
describe('isErrorEvent()', () => {
test('should work as advertised', () => {
expect(isErrorEvent(new Error())).toEqual(false);
expect(isErrorEvent(new ErrorEvent(''))).toEqual(true);
});
});
}
describe('isPrimitive()', () => {
test('should work as advertised', () => {
expect(isPrimitive(undefined)).toEqual(true);
expect(isPrimitive(null)).toEqual(true);
expect(isPrimitive(true)).toEqual(true);
expect(isPrimitive('foo')).toEqual(true);
expect(isPrimitive(42)).toEqual(true);
expect(isPrimitive({})).toEqual(false);
expect(isPrimitive([])).toEqual(false);
expect(isPrimitive(new Error('foo'))).toEqual(false);
expect(isPrimitive(new Date())).toEqual(false);
});
});
describe('isThenable()', () => {
test('should work as advertised', () => {
expect(isThenable(Promise.resolve(true))).toEqual(true);
expect(isThenable(resolvedSyncPromise(true))).toEqual(true);
expect(isThenable(undefined)).toEqual(false);
expect(isThenable(null)).toEqual(false);
expect(isThenable(true)).toEqual(false);
expect(isThenable('foo')).toEqual(false);
expect(isThenable(42)).toEqual(false);
expect(isThenable({})).toEqual(false);
expect(isThenable([])).toEqual(false);
expect(isThenable(new Error('foo'))).toEqual(false);
expect(isThenable(new Date())).toEqual(false);
});
});
describe('isInstanceOf()', () => {
test('should work as advertised', () => {
function Foo(): void {
/* no-empty */
}
expect(isInstanceOf(new Error('wat'), Error)).toEqual(true);
expect(isInstanceOf(new Date(), Date)).toEqual(true);
// @ts-expect-error Foo implicity has any type, doesn't have constructor
expect(isInstanceOf(new Foo(), Foo)).toEqual(true);
expect(isInstanceOf(new Error('wat'), Foo)).toEqual(false);
expect(isInstanceOf(new Date('wat'), Error)).toEqual(false);
});
test('should not break with incorrect input', () => {
expect(isInstanceOf(new Error('wat'), 1)).toEqual(false);
expect(isInstanceOf(new Error('wat'), 'wat')).toEqual(false);
expect(isInstanceOf(new Error('wat'), null)).toEqual(false);
expect(isInstanceOf(new Error('wat'), undefined)).toEqual(false);
});
});
describe('isVueViewModel()', () => {
test('should work as advertised', () => {
expect(isVueViewModel({ _isVue: true })).toEqual(true);
expect(isVueViewModel({ __isVue: true })).toEqual(true);
expect(isVueViewModel({ foo: true })).toEqual(false);
});
});
describe('isPlainObject', () => {
class MyClass {
public foo: string = 'bar';
}
it.each([
[{}, true],
[true, false],
[false, false],
[undefined, false],
[null, false],
['', false],
[1, false],
[0, false],
[{ aha: 'yes' }, true],
[new Object({ aha: 'yes' }), true],
[new String('aa'), false],
[new MyClass(), true],
[{ ...new MyClass() }, true],
])('%s is %s', (value, expected) => {
expect(isPlainObject(value)).toBe(expected);
});
});