Replies: 1 comment
-
First of all, vi.mock('path', async () => ({
...(await vi.importActual('path')
}) Also, mount(Component, {
global: {
plugins: [FloatingVue, Toast],
mixins: [{ methods: { route: vi.fn() } }]
}
}) You can also define it in import { config } from '@vue/test-utils'
config.global.plugins = [FloatingVue, Toast]
config.global.mixins = [{ methods: { route: vi.fn() } }] I am not sure about config.global.mocks = {
route: vi.fn()
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
To preface, I am very new to writing client-side tests... There doesn't seem to be a lot of discussions/topics/documentation on using/setting up vitest with inertia.js - so I am having some trouble getting it running without errors.
I have setup my project with Laravel + Inertia.js + Vite + Vitest - and everything works fine in terms of compiling with vite - however I am having some trouble figuring out how to setup a basic vitest test for my
UserIndex.vue
page.I found a topic related to testing with jest + inertia.js that I based my initial test setup off of.
If you look at my test file
demo.test.ts
below, you can see that I have setup avi.mock
for the@inertiajs/inertia-vue3
package - this was necessary because without it, it would return an error when trying to render the inertia.js<Head title="test"/>
component - which is imported like so:import {Head, Link} from '@inertiajs/inertia-vue3'
in myUserIndex.vue
page. The same goes for using the inertia helpers like $inertia and $page to access page props and inertia methods for example.The error was:
TypeError: Cannot read properties of undefined (reading 'createProvider')
After, mocking the
@inertiajs/inertia-vue3
I was able to bypass that particular error, however I am now running into another error related to theroute
mixin from inertia that is defined in the app.js - since it missing in the test environment, I am getting the following error regarding_ctx.route is not a function
, and warnings about failing to resolve directivetooltip
from thefloating-vue
package. The route() method is used to generate links which comes from the default inertia ziggy routing mixin defined inapp.js
.I have found that when running the test using
shallowMount
instead ofmount
from@vue/test-utils
- it will bypass the errors, but not the warnings, and it will pass the truthy test, but I am unable to get the text from the wrapper likeexpect(wrapper.text()).toContain('User')
- this returns empty text and compares to 'User' which is false.So my question is, am I on the right track with this test, should I be testing the entire page this way? If so, what can I do to resolve the route and directive errors? Should I be mocking all of these packages/directives?
Any direction would be very much appreciated. Please let me know if you would like to see any other code/files.
Thanks!
_ctx.route error and warnings
demo.test.ts
vitest.config.ts. (my test config, which is used only when running vitest)
app.js
vite.config.ts
Beta Was this translation helpful? Give feedback.
All reactions