How to use transformRequires method in Cypress

Best JavaScript code snippet using cypress

Run Cypress automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

transformRequires.spec.js

Source: transformRequires.spec.js Github

copy
1const assert = require('assert');
2
3const transformRequires = require('./transformRequires');
4
5const {
6  generateFunction,
7  generateMangledFunction,
8  generateRequire,
9  generateMangledRequire,
10  generateVariableAssignment,
11} = require('./testHelpers');
12
13// Tests just using filenames that relate to module ids (ie, ./2.js)
14
15it('1: require(2) => 1: require("./2")', () => {
16  const modules = [
17    {
18      id: 1,
19      code: generateFunction(
20        generateRequire(2)
21      ),
22    },
23    {
24      id: 2,
25      code: generateFunction(),
26    },
27  ];
28  const expectedOutput = [
29    {
30      id: 1,
31      code: generateFunction(
32        generateRequire('./2')
33      ),
34    },
35    {
36      id: 2,
37      code: generateFunction(),
38    },
39  ];
40
41
42  const knownPaths = {
43  };
44  const entryPointModuleId = 1;
45  const type = "webpack";
46  const output = transformRequires(modules, knownPaths, entryPointModuleId, type);
47
48  assert.deepEqual(output, expectedOutput);
49});
50
51// Tests involving relative paths
52
53it('1: require(2) => 1: require("./foo/bar/baz") where (2 = ./foo/bar/baz)', () => {
54  const modules = [
55    {
56      id: 1,
57      code: generateFunction(
58        generateRequire(2)
59      ),
60    },
61    {
62      id: 2,
63      code: generateFunction(),
64    },
65  ];
66  const expectedOutput = [
67    {
68      id: 1,
69      code: generateFunction(
70        generateRequire('./foo/bar/baz')
71      ),
72    },
73    {
74      id: 2,
75      code: generateFunction(),
76    },
77  ];
78
79
80  const knownPaths = {
81    2: './foo/bar/baz',
82  };
83  const entryPointModuleId = 1;
84  const type = "webpack";
85  const output = transformRequires(modules, knownPaths, entryPointModuleId, type);
86
87  assert.deepEqual(output, expectedOutput);
88});
89
90it('1: require(2) => 1: require("./bar/baz") where (1 = ./foo/index, 2 = ./foo/bar/baz)', () => {
91  const modules = [
92    {
93      id: 1,
94      code: generateFunction(
95        generateRequire(2)
96      ),
97    },
98    {
99      id: 2,
100      code: generateFunction(),
101    },
102  ];
103  const expectedOutput = [
104    {
105      id: 1,
106      code: generateFunction(
107        generateRequire('./bar/baz')
108      ),
109    },
110    {
111      id: 2,
112      code: generateFunction(),
113    },
114  ];
115
116
117  const knownPaths = {
118    1: './foo/index',
119    2: './foo/bar/baz',
120  };
121  const type = "webpack";
122  const entryPointModuleId = 1;
123  const output = transformRequires(modules, knownPaths, entryPointModuleId, type);
124
125  assert.deepEqual(output, expectedOutput);
126});
127
128it('1: require(2) => 1: require("../hello") where (1 = ./foo/bar/baz/index, 2 = ./foo/hello)', () => {
129  const modules = [
130    {
131      id: 1,
132      code: generateFunction(
133        generateRequire(2)
134      ),
135    },
136    {
137      id: 2,
138      code: generateFunction(),
139    },
140  ];
141  const expectedOutput = [
142    {
143      id: 1,
144      code: generateFunction(
145        generateRequire('../../hello')
146      ),
147    },
148    {
149      id: 2,
150      code: generateFunction(),
151    },
152  ];
153
154
155  const knownPaths = {
156    1: './foo/bar/baz/index',
157    2: './foo/hello',
158  };
159  const type = "webpack";
160  const entryPointModuleId = 1;
161  const output = transformRequires(modules, knownPaths, entryPointModuleId, type);
162
163  assert.deepEqual(output, expectedOutput);
164});
165
166// Tests involving `node_modules`
167
168it('1: require(2) => 1: require("foo") where (1 = ./foo/bar/baz/index, 2 = foo)', () => {
169  const modules = [
170    {
171      id: 1,
172      code: generateFunction(
173        generateRequire(2)
174      ),
175    },
176    {
177      id: 2,
178      code: generateFunction(),
179    },
180  ];
181  const expectedOutput = [
182    {
183      id: 1,
184      code: generateFunction(
185        generateRequire('foo')
186      ),
187    },
188    {
189      id: 2,
190      code: generateFunction(),
191    },
192  ];
193
194
195  const knownPaths = {
196    1: './foo/bar/baz/index',
197    2: 'foo',
198  };
199  const type = "webpack";
200  const entryPointModuleId = 1;
201  const output = transformRequires(modules, knownPaths, entryPointModuleId, type);
202
203  assert.deepEqual(output, expectedOutput);
204});
205
206it('1: require(2) => 1: require("foo/bar") where (1 = ./foo/bar/baz/index, 2 = foo/bar)', () => {
207  const modules = [
208    {
209      id: 1,
210      code: generateFunction(
211        generateRequire(2)
212      ),
213    },
214    {
215      id: 2,
216      code: generateFunction(),
217    },
218  ];
219  const expectedOutput = [
220    {
221      id: 1,
222      code: generateFunction(
223        generateRequire('foo/bar')
224      ),
225    },
226    {
227      id: 2,
228      code: generateFunction(),
229    },
230  ];
231
232
233  const knownPaths = {
234    1: './foo/bar/baz/index',
235    2: 'foo/bar',
236  };
237  const type = "webpack";
238  const entryPointModuleId = 1;
239  const output = transformRequires(modules, knownPaths, entryPointModuleId, type);
240
241  assert.deepEqual(output, expectedOutput);
242});
243
244// Tests involving looking up module paths to programatically determine where modules are located.
245
246it('1: require(2) => 1: require("./foo")', () => {
247  const modules = [
248    {
249      id: 1,
250      code: generateFunction(
251        generateRequire(2)
252      ),
253      lookup: {'./foo': 2},
254    },
255    {
256      id: 2,
257      code: generateFunction(),
258      lookup: {},
259    },
260  ];
261  const expectedOutput = [
262    {
263      id: 1,
264      code: generateFunction(
265        generateRequire('./foo')
266      ),
267      lookup: {'./foo': 2},
268    },
269    {
270      id: 2,
271      code: generateFunction(),
272      lookup: {},
273    },
274  ];
275
276
277  const knownPaths = {
278  };
279  const type = "webpack";
280  const entryPointModuleId = 1;
281  const output = transformRequires(modules, knownPaths, entryPointModuleId, type);
282
283  assert.deepEqual(output, expectedOutput);
284});
285
286it('1: require(2) => 1: require("./bar/baz")', () => {
287  const modules = [
288    {
289      id: 1,
290      code: generateFunction(
291        generateRequire(2)
292      ),
293      lookup: {'./bar/baz': 2},
294    },
295    {
296      id: 2,
297      code: generateFunction(),
298      lookup: {},
299    },
300  ];
301  const expectedOutput = [
302    {
303      id: 1,
304      code: generateFunction(
305        generateRequire('./bar/baz')
306      ),
307      lookup: {'./bar/baz': 2},
308    },
309    {
310      id: 2,
311      code: generateFunction(),
312      lookup: {},
313    },
314  ];
315
316
317  const knownPaths = {
318  };
319  const type = "webpack";
320  const entryPointModuleId = 1;
321  const output = transformRequires(modules, knownPaths, entryPointModuleId, type);
322
323  assert.deepEqual(output, expectedOutput);
324});
325
326it('1: require(2) => 1: require("foo")', () => {
327  const modules = [
328    {
329      id: 1,
330      code: generateFunction(
331        generateRequire(2)
332      ),
333      lookup: {'foo': 2},
334    },
335    {
336      id: 2,
337      code: generateFunction(),
338      lookup: {},
339    },
340  ];
341  const expectedOutput = [
342    {
343      id: 1,
344      code: generateFunction(
345        generateRequire('foo')
346      ),
347      lookup: {'foo': 2},
348    },
349    {
350      id: 2,
351      code: generateFunction(),
352      lookup: {},
353    },
354  ];
355
356  const knownPaths = {
357  };
358  const type = "webpack";
359  const entryPointModuleId = 1;
360  const output = transformRequires(modules, knownPaths, entryPointModuleId, type);
361
362  assert.deepEqual(output, expectedOutput);
363});
364
365// Tests with a require call containing a variable
366
367it('1: require(variable) => 1: require(variable)', () => {
368  const modules = [
369    {
370      id: 1,
371      code: generateFunction(
372        {
373          type: 'CallExpression',
374          callee: {
375            type: 'Identifier',
376            name: 'require',
377          },
378          arguments: [{
379            type: 'Identifier',
380            name: 'require',
381          }],
382        }
383      ),
384      lookup: {'./foo': 2},
385    },
386    {
387      id: 2,
388      code: generateFunction(),
389      lookup: {},
390    },
391  ];
392  const expectedOutput = modules;
393
394
395  const knownPaths = {
396  };
397  const type = "webpack";
398  const entryPointModuleId = 1;
399  const output = transformRequires(modules, knownPaths, entryPointModuleId, type);
400
401  assert.deepEqual(output, expectedOutput);
402});
403
404// Tests with mangled variables in the closure surrounding the module
405
406it('1: mangledRequire(2) => 1: mangledRequire("./2")', () => {
407  const modules = [
408    {
409      id: 1,
410      code: generateMangledFunction(
411        generateMangledRequire(2)
412      ),
413    },
414    {
415      id: 2,
416      code: generateFunction(),
417    },
418  ];
419  const expectedOutput = [
420    {
421      id: 1,
422      code: generateFunction(
423        generateRequire('./2')
424      ),
425    },
426    {
427      id: 2,
428      code: generateFunction(),
429    },
430  ];
431
432
433  const knownPaths = {
434  };
435  const entryPointModuleId = 1;
436  const type = "webpack";
437  const output = transformRequires(modules, knownPaths, entryPointModuleId, type);
438
439  assert.deepEqual(output, expectedOutput);
440});
441
442it('1: mangledRequire => 1: require  (where mangledRequire is just an identifier)', () => {
443  const modules = [
444    {
445      id: 1,
446      code: generateMangledFunction(
447        {type: 'Identifier', name: 'mangledRequire'}
448      ),
449    },
450    {
451      id: 2,
452      code: generateFunction(),
453    },
454  ];
455  const expectedOutput = [
456    {
457      id: 1,
458      code: generateFunction(
459        {type: 'Identifier', name: 'require'}
460      ),
461    },
462    {
463      id: 2,
464      code: generateFunction(),
465    },
466  ];
467
468
469  const knownPaths = {
470  };
471  const entryPointModuleId = 1;
472  const type = "webpack";
473  const output = transformRequires(modules, knownPaths, entryPointModuleId, type);
474
475  assert.deepEqual(output, expectedOutput);
476});
477
478// Tests replaceRequires = `variable`, such that when set, requires are aliased at the top of the
479// module closure instead of replaced.
480
481it('1: mangledRequire(2) => 1: mangledRequire("./2")   (where replaceRequires = variable)', () => {
482  const modules = [
483    {
484      id: 1,
485      code: generateMangledFunction(
486        generateMangledRequire(2)
487      ),
488    },
489    {
490      id: 2,
491      code: generateFunction(),
492    },
493  ];
494  const expectedOutput = [
495    {
496      id: 1,
497      code: generateMangledFunction(
498        generateVariableAssignment('mangledExports', {type: 'Identifier', name: 'exports'}),
499        generateVariableAssignment('mangledModule', {type: 'Identifier', name: 'module'}),
500        generateVariableAssignment('mangledRequire', {type: 'Identifier', name: 'require'}),
501        generateMangledRequire('./2')
502      ),
503    },
504    {
505      id: 2,
506      code: generateFunction(),
507    },
508  ];
509
510
511  const knownPaths = {
512  };
513  const entryPointModuleId = 1;
514  const type = "webpack";
515  const replaceRequires = "variable";
516  const output = transformRequires(modules, knownPaths, entryPointModuleId, type, replaceRequires);
517
518  assert.deepEqual(output, expectedOutput);
519});
520
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Cypress on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)