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

How to properly handle "unhandled" exceptions #2493

Closed
6 tasks done
danez opened this issue Dec 12, 2022 · 5 comments · Fixed by #2494
Closed
6 tasks done

How to properly handle "unhandled" exceptions #2493

danez opened this issue Dec 12, 2022 · 5 comments · Fixed by #2494

Comments

@danez
Copy link
Contributor

danez commented Dec 12, 2022

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:

netlify/zip-it-and-ship-it#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

@sheremet-va
Copy link
Member

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

@sheremet-va
Copy link
Member

There is also this message

You can ignore it by returning false from onConsoleLog in test config.

@danez
Copy link
Contributor Author

danez commented Dec 12, 2022

There is also this message

You can ignore it by returning false from onConsoleLog in test config.

That does not work, it does not look like these errors go through the user log. Is this error coming from vite? I cannot find anything in vitest regarding this error message.

@sheremet-va
Copy link
Member

Is this error coming from vite?

Yes.

@danez
Copy link
Contributor Author

danez commented 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"

@github-actions github-actions bot locked and limited conversation to collaborators Jun 9, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants