Open palmer-rm opened 4 days ago
@palmer-rm Hello there! I'm a bot here to help you with bugs, questions, and becoming a contributor. Let's squash those bugs together!
To resolve the issue where using the throw
keyword in a Vue component causes a unit test to timeout in a Nuxt 3 project, you should handle the error properly within the component. Instead of letting the error propagate and cause the test to timeout, you can catch the error and handle it gracefully. Here is an example of how you can modify the component to catch the error:
<template>
<div>
This should not display.
</div>
</template>
<script lang="ts">
export default defineComponent({
data () {
try {
throw new Error('💀')
} catch (error) {
console.error(error)
// Handle the error appropriately, e.g., set an error state
}
},
})
</script>
By catching the error within the data
method, you prevent the error from causing the unit test to timeout. Instead, the error is logged, and you can handle it as needed within the component.
Additionally, ensure you have set up your testing environment correctly using @nuxt/test-utils
and vitest
as described in the Nuxt documentation. This includes configuring your vitest.config.ts
to use the Nuxt environment:
import { defineVitestConfig } from '@nuxt/test-utils/config'
export default defineVitestConfig({
test: {
environment: 'nuxt',
// you can optionally set Nuxt-specific environment options
// environmentOptions: {
// nuxt: {
// rootDir: fileURLToPath(new URL('./playground', import.meta.url)),
// domEnvironment: 'happy-dom', // 'happy-dom' (default) or 'jsdom'
// overrides: {
// // other Nuxt config you want to pass
// }
// }
// }
}
})
This setup ensures that your tests run in a proper Nuxt environment, which can help prevent issues like timeouts due to unhandled errors [1][2][3].
To continue talking to Dosu, mention @dosu.
Environment
Darwin
v20.12.2
3.12.2
3.12.0
2.9.6
npm@10.5.0
-
devtools
,srcDir
,runtimeConfig
,modules
,typescript
,vite
@pinia/nuxt@0.5.1
,@nuxt/test-utils/module@3.13.1
,@nuxt/image@1.7.0
-
Reproduction
https://stackblitz.com/edit/github-jk1bg9?file=package.json,app.nuxt.test.ts
Command:
npm run test
Describe the bug
I am using Nuxt's createError to handle errors from within Vue components.
When using the
throw
keyword inside a component, a unit test that mounts that component fails due to a timeout.This happens regardless of whether the component throws Nuxt's
createError
,newError
, or anything else, e.g:Results in:
When mounting the component using
mount
in@vue/test-utils
(whichmountSuspended
wraps IIRC), usingthrow
in a component does not result in a failed test due to timeout.Using a
try catch
in the component still causes the test to timeout, i.e: