Skip to content
This repository was archived by the owner on May 22, 2024. It is now read-only.

Commit 5003df6

Browse files
authoredFeb 8, 2022
fix: use dynamic import() in tests (#980)
1 parent a941003 commit 5003df6

File tree

3 files changed

+91
-59
lines changed

3 files changed

+91
-59
lines changed
 

‎.eslintrc.cjs

-2
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@ module.exports = {
1414
files: 'tests/**/*.js',
1515
rules: {
1616
'import/max-dependencies': 'off',
17-
'import/no-dynamic-require': 'off',
1817
'max-lines-per-function': 'off',
1918
'max-statements': 'off',
20-
'node/global-require': 'off',
2119
'no-magic-numbers': 'off',
2220
},
2321
},

‎tests/helpers/main.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const { mkdirSync } = require('fs')
22
const { dirname, join, resolve } = require('path')
33
const { env, platform } = require('process')
4+
const { pathToFileURL } = require('url')
45

56
const execa = require('execa')
67
const { dir: getTmpDir } = require('tmp-promise')
@@ -56,7 +57,7 @@ const zipCheckFunctions = async function (t, fixture, { length = 1, fixtureDir =
5657
const requireExtractedFiles = async function (t, files) {
5758
await unzipFiles(files)
5859

59-
const jsFiles = files.map(replaceUnzipPath).map(require)
60+
const jsFiles = await Promise.all(files.map(replaceUnzipPath).map((file) => importFunctionFile(file)))
6061
t.true(jsFiles.every(Boolean))
6162
}
6263

@@ -107,6 +108,13 @@ const getRequires = async function ({ depth = Number.POSITIVE_INFINITY, filePath
107108
return [...requires, ...childRequires]
108109
}
109110

111+
// Import a file exporting a function.
112+
// Returns `default` exports as is.
113+
const importFunctionFile = async function (functionPath) {
114+
const result = await import(pathToFileURL(functionPath))
115+
return result.default === undefined ? result : result.default
116+
}
117+
110118
module.exports = {
111119
getRequires,
112120
zipNode,
@@ -115,4 +123,5 @@ module.exports = {
115123
zipCheckFunctions,
116124
FIXTURES_DIR,
117125
BINARY_PATH,
126+
importFunctionFile,
118127
}

‎tests/main.js

+81-56
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const {
4040
zipCheckFunctions,
4141
FIXTURES_DIR,
4242
BINARY_PATH,
43+
importFunctionFile,
4344
} = require('./helpers/main')
4445
const { computeSha1 } = require('./helpers/sha')
4546
const { makeTestMany } = require('./helpers/test_many')
@@ -201,7 +202,7 @@ testMany(
201202
opts: options,
202203
})
203204

204-
const func = require(`${tmpDir}/function.js`)
205+
const func = await importFunctionFile(`${tmpDir}/function.js`)
205206

206207
t.deepEqual(func, { mock: { stack: 'jam' }, stack: 'jam' })
207208
},
@@ -254,7 +255,7 @@ testMany(
254255
t.false(await pathExists(`${tmpDir}/node_modules/aws-sdk`))
255256

256257
try {
257-
const func = require(`${tmpDir}/function.js`)
258+
const func = await importFunctionFile(`${tmpDir}/function.js`)
258259

259260
func()
260261

@@ -374,7 +375,7 @@ testMany(
374375

375376
await unzipFiles([result])
376377

377-
const func = require(join(tmpDir, 'function.js'))
378+
const func = await importFunctionFile(join(tmpDir, 'function.js'))
378379

379380
t.true(func)
380381
},
@@ -456,10 +457,10 @@ testMany(
456457
join(tmpDir, 'function_export_only.zip_out', 'function_export_only.js'),
457458
join(tmpDir, 'function_import_only.zip_out', 'function_import_only.js'),
458459
]
459-
const func1 = () => require(functionPaths[0])
460-
const func2 = () => require(functionPaths[1])
461-
const func3 = () => require(functionPaths[2])
462-
const func4 = () => require(functionPaths[3])
460+
const func1 = () => importFunctionFile(functionPaths[0])
461+
const func2 = () => importFunctionFile(functionPaths[1])
462+
const func3 = () => importFunctionFile(functionPaths[2])
463+
const func4 = () => importFunctionFile(functionPaths[3])
463464

464465
const functionsAreESM = await Promise.all(
465466
functionPaths.map((functionPath) => detectEsModule({ mainFile: functionPath })),
@@ -470,12 +471,15 @@ testMany(
470471

471472
// Dynamic imports are not supported in Node <13.2.0.
472473
if (semver.gte(nodeVersion, '13.2.0')) {
473-
t.is(await func2()(), 0)
474+
const func2Default = await func2()
475+
t.is(await func2Default(), 0)
474476
}
475477

476-
t.is(func1().ZERO, 0)
477-
t.is(typeof func3().howdy, 'string')
478-
t.deepEqual(func4(), {})
478+
const { ZERO } = await func1()
479+
t.is(ZERO, 0)
480+
const { howdy } = await func3()
481+
t.is(typeof howdy, 'string')
482+
t.deepEqual(await func4(), {})
479483
},
480484
)
481485

@@ -506,10 +510,10 @@ testMany(
506510
join(tmpDir, 'function_export_only', 'function_export_only.js'),
507511
join(tmpDir, 'function_import_only', 'function_import_only.js'),
508512
]
509-
const func1 = () => require(functionPaths[0])
510-
const func2 = () => require(functionPaths[1])
511-
const func3 = () => require(functionPaths[2])
512-
const func4 = () => require(functionPaths[3])
513+
const func1 = () => importFunctionFile(functionPaths[0])
514+
const func2 = () => importFunctionFile(functionPaths[1])
515+
const func3 = () => importFunctionFile(functionPaths[2])
516+
const func4 = () => importFunctionFile(functionPaths[3])
513517

514518
const functionsAreESM = await Promise.all(
515519
functionPaths.map((functionPath) => detectEsModule({ mainFile: functionPath })),
@@ -520,12 +524,15 @@ testMany(
520524

521525
// Dynamic imports are not supported in Node <13.2.0.
522526
if (semver.gte(nodeVersion, '13.2.0')) {
523-
t.is(await func2()(), 0)
527+
const func2Default = await func2()
528+
t.is(await func2Default(), 0)
524529
}
525530

526-
t.is(func1().ZERO, 0)
527-
t.is(typeof func3().howdy, 'string')
528-
t.deepEqual(func4(), {})
531+
const { ZERO } = await func1()
532+
t.is(ZERO, 0)
533+
const { howdy } = await func3()
534+
t.is(typeof howdy, 'string')
535+
t.deepEqual(await func4(), {})
529536
},
530537
)
531538

@@ -540,7 +547,7 @@ testMany(
540547

541548
await unzipFiles(files)
542549

543-
const func = require(join(tmpDir, 'function.js'))
550+
const func = await importFunctionFile(join(tmpDir, 'function.js'))
544551

545552
// Dynamic imports were added in Node v13.2.0.
546553
if (semver.gte(nodeVersion, '13.2.0')) {
@@ -718,7 +725,8 @@ testMany(
718725
opts: options,
719726
})
720727
await unzipFiles(files)
721-
t.true(require(`${tmpDir}/function.js`))
728+
const returnValue = await importFunctionFile(`${tmpDir}/function.js`)
729+
t.true(returnValue)
722730
t.is(files[0].mainFile, join(FIXTURES_DIR, fixtureName, 'function', 'index.js'))
723731
},
724732
)
@@ -1333,7 +1341,8 @@ testMany(
13331341
async (options, t) => {
13341342
const { files, tmpDir } = await zipFixture(t, 'node-fetch', { opts: options })
13351343
await unzipFiles(files)
1336-
t.true(typeof require(`${tmpDir}/function.js`) === 'function')
1344+
const returnValue = await importFunctionFile(`${tmpDir}/function.js`)
1345+
t.true(typeof returnValue === 'function')
13371346
},
13381347
)
13391348

@@ -1345,7 +1354,8 @@ testMany(
13451354
opts: options,
13461355
})
13471356
await unzipFiles(files)
1348-
t.is(require(`${tmpDir}/function.js`), 'function-js-file-in-directory')
1357+
const returnValue = await importFunctionFile(`${tmpDir}/function.js`)
1358+
t.is(returnValue, 'function-js-file-in-directory')
13491359
},
13501360
)
13511361

@@ -1357,7 +1367,8 @@ testMany(
13571367
opts: options,
13581368
})
13591369
await unzipFiles(files)
1360-
t.is(require(`${tmpDir}/function.js`), 'index-js-file-in-directory')
1370+
const returnValue = await importFunctionFile(`${tmpDir}/function.js`)
1371+
t.is(returnValue, 'index-js-file-in-directory')
13611372
},
13621373
)
13631374

