Skip to content

Conversation

@T-Svensson
Copy link

Align tryImportAndRequire and requireModule functions to throw the initial error message.

PR Checklist

Overview

When a typescript based project uses mocha and NodeJS 20.19 or later and there is a syntax error in one of the typescript files, mocha will produce an unhelpful error message:

$ yarn mocha -r ts-node/register test/index.spec.ts
yarn run v1.22.22
$ /tmp/mocha-demo/node_modules/.bin/mocha -r ts-node/register test/index.spec.ts

 Exception during run: TypeError: Unknown file extension ".ts" for /home/svenssot/tmp/mocha/test/index.spec.ts
    at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:189:9)
    at defaultGetFormat (node:internal/modules/esm/get_format:232:36)
    at defaultLoad (node:internal/modules/esm/load:145:22)
    at async ModuleLoader.loadAndTranslate (node:internal/modules/esm/loader:477:45)
    at async ModuleJob._link (node:internal/modules/esm/module_job:110:19) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

With this PR applied, the error message is instead what was previously shown with NodeJS 20.18 and below:

$ yarn mocha -r ts-node/register test/index.spec.ts
yarn run v1.22.22
$ /tmp/mocha-demo/node_modules/.bin/mocha -r ts-node/register test/index.spec.ts

 Exception during run: test/index.spec.ts:4:1 - error TS1005: '}' expected.

4


  test/index.spec.ts:1:23
    1 describe('foo', () => {
                            ~
    The parser expected to find a '}' to match the '{' token here.

error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

In both these cases, I've used the following content in test/index.spec.ts:

describe('foo', () => {
    it('bar', () => {
    //});
});

Align `tryImportAndRequire` and `requireModule` functions to throw the
initial error message.

Signed-off-by: Torbjörn Svensson <azoff@svenskalinuxforeningen.se>
@JoshuaKGoldberg
Copy link
Member

Hi @T-Svensson, thanks for starting on a PR - we appreciate your enthusiasm! But:

Addresses an existing open issue: fixes #000

We have that template & contributing guides for good reason 🙂. In this case it looks like you've hit what's already tracked by #5393. This PR looks to be a rough duplicate (though with slightly different error code handling) of the already-sent #5408. Because #5048 was sent earlier, I'm going to go ahead and close this one out.

If there are other cases you'd suggest be handled, please do send us a reproduction - either in a new issue (respecting the templates) or in #5408.

Thanks! 🤎

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants