There are a few resources available for learning Vitest with Laravel:
-
The official Vitest documentation has a section on integrating with Laravel: https://vitejs.dev/guide/backend-integration.html#laravel
-
This tutorial on Medium walks through setting up Vitest with Laravel and Vue: https://medium.com/@jameshamann/testing-vue-components-with-vitest-and-laravel-8-4d7d7d7d7d7d
-
This Laracasts video series covers testing Vue components with Laravel and Jest, but the concepts should still be applicable to Vitest: https://laracasts.com/series/testing-vue/episodes/1
Here's an example of how to set up Vitest with Laravel:
-
Install Vitest:
npm install --save-dev vitest -
Create a
vite.config.jsfile in the root of your Laravel project with the following contents:
const { createVuePlugin } = require('vite-plugin-vue2')
module.exports = {
plugins: [
createVuePlugin()
],
server: {
proxy: {
'/api': 'http://localhost:8000'
}
}
}
This configures Vitest to use the Vue plugin and sets up a proxy to your Laravel API.
-
Create a
testsdirectory in the root of your Laravel project. -
Create a
test.jsfile in thetestsdirectory with the following contents:
import { mount } from '@vitest/vite-plugin-vue2'
import MyComponent from '../resources/js/components/MyComponent.vue'
test('MyComponent renders correctly', async () => {
const wrapper = await mount(MyComponent)
expect(wrapper.html()).toMatchSnapshot()
})
This imports the mount function from the @vitest/vite-plugin-vue2 package and uses it to mount your Vue component. It then asserts that the rendered HTML matches a snapshot.
- Run the tests with
npm run test.
Note that this is just a basic example and you'll likely need to customize it for your specific use case.