@@ -1369,7 +1380,8 @@ testMany(
13691380
opts: options,
13701381
})
13711382
await unzipFiles(files)
1372-
t.is(require(`${tmpDir}/function.js`).type, 'index-js-file-in-directory')
1383+
const { type } = await importFunctionFile(`${tmpDir}/function.js`)
1384+
t.is(type, 'index-js-file-in-directory')
13731385
},
13741386
)
13751387

@@ -1381,7 +1393,8 @@ testMany(
13811393
opts: options,
13821394
})
13831395
await unzipFiles(files)
1384-
t.is(require(`${tmpDir}/function.js`).type, 'function-js-file')
1396+
const { type } = await importFunctionFile(`${tmpDir}/function.js`)
1397+
t.is(type, 'function-js-file')
13851398
},
13861399
)
13871400

@@ -1393,7 +1406,8 @@ testMany(
13931406
opts: options,
13941407
})
13951408
await unzipFiles(files)
1396-
t.is(require(`${tmpDir}/function.js`).type, 'function-js-file')
1409+
const { type } = await importFunctionFile(`${tmpDir}/function.js`)
1410+
t.is(type, 'function-js-file')
13971411
},
13981412
)
13991413

@@ -1405,7 +1419,8 @@ testMany(
14051419
opts: options,
14061420
})
14071421
await unzipFiles(files)
1408-
t.true(typeof require(`${tmpDir}/function.js`).type === 'string')
1422+
const { type } = await importFunctionFile(`${tmpDir}/function.js`)
1423+
t.true(typeof type === 'string')
14091424
},
14101425
)
14111426

