Does Laravel Work With Jest?
Jest works well with Laravel's frontend JavaScript/TypeScript code, but requires careful setup since Laravel is a backend framework and Jest is a frontend testing tool.
Quick Facts
How Laravel Works With Jest
Laravel and Jest operate in different layers of your application. Jest excels at testing JavaScript/TypeScript that runs in your Laravel frontend—Vue components, React apps, utility functions, or any Node.js code. Jest is not designed to test PHP code; use PHPUnit for that instead. The integration is straightforward: install Jest via npm in your Laravel project, configure it to work with your asset bundler (Vite in Laravel 9+, or webpack in earlier versions), and test your frontend assets. Laravel's `resources/js` directory becomes your Jest testing ground. Many Laravel teams use Jest alongside PHPUnit, testing backend logic with PHPUnit and frontend logic with Jest. The main consideration is keeping your test configurations separate and understanding that Jest runs in Node.js while Laravel runs on PHP—they're complementary, not unified.
Best Use Cases
Jest Setup for Laravel with Vite
npm install --save-dev jest @vue/test-utils vue@3 babel-jest @babel/preset-env @babel/preset-typescript// jest.config.js
module.exports = {
preset: '@vue/cli-plugin-unit-jest/presets/typescript-and-babel',
testEnvironment: 'jsdom',
moduleNameMapper: {
'^@/(.*)$': '<rootDir>/resources/js/$1',
},
transform: {
'^.+\.vue$': '@vue/vue3-jest',
'.+\.(css|styl|less|sass|scss|svg|png|jpg|ttf|woff|woff2)$': 'jest-transform-stub',
'^.+\.tsx?$': 'ts-jest',
},
};
// resources/js/__tests__/Example.spec.ts
import { describe, it, expect } from '@jest/globals';
import { mount } from '@vue/test-utils';
import ExampleComponent from '@/components/ExampleComponent.vue';
describe('ExampleComponent', () => {
it('renders properly', () => {
const wrapper = mount(ExampleComponent);
expect(wrapper.text()).toContain('Hello');
});
});Known Issues & Gotchas
Jest and Vite have module resolution differences; imports may fail without proper configuration
Fix: Use the `moduleNameMapper` in jest.config.js to alias your imports to match Vite's path resolution (e.g., `@/` for resources/js)
Testing Laravel API responses requires mocking HTTP requests; Jest has no built-in Laravel integration
Fix: Use `jest.mock()` or libraries like `msw` (Mock Service Worker) to intercept and mock API calls from your frontend code
Environment variables from .env may not load in Jest tests automatically
Fix: Create a `jest.setup.js` file that manually loads environment variables using `dotenv` before tests run
Absolute imports (`@/components`) work in Laravel but need Jest configuration to resolve correctly
Fix: Configure moduleNameMapper in jest.config.js to map `@/*` to `<rootDir>/resources/js/*`
Alternatives
- •Vitest + Laravel: Modern alternative with faster feedback, better Vite integration, and lower setup friction
- •PHPUnit + Pest: For full backend testing; combines unit, feature, and integration tests in PHP
- •Playwright + Laravel: End-to-end testing that tests the entire Laravel app through a browser
Resources
Related Compatibility Guides
Explore more compatibility guides