How To Use Puppeteer With Jest & TypeScript
Automate and test everything in Chrome
End-to-end tests often use Selenium, which can be used to automate tasks.
However, you may not need the weight and the feature-set of Selenium. If you are looking for a more lightweight and easy-to-use tool for automating tasks or running end-to-end tests, Puppeteer is a good choice.
In this post, we will use:
- Puppeteer: A Node library to control and automate Chromium-based browsers (e.g. Google Chrome), which can be used in continuous integration tools such as GitLab and Jenkins. I’ve written a previous article about Puppeteer for end-to-end testing.
- Jest: A spectacular test runner for writing and executing tests written in JavaScript or TypeScript. It plays well with Node.js and popular front-end frameworks, such as Angular and React. I’ve written about Jest with Angular in a previous article.
- TypeScript: A superset of JavaScript which adds static typing as well as other nice features. Popular IDEs like IntelliJ and Visual Studio Code have great support for TypeScript which improves the developer experience.
All of those libraries are popular open-source projects, maintained by well-known software companies (Google, Facebook, and Microsoft), which means that the tools won’t run out of life soon.
At the end of this post, we should have a project that can reliably run tests or automatable tasks, written in TypeScript in a Chromium browser.

How To Setup Jest, Puppeteer, and TypeScript
- Install
TypeScript. - Create a
tsconfig.json file (TypeScript can do this for you). - Install
Jest and its typings@types/jest. - Install
Puppeteer and its typings@types/puppeteer. This will download a version of Chromium. If you already have Chrome and you don’t need to download it, you can usepuppeteer-core. - Install the
jest-puppeteer preset. This makes things easier for you as it greatly reduces the setup. - Install
@types/jest-environment-puppeteer to have proper TypeScript support in your tests. - Create a
jest.config.js. It should use thejest-puppeteerpreset. - Create a
jest-puppeteer.config.js. You can use this file to set custom Chrome options (e.g. to run in non-headless mode). - Create your tests. API or end-to-end tests should be put into a dedicated tests directory and have either the word
specortestin their filename. If your tests are not found, you can customizetestMatch injest.config.js. - Run the tests:
jest --runInBand.runInBand will disable parallel test execution which can be practical for running API or end-to-end tests. - Optional: If you need to perform tasks before or after all your tests are started, you can make use of Jest
globalSetup /globalTeardown. If you do, you need to call theglobalSetup/globalTeardown, done byjest-puppeteer, yourself (e.g. close the browser when the tests are finished).
