-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Tests pass but vitest fails non-deterministically and sometimes causes Unhandled Rejection #1692
Comments
This should be caused by some promises not catching errors. For example, the |
May i ask there is any possible solution? same issue here :( |
If you have a vitest setup file via
This helped me to find the specific file that I was failing to await a Promise that needed to be handled (which was difficult due to hundreds of files/tests). |
@sheremet-va could you tell if the issue fixed relates to my scenario? Facing the same problem right now and I'm using version 0.24.5. |
Yes, mentioned in this PR error is fixed. You probably have an asynchronous process that's executed after all tests have passed. Vitest cannot track it, it's unhandled. |
I see, thanks for getting back. |
@sheremet-va I'm facing smae issue with unhandled error. What's the strategies to catch it and find a source? |
UPDATE: Seems to have workedOne of the errors seemed to be related to LogRocket. The error was So I ended up refactoring my LogRocket import like this: async function importLogRocket(): Promise<any> {
if (process.env.VITEST === 'true') return Promise.resolve(null);
return await import('logrocket');
}
let LogRocket: any = null;
if (process.env.VITEST !== 'true') {
console.debug('LogRocket init');
importLogRocket().then((imported_logrocket) => {
LogRocket = imported_logrocket.default;
LogRocket.init(LR_ACCOUNT_ID);
});
} I'm sure I can refactor further, and probably get rid of the explicit Didn't work on its ownI followed the steps above in #1692 (comment) My vitest has a config file, vitest.config.ts in the root of my project. The config contains this (truncated example, I removed // vitest.config.ts
export default defineConfig({
test: {
setupFiles: ['test/vitest/setup-file.ts'],
}
}); The setup file now has the snippet mentioned above: // test/vitest/setup-file.ts
// FAIL LOUDLY on unhandled promise rejections / errors
process.on('unhandledRejection', (reason) => {
// eslint-disable-next-line no-console
console.log('FAILED TO HANDLE PROMISE REJECTION');
throw reason;
});
export default {}; (Note the All I can say is, while I don't yet understand why, |
@sheremet-va If I do have some async process running in a test suite and it passes only when all the other tests have passes, else throws this unhandled error as mentioned #1692 (comment) what might be the best way to handle it? @thoriumdesign did your solution work? did it stop throwing errors non-determinstically ? |
Describe the bug
While test cases results themselves are deterministic but the runs are not. Some of the runs produce unhandled rejection. This seems to be similar to #1191 but not same. My test specs are fairly minimal at this time, like just checking for component rendering and not much.
Reproduction
You should be able to reproduce it using this repo.
https://github.com/zinclabs/zinc/tree/38303f59af5f329c62068a320eecd4f53c17b107
steps.
run the test a couple of times. You will notice that sometimes tests are successful however sometimes there is unhandled rejection.
System Info
Used Package Manager
npm
Validations
The text was updated successfully, but these errors were encountered: