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

fix: use dynamic import() in tests #980

Merged
merged 1 commit into from
Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 0 additions & 2 deletions .eslintrc.cjs
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',
'no-magic-numbers': '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 @@ -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,
}