1 | 'use strict';
|
2 |
|
3 | function _vm() {
|
4 | const data = require('vm');
|
5 |
|
6 | _vm = function () {
|
7 | return data;
|
8 | };
|
9 |
|
10 | return data;
|
11 | }
|
12 |
|
13 | function _jestMock() {
|
14 | const data = require('jest-mock');
|
15 |
|
16 | _jestMock = function () {
|
17 | return data;
|
18 | };
|
19 |
|
20 | return data;
|
21 | }
|
22 |
|
23 | function _jestUtil() {
|
24 | const data = require('jest-util');
|
25 |
|
26 | _jestUtil = function () {
|
27 | return data;
|
28 | };
|
29 |
|
30 | return data;
|
31 | }
|
32 |
|
33 | function _fakeTimers() {
|
34 | const data = require('@jest/fake-timers');
|
35 |
|
36 | _fakeTimers = function () {
|
37 | return data;
|
38 | };
|
39 |
|
40 | return data;
|
41 | }
|
42 |
|
43 | function _defineProperty(obj, key, value) {
|
44 | if (key in obj) {
|
45 | Object.defineProperty(obj, key, {
|
46 | value: value,
|
47 | enumerable: true,
|
48 | configurable: true,
|
49 | writable: true
|
50 | });
|
51 | } else {
|
52 | obj[key] = value;
|
53 | }
|
54 | return obj;
|
55 | }
|
56 |
|
57 | class NodeEnvironment {
|
58 | constructor(config) {
|
59 | _defineProperty(this, 'context', void 0);
|
60 |
|
61 | _defineProperty(this, 'fakeTimers', void 0);
|
62 |
|
63 | _defineProperty(this, 'fakeTimersModern', void 0);
|
64 |
|
65 | _defineProperty(this, 'global', void 0);
|
66 |
|
67 | _defineProperty(this, 'moduleMocker', void 0);
|
68 |
|
69 | this.context = (0, _vm().createContext)();
|
70 | const global = (this.global = (0, _vm().runInContext)(
|
71 | 'this',
|
72 | Object.assign(this.context, config.testEnvironmentOptions)
|
73 | ));
|
74 | global.global = global;
|
75 | global.clearInterval = clearInterval;
|
76 | global.clearTimeout = clearTimeout;
|
77 | global.setInterval = setInterval;
|
78 | global.setTimeout = setTimeout;
|
79 | global.ArrayBuffer = ArrayBuffer;
|
80 |
|
81 |
|
82 |
|
83 | global.Uint8Array = Uint8Array;
|
84 |
|
85 | if (typeof URL !== 'undefined' && typeof URLSearchParams !== 'undefined') {
|
86 | global.URL = URL;
|
87 | global.URLSearchParams = URLSearchParams;
|
88 | }
|
89 |
|
90 | if (
|
91 | typeof TextEncoder !== 'undefined' &&
|
92 | typeof TextDecoder !== 'undefined'
|
93 | ) {
|
94 | global.TextEncoder = TextEncoder;
|
95 | global.TextDecoder = TextDecoder;
|
96 | }
|
97 |
|
98 | if (typeof queueMicrotask !== 'undefined') {
|
99 | global.queueMicrotask = queueMicrotask;
|
100 | }
|
101 |
|
102 | (0, _jestUtil().installCommonGlobals)(global, config.globals);
|
103 | this.moduleMocker = new (_jestMock().ModuleMocker)(global);
|
104 |
|
105 | const timerIdToRef = id => ({
|
106 | id,
|
107 |
|
108 | ref() {
|
109 | return this;
|
110 | },
|
111 |
|
112 | unref() {
|
113 | return this;
|
114 | }
|
115 | });
|
116 |
|
117 | const timerRefToId = timer => (timer && timer.id) || undefined;
|
118 |
|
119 | const timerConfig = {
|
120 | idToRef: timerIdToRef,
|
121 | refToId: timerRefToId
|
122 | };
|
123 | this.fakeTimers = new (_fakeTimers().LegacyFakeTimers)({
|
124 | config,
|
125 | global,
|
126 | moduleMocker: this.moduleMocker,
|
127 | timerConfig
|
128 | });
|
129 | this.fakeTimersModern = new (_fakeTimers().ModernFakeTimers)({
|
130 | config,
|
131 | global
|
132 | });
|
133 | }
|
134 |
|
135 | async setup() {}
|
136 |
|
137 | async teardown() {
|
138 | if (this.fakeTimers) {
|
139 | this.fakeTimers.dispose();
|
140 | }
|
141 |
|
142 | if (this.fakeTimersModern) {
|
143 | this.fakeTimersModern.dispose();
|
144 | }
|
145 |
|
146 | this.context = null;
|
147 | this.fakeTimers = null;
|
148 | this.fakeTimersModern = null;
|
149 | }
|
150 |
|
151 |
|
152 | runScript(script) {
|
153 | if (this.context) {
|
154 | return script.runInContext(this.context);
|
155 | }
|
156 |
|
157 | return null;
|
158 | }
|
159 |
|
160 | getVmContext() {
|
161 | return this.context;
|
162 | }
|
163 | }
|
164 |
|
165 | module.exports = NodeEnvironment;
|