/
module.test.ts
320 lines (236 loc) · 7.22 KB
/
module.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import path from 'path';
import dedent from 'dedent';
import * as babel from '@babel/core';
import Module from '../babel/module';
import type { Evaluator, StrictOptions } from '../babel/types';
beforeEach(() => Module.invalidate());
const evaluator: Evaluator = (filename, options, text) => {
const { code } = babel.transformSync(text, {
filename: filename,
})!;
return [code!, null];
};
function getFileName() {
return path.resolve(__dirname, `../__fixtures__/test.js`);
}
const options: StrictOptions = {
displayName: false,
evaluate: true,
rules: [
{
action: evaluator,
},
{
test: /\/node_modules\//,
action: 'ignore',
},
],
babelOptions: {},
};
beforeEach(() => Module.invalidateEvalCache());
it('creates module for JS files', () => {
const filename = '/foo/bar/test.js';
const mod = new Module(filename, options);
mod.evaluate('module.exports = () => 42');
expect(mod.exports()).toBe(42);
expect(mod.id).toBe(filename);
expect(mod.filename).toBe(filename);
});
it('requires JS files', () => {
const mod = new Module(getFileName(), options);
mod.evaluate(dedent`
const answer = require('./sample-script');
module.exports = 'The answer is ' + answer;
`);
expect(mod.exports).toBe('The answer is 42');
});
it('requires JSON files', () => {
const mod = new Module(getFileName(), options);
mod.evaluate(dedent`
const data = require('./sample-data.json');
module.exports = 'Our saviour, ' + data.name;
`);
expect(mod.exports).toBe('Our saviour, Luke Skywalker');
});
it('imports JS files', () => {
const mod = new Module(getFileName(), options);
mod.evaluate(dedent`
import answer from './sample-script';
export const result = 'The answer is ' + answer;
`);
expect(mod.exports.result).toBe('The answer is 42');
});
it('imports TypeScript files', () => {
const mod = new Module(
path.resolve(__dirname, '../__fixtures__/test.ts'),
options
);
mod.evaluate(dedent`
import answer from './sample-typescript';
export const result = 'The answer is ' + answer;
`);
expect(mod.exports.result).toBe('The answer is 27');
});
it('imports JSON files', () => {
const mod = new Module(getFileName(), options);
mod.evaluate(dedent`
import data from './sample-data.json';
const result = 'Our saviour, ' + data.name;
export default result;
`);
expect(mod.exports.default).toBe('Our saviour, Luke Skywalker');
});
it('returns module from the cache', () => {
/* eslint-disable no-self-compare */
const filename = getFileName();
const mod = new Module(filename, options);
const id = './sample-data.json';
expect(mod.require(id) === mod.require(id)).toBe(true);
expect(
new Module(filename, options).require(id) ===
new Module(filename, options).require(id)
).toBe(true);
});
it('clears modules from the cache', () => {
const filename = getFileName();
const id = './sample-data.json';
const result = new Module(filename, options).require(id);
expect(result === new Module(filename, options).require(id)).toBe(true);
Module.invalidate();
expect(result === new Module(filename, options).require(id)).toBe(false);
});
it('exports the path for non JS/JSON files', () => {
const mod = new Module(getFileName(), options);
expect(mod.require('./sample-asset.png')).toBe('./sample-asset.png');
});
it('returns module when requiring mocked builtin node modules', () => {
const mod = new Module(getFileName(), options);
expect(mod.require('path')).toBe(require('path'));
});
it('returns null when requiring empty builtin node modules', () => {
const mod = new Module(getFileName(), options);
expect(mod.require('fs')).toBe(null);
});
it('throws when requiring unmocked builtin node modules', () => {
const mod = new Module(getFileName(), options);
expect(() => mod.require('perf_hooks')).toThrow(
'Unable to import "perf_hooks". Importing Node builtins is not supported in the sandbox.'
);
});
it('has access to the global object', () => {
const mod = new Module(getFileName(), options);
expect(() =>
mod.evaluate(dedent`
new global.Set();
`)
).not.toThrow();
});
it("doesn't have access to the process object", () => {
const mod = new Module(getFileName(), options);
expect(() =>
mod.evaluate(dedent`
module.exports = process.abort();
`)
).toThrow('process.abort is not a function');
});
it('has access to NODE_ENV', () => {
const mod = new Module(getFileName(), options);
mod.evaluate(dedent`
module.exports = process.env.NODE_ENV;
`);
expect(mod.exports).toBe(process.env.NODE_ENV);
});
it('has require.resolve available', () => {
const mod = new Module(getFileName(), options);
mod.evaluate(dedent`
module.exports = require.resolve('./sample-script');
`);
expect(mod.exports).toBe(
path.resolve(path.dirname(mod.filename), 'sample-script.js')
);
});
it('has require.ensure available', () => {
const mod = new Module(getFileName(), options);
expect(() =>
mod.evaluate(dedent`
require.ensure(['./sample-script']);
`)
).not.toThrow();
});
it('has __filename available', () => {
const mod = new Module(getFileName(), options);
mod.evaluate(dedent`
module.exports = __filename;
`);
expect(mod.exports).toBe(mod.filename);
});
it('has __dirname available', () => {
const mod = new Module(getFileName(), options);
mod.evaluate(dedent`
module.exports = __dirname;
`);
expect(mod.exports).toBe(path.dirname(mod.filename));
});
it('has setTimeout, clearTimeout available', () => {
const mod = new Module(getFileName(), options);
expect(() =>
mod.evaluate(dedent`
const x = setTimeout(() => {
console.log('test');
},0);
clearTimeout(x);
`)
).not.toThrow();
});
it('has setInterval, clearInterval available', () => {
const mod = new Module(getFileName(), options);
expect(() =>
mod.evaluate(dedent`
const x = setInterval(() => {
console.log('test');
}, 1000);
clearInterval(x);
`)
).not.toThrow();
});
it('has setImmediate, clearImmediate available', () => {
const mod = new Module(getFileName(), options);
expect(() =>
mod.evaluate(dedent`
const x = setImmediate(() => {
console.log('test');
});
clearImmediate(x);
`)
).not.toThrow();
});
it('has global objects available without referencing global', () => {
const mod = new Module(getFileName(), options);
expect(() =>
mod.evaluate(dedent`
const x = new Set();
`)
).not.toThrow();
});
it('changes resolve behaviour on overriding _resolveFilename', () => {
const originalResolveFilename = Module._resolveFilename;
Module._resolveFilename = (id) => (id === 'foo' ? 'bar' : id);
const mod = new Module(getFileName(), options);
mod.evaluate(dedent`
module.exports = [
require.resolve('foo'),
require.resolve('test'),
];
`);
// Restore old behavior
Module._resolveFilename = originalResolveFilename;
expect(mod.exports).toEqual(['bar', 'test']);
});
it('correctly processes export declarations in strict mode', () => {
const filename = '/foo/bar/test.js';
const mod = new Module(filename, options);
mod.evaluate('"use strict"; exports = module.exports = () => 42');
expect(mod.exports()).toBe(42);
expect(mod.id).toBe(filename);
expect(mod.filename).toBe(filename);
});