Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Bug: Errorhandling fails when node:module registered errors are thrown #5085

Closed
4 tasks done
stalet opened this issue Jan 22, 2024 · 2 comments · Fixed by #5074
Closed
4 tasks done

🐛 Bug: Errorhandling fails when node:module registered errors are thrown #5085

stalet opened this issue Jan 22, 2024 · 2 comments · Fixed by #5074
Labels
status: accepting prs Mocha can use your help with this one! type: bug a defect, confirmed by a maintainer

Comments

@stalet
Copy link
Contributor

stalet commented Jan 22, 2024

Bug Report Checklist

  • I have read and agree to Mocha's Code of Conduct and Contributing Guidelines
  • I have searched for related issues and issues with the faq label, but none matched my issue.
  • I have 'smoke tested' the code to be tested by running it outside the real test suite to get a better sense of whether the problem is in the code under test, my usage of Mocha, or Mocha itself.
  • I want to provide a PR to resolve this

Expected

When running a test that throws a null error i expect to get information about what happened and the errorhandler to give useful output.

Actual

When an null error is thrown when running the tests the output from the mocha script is the program option list and a line:
✖ ERROR: null

Minimal, Reproducible Example

I created a PR for this error - thinking this was only applicable for Typescript errors, but as stated in the PR: all null errors it will cause this to happen.

Nevertheless, the example created for the original PR shows the error:
https://github.com/stalet/mocha-typescript-testproject

Versions

10.2.0

Additional Info

This error was reported as part of PR #5074 - but since it's not just applicable for Typescript errors the PR was closed - and I am opening this issue instead.

@stalet stalet added the type: bug a defect, confirmed by a maintainer label Jan 22, 2024
@stalet
Copy link
Contributor Author

stalet commented Jan 22, 2024

And as a sidenote - something must have changed in the latest versions - since I am now discovering this issue in multiple projects where i am using ts-node along with mocha.

@JoshuaKGoldberg
Copy link
Member

JoshuaKGoldberg commented Feb 7, 2024

I did a little digging. From https://github.com/stalet/mocha-typescript-testproject?tab=readme-ov-file#debugging-test:

  mocha:cli:cli caught error sometime before command handler: TypeError: Cannot convert object to primitive value
  mocha:cli:cli     at exports.handler (/home/stalet/dev/test/node_modules/.pnpm/mocha@10.2.0/node_modules/mocha/lib/cli/run.js:372:65) +674ms

That's:

console.error('\n' + (err.stack || `Error: ${err.message || err}`));

Putting a breakpoint in there (mocha test/index.test.ts --inspect-brk), err is:

> err
test/index.test.ts(5,1): error TS2582: Cannot find name 'describe'. Do you need to install type definitions for a test runner? Try `npm i --save-dev @types/jest` or `npm i --save-dev @types/mocha`.
test/index.test.ts(6,5): error TS2582: Cannot find name 'it'. Do you need to install type definitions for a test runner? Try `npm i --save-dev @types/jest` or `npm i --save-dev @types/mocha`.
test/index.test.ts(9,44): error TS2322: Type 'string' is not assignable to type 'number'.

> typeof err
'object'

> err.stack
undefined

> String(err)
Uncaught TypeError TypeError: Cannot convert object to primitive value

> Object.getOwnPropertyDescriptors(err)
{Symbol(nodejs.util.inspect.custom): {…}}

...so, the issue is not that the thrown error is null. It's that it's an object from a loader/worker thread that comes in with an undefined prototype. Which can't be converted/coerced to a string the way the code is trying to because it doesn't have a toString() method: https://stackoverflow.com/questions/41164750/cannot-convert-object-to-primitive-value.

nodejs/node#48207 is when those objects were fixed to be printable in a debugger. Adding "ts-node": { "transpileOnly": true } to the tsconfig.json per nodejs/node#48207 (comment) was enough to get correct printing in the repro!

 $ npx mocha test/index.test.ts              


  stuff
    1) transforms stuff


  0 passing (3ms)
  1 failing

  1) stuff
       transforms stuff:

      AssertionError: expected '{"foo":"foo","bar":"1"}' to equal '{"foo":"foo","bar":1}'
      + expected - actual

      -{"foo":"foo","bar":"1"}
      +{"foo":"foo","bar":1}
      
      at Context.<anonymous> (file:///Users/josh/repos/mocha-typescript-testproject/test/index.test.ts:7:61)
      at process.processImmediate (node:internal/timers:478:21)

https://nodejs.org/api/module.html#moduleregisterspecifier-parenturl-options -> https://nodejs.org/api/module.html#customization-hooks has context for what's going on. https://nodejs.org/api/util.html#custom-inspection-functions-on-objects specifically shows how turn these objects into a string.

const util = require('node:util');

const stringified = util.inspect(err);

console.log(stringified);
'test/index.test.ts(5,1): error TS2582: Cannot find name 'describe'. Do you need to install type definitions for a test runner? Try `npm i --save-dev @types/jest` or `npm i --save-dev @types/mocha`.
test/index.test.ts(6,5): error TS2582: Cannot find name 'it'. Do you need to install type definitions for a test runner? Try `npm i --save-dev @types/jest` or `npm i --save-dev @types/mocha`.
test/index.test.ts(9,44): error TS2322: Type 'string' is not assignable to type 'number'.
'

We can therefore narrow down that the issue is that errors thrown within a node:module's register-d module need to have explicit handling for being printed as strings. 😎

@JoshuaKGoldberg JoshuaKGoldberg changed the title 🐛 Bug: Errorhandling fails when null errors are thrown 🐛 Bug: Errorhandling fails when node:module registered errors are thrown Feb 7, 2024
@JoshuaKGoldberg JoshuaKGoldberg added the status: accepting prs Mocha can use your help with this one! label Feb 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: accepting prs Mocha can use your help with this one! type: bug a defect, confirmed by a maintainer
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants