Improvements to mountQuasar
See original GitHub issueAfter starting with jest + Quasar I have a couple of proposals which I think will greatly improve the usability of the supplied mountQuasar
utility.
Support using either mount
or shallowMount
This could easily be implemented by adding an option to the options
object (which is already there) and switching on that. The default can stay shallowMount
so it’s backwards compatible.
Add stubs
to the options
array and pass them to mount
Together with the possibility to switch the used mount function this gives a lot of flexibility, for instance making it possible to mount
fully and stubbing out selectively.
Add an option to set the resolution
This allow testing responsiveness, for instance components that should render different content based on $q.screen... I have this working in my project like this:
const resizeWindow = ({ x, y }) => {
global.innerWidth = x
global.innerHeight = y
global.dispatchEvent(new Event('resize'))
}
This has to run before createLocalVue() is called (I think). This too can be part of the options
object.
Make it possible to mount a component without creating a new localVue
This makes it easy to do multiple mounts in on test file, which is useful when you want to test props that have an effect in created()
(that’s one usecase, I’m sure there are more).
In my current project I solved this by refactoring out the code that calls createLocalVue
and having the test call that separately, but I’m sure there’s more elegant solutions.
Issue Analytics
- State:
- Created 3 years ago
- Comments:24 (21 by maintainers)
For example, VueRouter (like some other plugins) auto-register itself when included in the codebase if it finds a global Vue reference so you can avoid the
Vue.use(VueRouter)
in some scenarios, but with Vue3 that will not be possible anymore breaking the codebase of who relied on that mechanism.Takeoff: just because you currently can, it doesn’t mean you should, nor that it will keep working in the future.
That said, thank you for your feedback and for taking the time to test this out 😃 Unless more feedback comes up, I plan to release the stable version in next weeks as v2, as there has been some breaking changes
I looked further into what
mountQuasar()
andmountFactory()
were doing and I found that ifmount.localVue
is declared, thenplugins
gets discarded:Since it’s either/or and not both I tried
VueRouter
as a plugin and it worked:This even simplifies things a bit since I was able to remove everything related to
localVue
letting it be instantiated by theplugins
arrayI’m not sure if this will have issues in other scenarios, but I tested on a more complex component and this solution worked there as well. I also confirmed I had no issues with multiple
mountFactory()
in the same test suite.If this example reflects the intended usage; The docs could be updated to describe this structure for “Use with VueRouter” ~and “Use with Vuex”~ (see correction below) A “Use with localVue” section could be added in “Caveats” describing
localVue.use()
implementation mentioning thatmount.localVue
overridesplugins
.[Vuex] Correction:
Since it seemed like
mount.localVue
andplugins
should be able to co-exist I tested with modified beta to the effect of:and confirmed this allows the use of both implementations simultaneously. You may have avoided this intentionally for some scenario I’m not aware of, but I wanted to point out it seems to be generally ok.