How to properly handle "unhandled" exceptions

See original GitHub issue

Describe the bug

I have a test that asserts that an import throws, because the imported module is not found.

even though the actual error is caught by expect().rejects.toThrowError() it still shows up as unhandled.

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Unhandled Errors ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

Vitest caught 1 unhandled error during the test run.
This might cause false positive tests. Resolve unhandled errors to make sure your tests are not affected.

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Unhandled Rejection ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

Error: Cannot find module 'does-not-exist'
Require stack:
- /private/var/folders/y3/2b5xsndd3zvd32j6n_ypjg5r0000gn/T/zip-it-test-bundler-nft-31679-4ePKnlfzcJsT/node-module-missing/function.js
- /var/folders/y3/2b5xsndd3zvd32j6n_ypjg5r0000gn/T/zip-it-test-bundler-nft-31679-4ePKnlfzcJsT/function.js
 ❯ Function.Module._resolveFilename node:internal/modules/cjs/loader:995:15

I tried marking all the folders as external, but that does nothing.

There is also this message:

✘ [ERROR] Could not resolve "does-not-exist"

    tests/fixtures/node-module-missing/function.js:1:25:
      1 │ module.exports = require('does-not-exist')
        ╵                          ~~~~~~~~~~~~~~~~

  You can mark the path "does-not-exist" as external to exclude it from the bundle, which will
  remove this error. You can also surround this "require" call with a try/catch block to handle this
  failure at run-time instead of bundle-time.

which does not fail any test, but I also cannot get rid of, even if I set the importing file as external.

Reproduction

Minimal repro: https://github.com/danez-labs/vitest-import-unhandled

  • npm i
  • npm test

Or the original problem:

https://github.com/netlify/zip-it-and-ship-it/pull/1286

  • npm i
  • npm exec vitest -- run -t 'Throws on missing dependencies '

System Info

System:
    OS: macOS 12.6.1
    CPU: (10) arm64 Apple M1 Pro
    Memory: 2.90 GB / 32.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 18.12.1 - ~/.asdf/installs/nodejs/lts/bin/node
    Yarn: 1.22.19 - /opt/homebrew/bin/yarn
    npm: 8.19.2 - ~/.asdf/plugins/nodejs/shims/npm
  Browsers:
    Safari: 16.1

Used Package Manager

npm

Validations

Issue Analytics

  • State:closed
  • Created 9 months ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
sheremet-vacommented, Dec 12, 2022

This is a bug in Vitest, the fix is not released yet.

0reactions
danezcommented, Dec 12, 2022

Yes.

Seems the error is coming directly from esbuild. Funny enough the transform API from esbuild (which is used in vitest/vite/rollup, not sure where exactly) has no external option. 🤣 So I guess I’m back to ignoring this message 😃

Error: Transform failed with 1 error:
/Users/danieltschinder/Projects/netlify/zip-it-and-ship-it/node_modules/esbuild/lib/main.js:247:12: ERROR: Invalid option in transform() call: "external"
Read more comments on GitHub >

github_iconTop Results From Across the Web

What is an Unhandled Exception, and How to Catch All C# ...
An exception is a known type of error. An unhandled exception occurs when the application code does not properly handle exceptions.
Read more >
c# - How can I make something that catches all 'unhandled ...
Take a look at the example from the ThreadException documentation: public static void Main(string[] args) { // Add the event handler for handling...
Read more >
Processing Unhandled Exceptions (C#) - Microsoft Learn
To accomplish these tasks we first need to determine the details of the exception that was raised. Use the Server object's GetLastError method ......
Read more >
Defining Unhandled Exceptions and Catching All C# Exceptions
An exception is a known type of error. An unhandled exception occurs when the application code does not properly handle exceptions.
Read more >
How to resolve unhandled exceptions in Node.js
Approach 1: Using try-catch block: We know that Node. · Example: · Note: However, be careful not to use try-catch in asynchronous code,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found