test-utils
test-utils copied to clipboard
Components tests it is triggering global route middleware
When attempting to render a component for a whitebox test, the entire Nuxt environment is being loaded instead of just the component. This behavior is likely caused by the presence of an authentication guard in the project, which leads to redirection to the sign-in page during test execution.
Steps to Reproduce
Access the following StackBlitz link: Reproduction Test
Expected Behavior
The test should only render the targeted component without triggering global route middleware or redirections.
Actual Behavior
Upon running the test, the entire Nuxt environment is loaded, including global route middleware, leading to redirection to the sign-in page due to the presence of the authentication guard.
Additional Information
Nuxt project info:
- Operating System: Linux
- Node Version: v18.16.0
- Nuxt Version: 3.5.3
- Nitro Version: 2.4.1
- Package Manager: [email protected]
- Builder: vite
- User Config: alias, app, build, sourcemap, ssr, runtimeConfig, vite, css, modules, components, ignore, dayjs, typescript, experimental, devtools
- Runtime Modules: [email protected], [email protected], @pinia/nuxt@^0.4.11
- Build Modules: -
I am facing the same issue, check this for workaround nuxt/test-utils#565
~~@sky-code That workaround doesn't help with a global middleware being triggered.~~
@vsanrocha With some modification to @sky-code's workaround, yeah, it looks like like you could clear out the middleware if that's your desired outcome:
export default defineNuxtConfig({
...
hooks: {
'app:resolve': async app => {
const process = await import('node:process');
if (String(process.env?.TEST) === 'true') {
app.middleware = [];
}
},
},
});
That doesn't seem desirable for e2e testing, though.