Setup Testing for Typescript Using Jest
Blog / Setup Testing for Typescript Using Jest
2 Minutes Read
By @akash_dathan

Setup Testing for Typescript Using Jest

Jest is one of the prominent testing frameworks available. Following is how you can set up Jest easily.


Installing Jest

Jest has to be installed as a dev dependency since we don’t need to include it in the PROD build

npm install --save-dev jest @types/jest ts-jest

Configuring Jest

  • The following config can be added to the root level of package.json file.
  • rootDir has to be set to the directory containing your tests.
  • testRegex contains the regex to match your test file name.
"jest": {
  "moduleFileExtensions": [
    "js",
    "json",
    "ts"
  ],
  "moduleNameMapper": {
    "^jose/(.*)$": "<rootDir>/../node_modules/jose/dist/node/cjs/$1"
  },
  "rootDir": "src",
  "testRegex": ".*\\.test\\.ts$",
  "transform": {
    "^.+\\.(t|j)s$": "ts-jest"
  },
  "testEnvironment": "node"
}

Running Tests

  • Configure the test command in your package.json file under the scripts section
"scripts": {
  ...
  "test": "jest ./src",
  ...
}
  • Run the tests using the following command
npm run test

Thats all folks, happy hacking 🙌