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

fix: add async to onFinish report event #623

Conversation

edimitchel
Copy link
Member

@edimitchel edimitchel commented Jan 24, 2022

Error when using UI with Vitest

(node:50984) UnhandledPromiseRejectionWarning: Error: listen EACCES: permission denied 127.0.0.1:51204
at Server.setupListenHandle [as _listen2] (net.js:1301:21)
at listenInCluster (net.js:1366:12)
at doListen (net.js:1503:7)
at processTicksAndRejections (internal/process/task_queues.js:81:21)
(Use node --trace-warnings ... to show where the warning was created)
(node:50984) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:50984) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future,
promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

@netlify
Copy link

netlify bot commented Jan 24, 2022

✔️ Deploy Preview for vitest-dev ready!

🔨 Explore the source changes: 34071b2

🔍 Inspect the deploy log: https://app.netlify.com/sites/vitest-dev/deploys/61ef39456cebba000791774c

😎 Browse the preview: https://deploy-preview-623--vitest-dev.netlify.app

@edimitchel edimitchel force-pushed the fix/unhandled-promises-on-end-tests branch from 975c8e7 to 1ca66fe Compare January 24, 2022 19:37
@Demivan
Copy link
Member

Demivan commented Jan 24, 2022

Adding async to a function that does not have await should not affect it. What does this do?

@edimitchel
Copy link
Member Author

async behind a function transform the function to a awaitable.

The function report which make sure to send report event needs function which return promise.
See https://github.com/vitest-dev/vitest/blob/main/packages/vitest/src/node/core.ts#L378-L381

@Demivan
Copy link
Member

Demivan commented Jan 24, 2022

Ok, makes sense that this will return a promise now.
Should this be extended to onCollected and onUserConsoleLog? Those are marked as awaitable in reporter interface too.

@prazdevs
Copy link

Hi, tested it on my repo that was crashing on --ui it works perfectly fine now and reruns as expected ! 👍

@mitchelvanbever
Copy link
Contributor

Hi, tested it on my repo that was crashing on --ui it works perfectly fine now and reruns as expected ! 👍

Same here 👌

@edimitchel
Copy link
Member Author

Ok, makes sense that this will return a promise now.
Should this be extended to onCollected and onUserConsoleLog? Those are marked as awaitable in reporter interface too.

Good comment, I think we can remove the Awaitable type.

Copy link
Member

@sheremet-va sheremet-va left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to make them all Awaitable to support async implementation in reporters. Also shouldn't they await Promise.all and not just turn into an async function?

async function onFinished(files?: File[] | undefined) {
  await Promise.all(
    this.clients.map((client) => {
      return client.onFinished?.(files)
    })
  )
}

@antfu
Copy link
Member

antfu commented Jan 25, 2022

Actually, events prefixed with on should not expect returns (error on receiver side does not matter to the sender), add it here

eventNames: ['onCollected', 'onUserConsoleLog', 'onTaskUpdate'],

Should work

@bjarkihall
Copy link

Thanks for the PR!
For those trying to find an issue/PR a text would work better than a screenshot when searching, so I'll just paste it here:

(node:50984) UnhandledPromiseRejectionWarning: Error: listen EACCES: permission denied 127.0.0.1:51204
    at Server.setupListenHandle [as _listen2] (net.js:1301:21)
    at listenInCluster (net.js:1366:12)
    at doListen (net.js:1503:7)
    at processTicksAndRejections (internal/process/task_queues.js:81:21)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:50984) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:50984) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, 
promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

antfu added a commit that referenced this pull request Jan 25, 2022
@antfu
Copy link
Member

antfu commented Jan 25, 2022

I have made the fix in 6b02dea and released it as v0.2.3. Please check if it fixes the error

@bjarkihall
Copy link

bjarkihall commented Jan 25, 2022

@antfu I've tried v.0.2.3 on a clean install (using npm) but still get the same error message when running npx vitest --ui
npx vitest itself still works without any problems.
This is likely the same issue as the one reported in #627

The issue provides reproduction steps but it also has a screenshot of where the error is thrown - a Promise.reject is invoked but there's no try/catch for it in when using async/await, could it be that there's no error boundary in the callstack to catch it, so node stops the execution?

@mitchelvanbever
Copy link
Contributor

I have made the fix in 6b02dea and released it as v0.2.3. Please check if it fixes the error

I've now tested two local repo's that had the issue prior to V0.2.3 but after upgrading I'm no longer seeing the messages and/or crashes happening (with and without ui flag).

Stackblitz seems to not complain anymore as well.

@bjarkihall
Copy link

@mitchelvanbever on a second look at the original image from @edimitchel, the error messages are indeed not the same, although they both show a similar warning + stacktrace (both caused by unhandled promise rejections).

This one is related to custom reporters.
I'll follow up on this in the issue I linked to --ui (#627).

Any ideas how unhandled promise rejections could be further prevented or at least caught, for the sake of a clearer stacktrace?

@edimitchel
Copy link
Member Author

@antfu what should we do ?

@sheremet-va
Copy link
Member

@antfu what should we do ?

I think it should be fixed now. Can you confirm? If so, we may close the PR

@edimitchel edimitchel closed this Feb 9, 2022
@edimitchel edimitchel deleted the fix/unhandled-promises-on-end-tests branch March 15, 2022 22:49
chaii3 pushed a commit to chaii3/vitest that referenced this pull request May 13, 2022
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.

None yet

7 participants