Skip to content

Commit

Permalink
Fix resolve legacy requires
Browse files Browse the repository at this point in the history
Closes #191
  • Loading branch information
inukshuk committed Nov 29, 2021
1 parent 7fbd8a7 commit 8d7c059
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
17 changes: 14 additions & 3 deletions lib/mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const Mocha = require('mocha')
const errors = require('mocha/lib/errors')
const { resolve } = require('path')
const { existsSync } = require('fs')

const {
handleRequires,
Expand All @@ -16,15 +17,24 @@ const {
ONE_AND_DONES
} = require('mocha/lib/cli/one-and-dones')

const cwd = process.cwd()

const handleLegacyRequires = async (mods) => {
for (let mod of mods) {
if (existsSync(mod, { cwd }) || existsSync(`${mod}.js`, { cwd })) {
mod = resolve(mod)
}
require(mod)
}
}

const runMocha = async (argv, done) => {
Error.stackTraceLimit = Infinity

if (process.type === 'renderer') {
// Node.js ESM loader is not available in Electron Renderer
// so we require dependencies explicitly here.
for (const mod of argv.require) {
require(resolve(mod))
}
await handleLegacyRequires(argv.require)
} else {
await handleRequires(argv.require)
}
Expand All @@ -50,6 +60,7 @@ module.exports = {
errors,
helpers: {
handleRequires,
handleLegacyRequires,
runMocha
}
}
7 changes: 1 addition & 6 deletions lib/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,6 @@ exports.builder = yargs => {
argv.inspect = true
}

if (argv['require-main']) {
argv['require-main'] = argv['require-main'].map(script => resolve(script))
}
if (argv.script) {
argv.script = argv.script.map(script => resolve(script))
}
Expand All @@ -152,9 +149,7 @@ exports.builder = yargs => {
exports.handler = async argv => {
try {
if (argv['require-main']) {
for (const mod of argv['require-main']) {
require(mod)
}
await helpers.handleLegacyRequires(argv['require-main'])
}

await app.whenReady()
Expand Down

0 comments on commit 8d7c059

Please sign in to comment.