Does Ruby on Rails Work With Jest?
Jest works well with Rails for testing JavaScript/frontend code, but requires explicit setup since Rails' default test stack is Minitest; they operate in separate testing domains.
Quick Facts
How Ruby on Rails Works With Jest
Ruby on Rails and Jest serve different purposes in the same application. Rails handles backend testing with Minitest by default, while Jest is the JavaScript testing framework for frontend code—especially valuable in modern Rails apps using Webpacker, esbuild, or import maps with JavaScript modules. They coexist peacefully in a Rails project because Jest tests run against your compiled JavaScript independently of the Rails test suite. Setup involves adding Jest to your package.json and configuring it to understand Rails' module resolution (like absolute imports from app/javascript). Most modern Rails projects using Jest pair it with React or Vue components in the frontend, keeping Jest tests isolated from Rails' backend test suite. The developer experience is smooth: run `npm test` for JavaScript tests and `rails test` for Ruby tests in separate processes. The main architectural consideration is ensuring your Rails API properly exposes endpoints that Jest tests can mock or stub, and maintaining clear separation between frontend and backend test concerns.
Best Use Cases
Quick Setup
npm install --save-dev jest @testing-library/react @testing-library/jest-dom babel-jest @babel/preset-react// jest.config.js
module.exports = {
testEnvironment: 'jsdom',
setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
moduleNameMapper: {
'\\.(css|scss)$': '<rootDir>/__mocks__/styleMock.js',
'^app/(.*)$': '<rootDir>/app/javascript/$1',
},
testMatch: ['**/__tests__/**/*.(test|spec).js'],
transform: {
'^.+\\.jsx?$': 'babel-jest',
},
};
// app/javascript/__tests__/Button.test.js
import { render, screen } from '@testing-library/react';
import Button from '../components/Button';
test('renders button with text', () => {
render(<Button label="Click me" />);
expect(screen.getByText('Click me')).toBeInTheDocument();
});Known Issues & Gotchas
Jest doesn't understand Rails asset paths by default (image/stylesheet requires fail)
Fix: Configure Jest moduleNameMapper to mock asset imports: { '\.(css|scss)$': '<rootDir>/__mocks__/styleMock.js' }
Circular dependencies between Rails view helpers and JavaScript can cause issues
Fix: Keep JavaScript tests focused on frontend logic; mock Rails API endpoints rather than importing backend code
Rails' Webpacker/esbuild JavaScript entrypoints may conflict with Jest's module resolution
Fix: Use jest.config.js to explicitly set moduleDirectories and testMatch patterns to avoid Rails bundler confusion
Alternatives
- •Rails + Vitest: Modern alternative using Vitest instead of Jest for faster testing with similar API
- •Rails + Mocha + Chai: Traditional Node.js testing stack with more flexibility but less convention
- •Rails only with Minitest: Skip Jest entirely and test JavaScript via Minitest integration tests
Resources
Related Compatibility Guides
Explore more compatibility guides