oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
74.49k stars 2.79k forks source link

Running typescript jest tests with bun #5190

Open robobun opened 1 year ago

robobun commented 1 year ago

Hi there!

Trying to migrate our codebase to bun. Sadly we can't use the built in bun test runner as we need to use mocks, which are currently unsupported. Our current test setup is jest, using the ts-jest preset in order to run them

As part of the initial configuration with bun, we updated our tsconfig, from:

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es2022",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "allowSyntheticDefaultImports": true,
    "outDir": "dist",
    "moduleResolution": "node",
    "sourceMap": true
  },
  "compileOnSave": true,
  "include": ["src/**/*.ts"],
  "exclude": ["node_modules"],
}

to

{
  "compilerOptions": {
    "lib": ["ESNext"],
    "module": "esnext",
    "target": "esnext",
    "moduleResolution": "bundler",
    "moduleDetection": "force",
    "allowImportingTsExtensions": true,
    "noEmit": true,
    "composite": true,
    "strict": true,
    "downlevelIteration": true,
    "skipLibCheck": true,
    "jsx": "preserve",
    "allowSyntheticDefaultImports": true,
    "forceConsistentCasingInFileNames": true,
    "allowJs": true,
    "types": [
      "bun-types" // add Bun global
    ]
  }
}

However, with this updated tsconfig, both bun run test and npm run test now fail, as every import resolves to undefined, so code like this in test files fails

import { jsdom } from 'jsdom'
jsdom.JSDOM

as jsdom is undefined

Originally reported on Discord: Running typescript jest tests with bun

Wazbat commented 1 year ago

Created example repo: https://github.com/Wazbat/bun-ts-jest-example