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

Commit

Permalink
fix: use dynamic import() in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Jan 28, 2022
1 parent 4404671 commit dcf9fed
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 60 deletions.
2 changes: 0 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
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',
},
},
],
Expand Down
11 changes: 10 additions & 1 deletion tests/helpers/main.js
Original file line number Diff line number Diff line change
@@ -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 @@ -55,7 +56,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 @@ -106,11 +107,19 @@ 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,
zipFixture,
unzipFiles,
zipCheckFunctions,
FIXTURES_DIR,
importFunctionFile,
}

0 comments on commit dcf9fed

Please sign in to comment.