-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
SingleContextNodeEnvironment.test.ts
103 lines (90 loc) · 4.55 KB
/
SingleContextNodeEnvironment.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
/*
* Copyright (C) 2020 Klaus Reimer <[email protected]>
* See LICENSE.md for licensing information.
*/
import fs from "fs";
import util from "util";
import v8 from "v8";
const readFile = util.promisify(fs.readFile);
function getSuperClass(cls: Function): Function {
const prototype = Object.getPrototypeOf(cls.prototype);
return prototype ? prototype.constructor : null;
}
describe("instanceof", () => {
const buffers = fs.readdirSync(__dirname);
const buffer = fs.readFileSync(__filename);
const error = (() => {
try {
fs.readFileSync("/");
return null;
} catch (e) {
return e;
}
})() as Error;
const promise = readFile(__filename);
const nodeArrayType = buffers.constructor;
const nodeErrorType = error.constructor;
const nodePromiseType = promise.constructor;
const nodeUint8ArrayType = getSuperClass(buffer.constructor);
const nodeTypedArrayType = getSuperClass(nodeUint8ArrayType);
const nodeObjectType = getSuperClass(nodeTypedArrayType);
const globalTypedArrayType = getSuperClass(Uint8Array);
it("works with node array type", () => {
expect(buffers instanceof Array).toBe(true);
expect(buffers instanceof Object).toBe(true);
expect([] instanceof nodeArrayType).toBe(true);
expect([] instanceof nodeObjectType).toBe(true);
});
it("works with node error type", () => {
expect(error instanceof Error).toBe(true);
expect(error instanceof Object).toBe(true);
expect(new Error() instanceof nodeErrorType).toBe(true);
expect(new Error() instanceof nodeObjectType).toBe(true);
});
it("works with node promise type", () => {
expect(promise instanceof Promise).toBe(true);
expect(promise instanceof Object).toBe(true);
expect(new Promise<void>(resolve => resolve()) instanceof nodePromiseType).toBe(true);
expect(new Promise<void>(resolve => resolve()) instanceof nodeObjectType).toBe(true);
});
it("works with node Uint8Array type", () => {
expect(buffer instanceof Buffer).toBe(true);
expect(buffer instanceof Uint8Array).toBe(true);
expect(buffer instanceof globalTypedArrayType).toBe(true);
expect(buffer instanceof Object).toBe(true);
expect(new Uint8Array([]) instanceof nodeUint8ArrayType).toBe(true);
expect(new Uint8Array([]) instanceof nodeTypedArrayType).toBe(true);
expect(new Uint8Array([]) instanceof nodeObjectType).toBe(true);
});
it("recognizes typed arrays as objects", () => {
expect(new Uint8Array([ 1, 2, 3 ]) instanceof Object).toBe(true);
expect(new Uint8ClampedArray([ 1, 2, 3 ]) instanceof Object).toBe(true);
expect(new Uint16Array([ 1, 2, 3 ]) instanceof Object).toBe(true);
expect(new Uint32Array([ 1, 2, 3 ]) instanceof Object).toBe(true);
expect(new BigUint64Array([]) instanceof Object).toBe(true);
expect(new Int8Array([ 1, 2, 3 ]) instanceof Object).toBe(true);
expect(new Int16Array([ 1, 2, 3 ]) instanceof Object).toBe(true);
expect(new Int32Array([ 1, 2, 3 ]) instanceof Object).toBe(true);
expect(new BigInt64Array([]) instanceof Object).toBe(true);
expect(new Float32Array([ 1, 2, 3 ]) instanceof Object).toBe(true);
expect(new Float64Array([ 1, 2, 3 ]) instanceof Object).toBe(true);
});
it("recognizes typed arrays as instances of TypedArray", () => {
expect(new Uint8Array([ 1, 2, 3 ]) instanceof globalTypedArrayType).toBe(true);
expect(new Uint8ClampedArray([ 1, 2, 3 ]) instanceof globalTypedArrayType).toBe(true);
expect(new Uint16Array([ 1, 2, 3 ]) instanceof globalTypedArrayType).toBe(true);
expect(new Uint32Array([ 1, 2, 3 ]) instanceof globalTypedArrayType).toBe(true);
expect(new BigUint64Array([]) instanceof globalTypedArrayType).toBe(true);
expect(new Int8Array([ 1, 2, 3 ]) instanceof globalTypedArrayType).toBe(true);
expect(new Int16Array([ 1, 2, 3 ]) instanceof globalTypedArrayType).toBe(true);
expect(new Int32Array([ 1, 2, 3 ]) instanceof globalTypedArrayType).toBe(true);
expect(new BigInt64Array([]) instanceof globalTypedArrayType).toBe(true);
expect(new Float32Array([ 1, 2, 3 ]) instanceof globalTypedArrayType).toBe(true);
expect(new Float64Array([ 1, 2, 3 ]) instanceof globalTypedArrayType).toBe(true);
});
it("works with v8 serialize/deserialize", () => {
const m1 = new Map();
const m2 = v8.deserialize(v8.serialize(m1));
expect(m1).toEqual(m2);
});
});