@@ -1417,7 +1432,8 @@ testMany(
14171432
opts: options,
14181433
})
14191434
await unzipFiles(files)
1420-
t.true(typeof require(`${tmpDir}/function.js`).type === 'string')
1435+
const { type } = await importFunctionFile(`${tmpDir}/function.js`)
1436+
t.true(typeof type === 'string')
14211437
},
14221438
)
14231439

@@ -1429,7 +1445,8 @@ testMany(
14291445
opts: options,
14301446
})
14311447
await unzipFiles(files)
1432-
t.true(typeof require(`${tmpDir}/function.js`).type === 'string')
1448+
const { type } = await importFunctionFile(`${tmpDir}/function.js`)
1449+
t.true(typeof type === 'string')
14331450
},
14341451
)
14351452

@@ -1441,7 +1458,8 @@ testMany(
14411458
opts: options,
14421459
})
14431460
await unzipFiles(files)
1444-
t.true(typeof require(`${tmpDir}/function.js`).type === 'string')
1461+
const { type } = await importFunctionFile(`${tmpDir}/function.js`)
1462+
t.true(typeof type === 'string')
14451463
},
14461464
)
14471465

@@ -1461,9 +1479,12 @@ testMany(
14611479
t.is(file.bundler, 'esbuild')
14621480
})
14631481

1464-
t.true(require(`${tmpDir}/func1.js`).handler())
1465-
t.true(require(`${tmpDir}/func2.js`).handler())
1466-
t.true(require(`${tmpDir}/func3.js`).handler())
1482+
const { handler: handler1 } = await importFunctionFile(`${tmpDir}/func1.js`)
1483+
t.true(handler1())
1484+
const { handler: handler2 } = await importFunctionFile(`${tmpDir}/func2.js`)
1485+
t.true(handler2())
1486+
const { handler: handler3 } = await importFunctionFile(`${tmpDir}/func3.js`)
1487+
t.true(handler3())
14671488
},
14681489
)
14691490

@@ -1475,7 +1496,8 @@ testMany(
14751496
opts: options,
14761497
})
14771498
await unzipFiles(files)
1478-
t.true(require(`${tmpDir}/function.js`).value)
1499+
const { value } = await importFunctionFile(`${tmpDir}/function.js`)
1500+
t.true(value)
14791501
},
14801502
)
14811503

@@ -1487,7 +1509,8 @@ testMany(
14871509
opts: options,
14881510
})
14891511
await unzipFiles(files)
1490-
t.true(require(`${tmpDir}/function.js`).value)
1512+
const { value } = await importFunctionFile(`${tmpDir}/function.js`)
1513+
t.true(value)
14911514
},
14921515
)
14931516

