fix: read ambigous TS files with type-stripping enabled with require first#1260
Open
shadowusr wants to merge 1 commit into
Open
fix: read ambigous TS files with type-stripping enabled with require first#1260shadowusr wants to merge 1 commit into
shadowusr wants to merge 1 commit into
Conversation
commit: |
KuznetsovRoman
approved these changes
May 22, 2026
Comment on lines
+16
to
+20
| - name: Use Node.js 24.x | ||
| uses: actions/setup-node@v4 | ||
| with: | ||
| node-version: 24.x | ||
| cache: "npm" |
Member
There was a problem hiding this comment.
I think we should run another node.js versions also: like "20" and "22"
If you checked it once and it works on 18/20/22 - its fine, we can keep "only node 24" check
Member
Author
There was a problem hiding this comment.
ESM wouldn't work on node < v20.17.0, and only 24 has typescript types stripping turned on by default. I'll verify once again locally on different node versions, but going to ultimately keep the check turned on for 24 only
Comment on lines
+94
to
+100
| for (const file of files) { | ||
| mocha.files = [file]; | ||
|
|
||
| if (!shouldLoadWithRequire(file)) { | ||
| await mocha.loadFilesAsync({ esmDecorator }); | ||
| continue; | ||
| } |
Member
There was a problem hiding this comment.
Why isn't it written like that?
const loadWithRequireFiles = [];
const loadDefaultFiles = [];
for (const file of files) {
if (shouldLoadWithRequire(file)) {
loadWithRequireFiles.push(file)
} else {
loadDefaultFiles.push(file)
}
}
// Load "loadDefaultFiles" with some kind of Promise.all
// Load "loadWithRequireFiles" with "mocha.loadFiles"
Member
Author
There was a problem hiding this comment.
We wouldn't gain anything from doing that: mocha itself loads files sequentially, here's the source of loadFilesAsync:
exports.loadFilesAsync = async (
files,
preLoadFunc,
postLoadFunc,
esmDecorator
) => {
for (const file of files) {
preLoadFunc(file);
const result = await exports.requireOrImport(
path.resolve(file),
esmDecorator
);
postLoadFunc(file, result);
}
};
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What's done?
Before, we had the following issue when node's built in typescript types stripping (https://nodejs.org/download/release/v24.1.0/docs/api/typescript.html#type-stripping) is enabled:
readFilesAsync, whichimport()s them first.imports in typescript file and tries to re-read the file as ESM, issuing the warningMODULE_TYPELESS_PACKAGE_JSON] Warning: Module type of some-file.hermione.ts?rand=0.4001007512274388 is not specified and it doesn't parse as CommonJS. Reparsing as ES module because module syntax was detectedThis means 3 file reads and 1 warning in console.
This PR adds the following logic:
loadFile(which usesrequire) and secondly if it fails,loadFileAsync.Also added 4 test projects that test file reading under different circumstances e2e.