1. Install Dev Dependencies

npm install --save-dev typescript  @jest/globals @types/express ts-node jest ts-jest @types/jest supertest @types/supertest

npm install express
Package Purpose
typescript Allows you to write .ts TypeScript code.
ts-node Runs .ts files directly without compiling.
jest JavaScript testing framework.
ts-jest Makes Jest understand TypeScript.
@jest/globals Gives access to describe, it, expect in ESM.
@types/jest TypeScript type definitions for Jest.
supertest Helps send fake HTTP requests to your Express app in tests.
@types/supertest TypeScript types for supertest.
@types/express TypeScript types for Express (so editor shows suggestions & errors).

2. Enable ESM in package.json

  "type": "module",
  "scripts": {
    "build": "tsc",
    "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js"
  },

"type": "module" → tells Node to treat .js files as ESM


3. Configure TypeScript (tsconfig.json)

{
  "compilerOptions": {
    "target": "ESNext",
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "outDir": "dist",
    "esModuleInterop": true,
    "isolatedModules": true,
    "strict": true,
    "skipLibCheck": true
  },
  "exclude": ["node_modules", "dist", "__tests__"]
}

module: "NodeNext" is important for correct .ts + ESM resolution.


4. Configure Jest (jest.config.ts)

const config = {
  verbose: true,
  transform: {
    "^.+\\\\.ts?$": [
      "ts-jest",
      { useESM: true }
    ]
  },
  extensionsToTreatAsEsm: [".ts"],
  moduleNameMapper: {
    "^(\\\\.{1,2}/.*)\\\\.js$": "$1"
  },

  testPathIgnorePatterns: ["/dist/"]
};

export default config;

useESM: true ensures Jest compiles .ts files as native ES modules.


5. Project Folder Structure

image.png


6. Express App Example (src/index.ts)