-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjest.setup.js
More file actions
79 lines (76 loc) · 2.51 KB
/
jest.setup.js
File metadata and controls
79 lines (76 loc) · 2.51 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
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
// Jest setup file to provide minimal Web APIs required by undici/testcontainers
// Must run before test files import modules that depend on undici.
import { Readable } from 'node:stream';
import { TextEncoder, TextDecoder } from 'node:util';
if (typeof globalThis.File === 'undefined') {
globalThis.File = class File {
constructor(bits, filename, options = {}) {
this.bits = bits;
this.name = filename;
this.type = options.type || '';
this.lastModified = options.lastModified || Date.now();
}
get size() {
return this.bits.reduce((acc, bit) => {
if (typeof bit === 'string')
return acc + new TextEncoder().encode(bit).length;
if (bit instanceof ArrayBuffer) return acc + bit.byteLength;
if (ArrayBuffer.isView(bit)) return acc + bit.byteLength;
return acc + (bit && bit.length ? bit.length : 0);
}, 0);
}
async text() {
const parts = [];
for (const bit of this.bits) {
if (typeof bit === 'string') parts.push(bit);
else if (bit instanceof ArrayBuffer)
parts.push(new TextDecoder().decode(bit));
else parts.push(String(bit));
}
return parts.join('');
}
slice() {
return new File(this.bits, this.name, { type: this.type });
}
stream() {
return Readable.from(this.bits);
}
};
}
if (typeof globalThis.Blob === 'undefined') {
globalThis.Blob = class Blob {
constructor(bits, options = {}) {
this.bits = bits;
this.type = options.type || '';
}
get size() {
return this.bits.reduce((acc, bit) => {
if (typeof bit === 'string')
return acc + new TextEncoder().encode(bit).length;
if (bit instanceof ArrayBuffer) return acc + bit.byteLength;
if (ArrayBuffer.isView(bit)) return acc + bit.byteLength;
return acc + (bit && bit.length ? bit.length : 0);
}, 0);
}
async text() {
const parts = [];
for (const bit of this.bits) {
if (typeof bit === 'string') parts.push(bit);
else if (bit instanceof ArrayBuffer)
parts.push(new TextDecoder().decode(bit));
else parts.push(String(bit));
}
return parts.join('');
}
slice() {
return new Blob(this.bits, { type: this.type });
}
stream() {
return Readable.from(this.bits);
}
};
}
if (typeof globalThis.TextEncoder === 'undefined')
globalThis.TextEncoder = TextEncoder;
if (typeof globalThis.TextDecoder === 'undefined')
globalThis.TextDecoder = TextDecoder;