-
-
Notifications
You must be signed in to change notification settings - Fork 41
feat: register all auto-imports for mocking #254
Conversation
No dependency changes detected. Learn more about Socket for GitHub ↗︎ 👍 No dependency changes detected in pull request |
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. |
a2790ad
to
744d482
Compare
@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 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. |
744d482
to
508f3a7
Compare
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.
508f3a7
to
604baf5
Compare
Co-authored-by: Ghazi <ghazialhouwari@gmail.com>
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.