Repository navigation
fix: stub directives registered with a camelCase or kebab-case name - #2982
Merged
cexbrayat merged 1 commit intoOct 28, 2026
Merged
Conversation
`stubs: { vMyDirective: ... }` only replaced a directive registered as
`MyDirective`. A component registering it as `myDirective` or
`my-directive`, which Vue resolves for `v-my-directive` just the same,
kept running the real directive. Stub keys must be `v` followed by an
uppercase letter to be treated as directives, so no stub key could
match those registrations.
The directive transformer looked the registered name up verbatim among
the stub names. Match it with `matchName` instead, as component stubs
already do, and keep replacing the directive under its registered key.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
✅ Deploy Preview for vue-test-utils-docs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
commit: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
global.stubsonly replaces a directive when the component registers it under exactly the name in the stub key minus thev. If the directive is registered asmyDirectiveormy-directive,vMyDirectiveis ignored and the real directive keeps running, even though Vue resolvesv-my-directiveto those registrations too:Lowercase keys are the common Options API style. The Vue docs register their example as
directives: { highlight }forv-highlight, whichvHighlightcannot stub today. A stub key is only treated as a directive when it isvfollowed by an uppercase letter, so no stub key can match such a registration.Root cause
createStubDirectivesTransformer(src/vnodeTransformers/stubDirectivesTransformer.ts) picks the directives to replace withObject.keys(type.directives).filter(key => key in directives).getDirectivesFromStubsstrips the leadingvfrom the stub key (vMyDirectivebecomesMyDirective), so only a registration named exactlyMyDirectivematches. Vue'sresolveAssetlooks a template directive up by its name, the camelized name and the capitalized name, somyDirectiveandmy-directiveare equally valid registrations forv-my-directive.Fix
Compare the registered name with the stub names using
matchName, the helper the component stubs already use. The directive is still replaced under the key it is registered with. PascalCase registrations behave as before, andvMyDirective: falsestill opts out.Validation
I added two regression tests to
tests/mountingOptions/global.stubs.spec.ts. One stubs a camelCase registration with a stub directive. The other replaces a kebab-case registration with thetruenoop.I could not install the project's dependencies in my environment, so I have not run vitest, lint, format-check or the type checks locally. Instead I loaded Vue 3.5.44's
runtime-coreandcompiler-domfrom source, using the DOM-free@vue/runtime-testrenderer. I then ran this repository's stub transformers on the same templates as thedirectivestests. Onmain, both new cases render the real directive's class. With this change they render the stub, and the existing directive cases give the same output as before.🤖 Generated with Claude Code