test-utils icon indicating copy to clipboard operation
test-utils copied to clipboard

Components tests it is triggering global route middleware

Open vsanrocha opened this issue 2 years ago • 4 comments

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: -

vsanrocha avatar Jul 27 '23 20:07 vsanrocha

I am facing the same issue, check this for workaround nuxt/test-utils#565

sky-code avatar Aug 04 '23 23:08 sky-code

~~@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.

niko-chaffinchicas avatar Sep 15 '23 00:09 niko-chaffinchicas