Skip to content
This repository has been archived by the owner on Dec 19, 2023. It is now read-only.

feat: register all auto-imports for mocking #254

Merged
merged 2 commits into from
Jul 17, 2023

Conversation

aapokiiso
Copy link
Contributor

@aapokiiso aapokiiso commented Jul 11, 2023

It is not possible to mock auto-imports if they are registed with the imports:extend hook. This is because nuxt-vitest also uses this hook to register imports for mocking, but because there is no guaranteed call order of the hook handlers, some auto-import registrations happen after nuxt-vitest's hook handler.

This PR moves import registration to wait until Nuxt is ready, making all auto-imports available for mocking.

There are some hints in the current implementation towards this being unwanted behaviour, namely the nuxtImportSources whitelist introduced in 8806143.

I wanted to open this PR anyway to get feedback if there is some inherent flaw in making all auto-imports available for mocking like this.

@socket-security
Copy link

socket-security bot commented Jul 11, 2023

No dependency changes detected. Learn more about Socket for GitHub ↗︎

👍 No dependency changes detected in pull request

@danielroe
Copy link
Owner

danielroe commented Jul 11, 2023

I think it would be good, if possible, for all auto imports to be mockable. (wdyt@antfu?)

Linking #94 for previous work in this area.

Note that I would prefer a local module that registers auto imports rather than adding an existing, more complex module, to the dependencies.

Also note that we are currently blocked by newer versions of vitest for mocking anything: see vitest-dev/vitest#3308 which may resolve things.

@aapokiiso aapokiiso force-pushed the register-all-imports branch 2 times, most recently from a2790ad to 744d482 Compare July 11, 2023 12:10
@aapokiiso
Copy link
Contributor Author

aapokiiso commented Jul 11, 2023

@danielroe I looked into this a bit further, and the issue is not external packages themselves, but rather specifically @nuxtjs/apollo using Nuxt Kit's addImports utility, which hooks into imports:extend under the hood. This seems to cause a race condition, since nuxt-vitest also uses the imports:extend hook to gather auto-imports for mocking.

I modified the commit to remove the external dependency and to test this via the local module.

Edit: Updated also the PR description to match this.

It is not possible to mock auto-imports if they are registed with the
`imports:extend` hook. This is because nuxt-vitest also uses this hook to
register imports for mocking, but because there is no guaranteed call order of
the hook handlers, some auto-import registrations happen after nuxt-vitest's
hook handler.

This PR moves import registration to wait until Nuxt is ready, making all
auto-imports available for mocking.

There are some hints in the current implementation towards this being unwanted
behaviour, namely the `nuxtImportSources` whitelist introduced in 8806143.

I wanted to open this PR anyway to get feedback if there is some inherent
flaw in making all auto-imports available for mocking like this.
@danielroe danielroe merged commit d648e6a into danielroe:main Jul 17, 2023
@aapokiiso aapokiiso deleted the register-all-imports branch July 17, 2023 13:41
danielroe pushed a commit that referenced this pull request Jul 17, 2023
Co-authored-by: Ghazi <ghazialhouwari@gmail.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants