vite-jest
raw JSON → 0.1.4 verified Mon Apr 27 auth: no javascript
A Jest transformer for seamless Vite integration in testing, version 0.1.4. Provides Vite's transform pipeline for Jest, allowing use of Vite plugins and TypeScript without separate config. Lightweight alternative to ts-jest when using Vite. Active development but experimental.
Common errors
error Cannot find module 'vite-jest' from 'jest.config.js' ↓
cause vite-jest not installed or not in node_modules.
fix
Run
npm install -D vite-jest error TypeError: Invalid value: Vite config must be a sync function or an object. ↓
cause Passing an async Vite config to vite-jest; vite-jest expects sync config.
fix
Make vite.config.ts export a sync object or a sync function.
error vite-jest: rootDir option must be specified in Jest config transform block. ↓
cause vite-jest cannot determine the project root automatically in some setups.
fix
Add
rootDir: __dirname to the transform options in jest.config.js. Warnings
breaking vite-jest 0.1.x requires Jest 27+ and Vite 2.4.2+. Using older versions causes undefined transform errors. ↓
fix Upgrade to vite-jest@0.1.4, Jest 27, Vite 2.4.2+.
deprecated vite-jest is experimental and may break with Vite or Jest minor updates. ↓
fix Consider using vitest for production testing.
gotcha Using CommonJS require() to import vite-jest throws 'require is not defined' or similar ESM errors. ↓
fix Use ESM import syntax. If Jest config is in CommonJS, use dynamic import: `module.exports = { transform: { '^.+\.ts$': ['vite-jest', { rootDir: __dirname }] } }`
gotcha When using Vite plugins that rely on server context (e.g., @vitejs/plugin-legacy), tests may fail because there is no dev server. ↓
fix Avoid plugins that require server context in tests, or mock them.
Install
npm install vite-jest yarn add vite-jest pnpm add vite-jest Imports
- viteJest wrong
const viteJest = require('vite-jest')correctimport viteJest from 'vite-jest' - createTransformer
import { createTransformer } from 'vite-jest' - ProjectConfig wrong
import { ProjectConfig } from 'vite-jest'correctimport type { ProjectConfig } from 'vite-jest'
Quickstart
// jest.config.js
module.exports = {
testEnvironment: 'node',
transform: {
'^.+\.[jt]sx?$': ['vite-jest', { rootDir: __dirname }]
}
};
// sum.ts
export const sum = (a: number, b: number) => a + b;
// sum.test.ts
import { sum } from './sum';
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});