esbuild-jest2

raw JSON →
0.6.7 verified Mon Apr 27 auth: no javascript

A Jest transformer that uses esbuild for fast compilation of TypeScript, JavaScript, JSX, and TSX files. Version 0.6.7 is the latest stable release, maintained as a fork of the abandoned esbuild-jest with active updates for Jest v29+. It integrates esbuild's blazing-fast bundler into the Jest transform pipeline, supporting custom loaders, sourcemaps, JSX factory/fragment configuration, and target/format options. Unlike ts-jest, which relies on tsc, esbuild-jest2 offers significantly faster transformation but does not enforce type checking. It requires esbuild >=0.20.1 as a peer dependency and ships TypeScript declarations.

error Cannot find module 'esbuild-jest2' from 'transform.js'
cause Package not installed or incorrectly spelled in Jest config.
fix
Run npm install --save-dev esbuild-jest2 and ensure Jest config uses 'esbuild-jest2' (not 'esbuild-jest').
error TypeError: (0 , esbuild_jest2.createTransformer) is not a function
cause Using default import instead of named import for createTransformer.
fix
Change import to import { createTransformer } from 'esbuild-jest2' or const { createTransformer } = require('esbuild-jest2').
error Error: esbuild: Expected ";" but found "<"
cause JSX syntax encountered but esbuild loader not set correctly for .tsx or .jsx files.
fix
Set transform regex to include .tsx and .jsx (e.g., '^.+\\.(ts|tsx|jsx)$') or ensure loader config includes 'tsx' for appropriate files.
gotcha esbuild-jest2 does not perform type checking; test may pass despite TypeScript errors.
fix Run a separate type-check step (e.g., `tsc --noEmit`) before or in parallel with tests.
gotcha The package name is 'esbuild-jest2', not 'esbuild-jest' - using wrong name will cause Jest to fail to find transformer.
fix Use correct string 'esbuild-jest2' in Jest transform configuration.
breaking Peer dependency esbuild >=0.20.1 required; older esbuild versions may cause runtime errors or missing APIs.
fix Update esbuild to >=0.20.1: `npm install esbuild@latest`
deprecated The original esbuild-jest package is abandoned; esbuild-jest2 is the actively maintained fork.
fix Migrate from esbuild-jest to esbuild-jest2 by changing package name and requiring esbuild >=0.20.1.
gotcha TypeScript type imports (Options) must use `import type` to avoid runtime errors.
fix Use `import type { Options } from 'esbuild-jest2'`
npm install esbuild-jest2
yarn add esbuild-jest2
pnpm add esbuild-jest2

Configures Jest to transform .ts/.tsx files using esbuild-jest2 with sourcemaps, target es2020, and custom JSX settings.

// jest.config.js
module.exports = {
  transform: {
    '^.+\\.tsx?$': [
      'esbuild-jest2',
      {
        sourcemap: true,
        target: 'es2020',
        jsxFactory: 'h',
        jsxFragment: 'Fragment',
        loaders: {
          '.spec.ts': 'tsx'
        }
      }
    ]
  }
};

// example.spec.ts
test('esbuild transform', () => {
  const x: number = 1;
  expect(x + 1).toBe(2);
});