How to properly handle "unhandled" exceptions
See original GitHub issueDescribe 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 inpm test
Or the original problem:
https://github.com/netlify/zip-it-and-ship-it/pull/1286
npm inpm 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
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn’t already an issue that reports the same bug to avoid creating a duplicate.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
Issue Analytics
- State:
- Created 9 months ago
- Comments:5 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This is a bug in Vitest, the fix is not released yet.
Seems the error is coming directly from
esbuild. Funny enough the transform API fromesbuild(which is used in vitest/vite/rollup, not sure where exactly) has noexternaloption. 🤣 So I guess I’m back to ignoring this message 😃