harlan-zw / nuxt-seo

The complete SEO solution for Nuxt.
https://nuxtseo.com
MIT License
1.08k stars 70 forks source link

defineOgImageComponent is undefined in component testing with @nuxt/test-utils #265

Closed adamdehaven closed 3 months ago

adamdehaven commented 4 months ago

Describe the bug

When mounting a component with mountSuspended or renderSuspended via @nuxt/test-utils, when the exports are compiled for the tests, defineOgImageComponent (and defineOgImage, etc.) composable is not available for import and throws an error:

ReferenceError: defineOgImageComponent is not defined

When running nuxt prepare manually, the exports exist; however, when testing with @nuxt/test-utils the composables do not seem to be available/resolve correctly.

Reproduction

No response

System / Nuxt Info

- Operating System: Darwin
- Node Version:     v20.14.0
- Nuxt Version:     3.12.3
- CLI Version:      3.12.0
- Nitro Version:    2.9.7
- Package Manager:  pnpm@9.3.0
- Builder:          -
- User Config:      -
- Runtime Modules:  -
- Build Modules:    -
adamdehaven commented 4 months ago

I can get around the error by adding ogImage: { enabled: false } to my vitest overrides; however, I'm curious if this is the best solution

harlan-zw commented 4 months ago

This would be an upstream issue in https://github.com/nuxt/test-utils, not sure there's anything I can do within the module to help.

The workaround your using looks good.

adamdehaven commented 4 months ago

I'm wondering if there's an underlying issue in the test-utils repo whereby the equivalent of the prepare script is either different, or doesn't accurately generate all necessary types for installed modules 🤔

Unsure of where to look over there

harlan-zw commented 3 months ago

I'll need to close this for now because there aren't any more next steps I can take.

The workaround is good because nuxt-og-image will automatically mock composable functions when the module is disabled so it's actually working perfectly.

Ideally @nuxt/test-utils has a solution for this in the future though.

adamdehaven commented 3 months ago

Yep the composables are mocked so no errors. Only suggestion would be a note in the docs somewhere

kieron commented 2 days ago

Any idea what to do if im getting this but not using vitest or @nuxt/test-utils?