-
Notifications
You must be signed in to change notification settings - Fork 3
/
test-setup.js
47 lines (39 loc) · 1.25 KB
/
test-setup.js
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
const { addHook } = require('pirates');
const { transform } = require('sucrase');
const { JSDOM } = require('jsdom');
const dom = new JSDOM(
'<!doctype html><html><head><meta charset="utf-8">' +
'</head><body></body></html>',
{ pretendToBeVisual: true }
);
global.window = dom.window;
const apis = new Set(Object.keys(dom.window));
/**
* These does not work by default and we don't need them ATM
*/
apis.delete('localStorage');
apis.delete('sessionStorage');
apis.forEach(function (key) {
if (!global[key]) {
global[key] = dom.window[key];
}
});
addHook((code) => `import React from 'react';${code}`, {
exts: ['.tsx'],
ignoreNodeModules: false,
});
addHook(
(code, filePath) => {
const { code: transformedCode, sourceMap } = transform(code, {
sourceMapOptions: { compiledFilename: filePath },
transforms: ['imports', 'typescript', 'jsx'],
filePath,
});
const mapBase64 = Buffer.from(JSON.stringify(sourceMap)).toString(
'base64'
);
const suffix = `//# sourceMappingURL=data:application/json;charset=utf-8;base64,${mapBase64}`;
return `${transformedCode}\n${suffix}`;
},
{ exts: ['.ts', '.tsx'], ignoreNodeModules: false }
);