@@ -1500,7 +1523,7 @@ testMany(
15001523
})
15011524
await unzipFiles(files)
15021525

1503-
const result = require(`${tmpDir}/function.js`)
1526+
const result = await importFunctionFile(`${tmpDir}/function.js`)
15041527

15051528
// We want to assert that the `target` specified in the tsconfig file (es5)
15061529
// was overridden by our own target. It's not easy to assert that without
@@ -1625,7 +1648,7 @@ testMany(
16251648
opts,
16261649
})
16271650

1628-
const functionEntry = require(`${files[0].path}/function.js`)
1651+
const functionEntry = await importFunctionFile(`${files[0].path}/function.js`)
16291652

16301653
t.true(functionEntry)
16311654
},
@@ -1648,7 +1671,7 @@ testMany(
16481671
opts,
16491672
})
16501673

1651-
const func = require(`${tmpDir}/func1.js`)
1674+
const func = await importFunctionFile(`${tmpDir}/func1.js`)
16521675

16531676
const { body: body1 } = await func.handler({ queryStringParameters: { name: 'post1' } })
16541677
const { body: body2 } = await func.handler({ queryStringParameters: { name: 'post2' } })
@@ -1710,7 +1733,7 @@ testMany(
17101733
t.false(files[0].inputs.includes(join(FIXTURES_DIR, fixtureName, 'node_modules', 'test-child', 'unused_file.js')))
17111734
}
17121735

1713-
const functionEntry = require(`${tmpDir}/function.js`)
1736+
const functionEntry = await importFunctionFile(`${tmpDir}/function.js`)
17141737

17151738
t.true(functionEntry)
17161739
},
@@ -1733,7 +1756,7 @@ testMany(
17331756
opts,
17341757
})
17351758

1736-
const function1Entry = require(`${tmpDir}/func1.js`)
1759+
const function1Entry = await importFunctionFile(`${tmpDir}/func1.js`)
17371760

17381761
// The function should not be on a `src/` namespace.
17391762
t.false(unixify(function1Entry[0]).includes('/src/'))
@@ -1764,7 +1787,7 @@ testMany(
17641787
opts,
17651788
})
17661789

1767-
const function2Entry = require(`${tmpDir}/func2.js`)
1790+
const function2Entry = await importFunctionFile(`${tmpDir}/func2.js`)
17681791

17691792
// The function should be on a `src/` namespace because there's a conflict
17701793
// with the /func2.js path present in `includedFiles`.
@@ -1794,9 +1817,9 @@ testMany(
17941817
opts,
17951818
})
17961819

1797-
const functionCommon = require(`${tmpDir}/function.js`)
1798-
const functionInternal = require(`${tmpDir}/function_internal.js`)
1799-
const functionUser = require(`${tmpDir}/function_user.js`)
1820+
const functionCommon = await importFunctionFile(`${tmpDir}/function.js`)
1821+
const functionInternal = await importFunctionFile(`${tmpDir}/function_internal.js`)
1822+
const functionUser = await importFunctionFile(`${tmpDir}/function_user.js`)
18001823

18011824
// Functions from rightmost directories in the array take precedence.
18021825
t.is(functionCommon, 'user')
@@ -1831,7 +1854,7 @@ test('When generating a directory for a function with `archiveFormat: "none"`, i
18311854
archiveFormat: 'none',
18321855
})
18331856

1834-
const functionEntry = require(`${functionDirectory}/function.js`)
1857+
const functionEntry = await importFunctionFile(`${functionDirectory}/function.js`)
18351858

18361859
t.true(functionEntry)
18371860

@@ -1917,7 +1940,7 @@ test('Adds a runtime shim and includes the files needed for dynamic imports usin
19171940
},
19181941
})
19191942

1920-
const func = require(`${tmpDir}/function.js`)
1943+
const func = await importFunctionFile(`${tmpDir}/function.js`)
19211944
const values = func('one')
19221945
const expectedLength = 5
19231946

@@ -1952,7 +1975,7 @@ test('Adds a runtime shim and includes the files needed for dynamic imports usin
19521975
},
19531976
})
19541977

1955-
const func = require(`${tmpDir}/function.js`)
1978+
const func = await importFunctionFile(`${tmpDir}/function.js`)
19561979

19571980
t.deepEqual(func('en')[0], ['yes', 'no'])
19581981
t.deepEqual(func('en')[1], ['yes', 'no'])
@@ -1970,7 +1993,7 @@ test('The dynamic import runtime shim handles files in nested directories', asyn
19701993
},
19711994
})
19721995

