Skip to content

Commit

Permalink
fix: use dynamic import() in tests (#980)
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Feb 8, 2022
1 parent a941003 commit 5003df6
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 59 deletions.
2 changes: 0 additions & 2 deletions .eslintrc.cjs
Expand Up @@ -14,10 +14,8 @@ module.exports = {
files: 'tests/**/*.js',
rules: {
'import/max-dependencies': 'off',
'import/no-dynamic-require': 'off',
'max-lines-per-function': 'off',
'max-statements': 'off',
'node/global-require': 'off',
'no-magic-numbers': 'off',
},
},
Expand Down
11 changes: 10 additions & 1 deletion tests/helpers/main.js
@@ -1,6 +1,7 @@
const { mkdirSync } = require('fs')
const { dirname, join, resolve } = require('path')
const { env, platform } = require('process')
const { pathToFileURL } = require('url')

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

const jsFiles = files.map(replaceUnzipPath).map(require)
const jsFiles = await Promise.all(files.map(replaceUnzipPath).map((file) => importFunctionFile(file)))
t.true(jsFiles.every(Boolean))
}

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

// Import a file exporting a function.
// Returns `default` exports as is.
const importFunctionFile = async function (functionPath) {
const result = await import(pathToFileURL(functionPath))
return result.default === undefined ? result : result.default
}

module.exports = {
getRequires,
zipNode,
Expand All @@ -115,4 +123,5 @@ module.exports = {
zipCheckFunctions,
FIXTURES_DIR,
BINARY_PATH,
importFunctionFile,
}

1 comment on commit 5003df6

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⏱ Benchmark results

largeDepsEsbuild: 7s

largeDepsZisi: 57.6s

Please sign in to comment.