Skip to content

fix: resolve relative paths in unloadFile against the working directory - #6468

Closed
RaphaelFakhri wants to merge 1 commit into
mochajs:mainfrom
RaphaelFakhri:fix/unload-file-relative-path
Closed

RaphaelFakhri wants to merge 1 commit into
mochajs:mainfrom
RaphaelFakhri:fix/unload-file-relative-path

Conversation

@RaphaelFakhri

Copy link
Copy Markdown

PR Checklist

Overview

Mocha#loadFiles resolves each file with path.resolve(), so relative paths are relative to the current working directory. Mocha.unloadFile passes the path straight to a require.resolve created in lib/nodejs/, so a relative path resolves from there instead. Since #6355, the resulting MODULE_NOT_FOUND is swallowed, so the file silently stays in the require cache and the next run registers no tests:

const mocha = new Mocha();
mocha.addFile("test/a.spec.js");
mocha.run(() => {
  mocha.unloadFiles();
  mocha.run(); // 0 passing
});

This change resolves the path with path.resolve() before require.resolve(), matching loadFiles. Absolute paths, which watch mode uses, are unchanged.

A new unit test in test/node-unit/mocha.spec.cjs unloads a file by its cwd-relative path. It fails without the fix and passes with it. npm run test-node:unit and the watch integration tests pass.

This picks up the fix from #4549 and #6259, which were closed before review.

@linux-foundation-easycla

Copy link
Copy Markdown

CLA Not Signed

@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

Automation signals

Activity patterns show signs of automation.

View full analysis →

Report this account →

Evidence
  • Extreme fork automation: 60 repositories forked in rapid succession (within 24 hours)
  • Very high PR volume (daily): 62 PRs within a single 24-hour window
  • High PR volume (weekly): 62 PRs within a single 7-day window

Last 5 PRs:

This is an automated analysis by AgentScan

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

unloadFiles throws MODULE_NOT_FOUND in ESM context for files added with relative path

1 participant