nuxt / test-utils

🧪 Test utilities for Nuxt
http://nuxt.com/docs/getting-started/testing
MIT License
311 stars 79 forks source link

Components tests it is triggering global route middleware #526

Open vsanrocha opened 1 year ago

vsanrocha commented 1 year ago

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:

sky-code commented 1 year ago

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

niko-chaffinchicas commented 1 year ago

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

pablo-vega-kloeckner-i commented 2 months ago

I am having the same issue. I want to know if there is an update on this issue. I also opened a discussion about generating a test file, in the examples folder, of how to test a middleware in Nuxt. https://github.com/nuxt/test-utils/discussions/873

awacode21 commented 7 hours ago

Experiencing the same problem! when doing component tests with vitest & nuxt they run through my applications middleware which should not be the case when i want to test the component in isolation