add dependencies

typescript has included some deps, install deps below

  1. ts-node
  2. ts-jest

add config file

ts-jest support .ts config, just add file jest.config.ts to project root dir.

u have to told jest to use ts-jest to compile before runing, so config {preset: 'ts-jest'}.

the content like:

1
2
3
4
5
import type { Config } from '@jest/types';
const config: Config.InitialOptions = {
preset: 'ts-jest'
};
export default config;

test

u can just run:

1
npm jest

by default, it will run all *.test.ts files which in <root_dir>/test

see more ts-jest config options at https://kulshekhar.github.io/ts-jest/docs/

test single file

in common, we don’t want run all test files.

u can add filename after command ‘jest’

1
npm jest filename_with_extension

see more jest command options at https://jestjs.io/docs/cli

config vscode to run jest in debug view

add configurations in .vscode/launch.json

  • jest current opened file

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    {
    "type": "node",
    "request": "launch",
    "name": "Jest Current File",
    "program": "${workspaceFolder}/node_modules/.bin/jest",
    "args": [
    "${fileBasenameNoExtension}",
    ],
    "console": "integratedTerminal",
    "internalConsoleOptions": "neverOpen",
    "disableOptimisticBPs": true,
    "windows": {
    "program": "${workspaceFolder}/node_modules/jest/bin/jest",
    }
    }
  • jest all files

remove prop:args above, see jest cli docs and vscode launch debug