1973-
const func = require(`${tmpDir}/function.js`)
1996+
const func = await importFunctionFile(`${tmpDir}/function.js`)
19741997

19751998
t.deepEqual(func('en')[0], ['yes', 'no'])
19761999
t.deepEqual(func('en')[1], ['yes', 'no'])
@@ -1991,7 +2014,7 @@ test('The dynamic import runtime shim handles files in nested directories when u
19912014
},
19922015
})
19932016

1994-
const func = require(`${tmpDir}/function/function.js`)
2017+
const func = await importFunctionFile(`${tmpDir}/function/function.js`)
19952018

19962019
t.deepEqual(func('en')[0], ['yes', 'no'])
19972020
t.deepEqual(func('en')[1], ['yes', 'no'])
@@ -2011,7 +2034,7 @@ test('Negated files in `included_files` are excluded from the bundle even if the
20112034
},
20122035
})
20132036

2014-
const func = require(`${tmpDir}/function.js`)
2037+
const func = await importFunctionFile(`${tmpDir}/function.js`)
20152038

20162039
t.deepEqual(func('pt')[0], ['sim', 'não'])
20172040
t.deepEqual(func('pt')[1], ['sim', 'não'])
@@ -2052,7 +2075,7 @@ test('Creates dynamic import shims for functions with the same name and same shi
20522075
})
20532076

20542077
for (let ind = 1; ind <= FUNCTION_COUNT; ind++) {
2055-
const func = require(`${tmpDir}/function${ind}.js`)
2078+
const func = await importFunctionFile(`${tmpDir}/function${ind}.js`)
20562079

20572080
t.deepEqual(func('en')[0], ['yes', 'no'])
20582081
t.deepEqual(func('en')[1], ['yes', 'no'])
@@ -2072,7 +2095,7 @@ test('Creates dynamic import shims for functions using `zipFunction`', async (t)
20722095

20732096
await unzipFiles([result])
20742097

2075-
const func = require(`${tmpDir}/function.js`)
2098+
const func = await importFunctionFile(`${tmpDir}/function.js`)
20762099

20772100
t.deepEqual(func('en')[0], ['yes', 'no'])
20782101
t.deepEqual(func('en')[1], ['yes', 'no'])
@@ -2438,6 +2461,7 @@ test('Creates a manifest file with the list of created functions if the `manifes
24382461
},
24392462
})
24402463

2464+
// eslint-disable-next-line import/no-dynamic-require, node/global-require
24412465
const manifest = require(manifestPath)
24422466

24432467
t.is(manifest.version, 1)
@@ -2469,7 +2493,7 @@ testMany(
24692493
opts,
24702494
})
24712495

2472-
const isEven = require(`${tmpDir}/function`)
2496+
const isEven = await importFunctionFile(`${tmpDir}/function.js`)
24732497
t.is(isEven(2), '2 is even')
24742498
},
24752499
)
@@ -2494,7 +2518,7 @@ testMany(
24942518

24952519
await unzipFiles([result])
24962520

2497-
const { mock1, mock2 } = require(`${tmpDir}/function-1.js`)
2521+
const { mock1, mock2 } = await importFunctionFile(`${tmpDir}/function-1.js`)
24982522

24992523
t.true(mock1)
25002524
t.true(mock2)
@@ -2560,6 +2584,7 @@ testMany(
25602584

25612585
files.every((file) => t.is(file.schedule, schedule))
25622586

2587+
// eslint-disable-next-line import/no-dynamic-require, node/global-require
25632588
const manifest = require(manifestPath)
25642589

25652590
manifest.functions.forEach((fn) => {
@@ -2579,7 +2604,7 @@ test('Generates a sourcemap for any transpiled files when `nodeSourcemap: true`'
25792604
featureFlags: { nftTranspile: true },
25802605
},
25812606
})
2582-
const func = require(join(files[0].path, 'function.js'))
2607+
const func = await importFunctionFile(join(files[0].path, 'function.js'))
25832608

25842609
try {
25852610
func.handler()

1 commit comments

Comments
 (1)

github-actions[bot] commented on Feb 8, 2022

@github-actions[bot]
Contributor

⏱ Benchmark results

largeDepsEsbuild: 7s

largeDepsZisi: 57.6s

This repository has been archived.