Jest is a delightful JavaScript Testing Framework with a focus on simplicity.

superplate serving optional Jest testing plugin which uses ts-jest under the hood and comes with nock and isomorphic-unfetch.


You can see how superplate's Jest plugin is implemented below.


We need to configure Jest to work with our environment. We'll be doing this in test/jest.config.js with the following code;

const { pathsToModuleNameMapper } = require("ts-jest/utils");
const { compilerOptions } = require("../tsconfig.json");
const paths = compilerOptions.paths ? compilerOptions.paths : {};
module.exports = {
rootDir: "../",
setupFilesAfterEnv: ["<rootDir>/test/jest.setup.js"],
testPathIgnorePatterns: ["<rootDir>/.next/", "<rootDir>/node_modules/"],
moduleNameMapper: {
...pathsToModuleNameMapper(paths, { prefix: "<rootDir>/" }),
"\\.(scss|sass|css)$": "identity-obj-proxy",

What we basically do is;

  • Specifying a root directory for tests
  • Path to our Setup configuration for the testing environment
  • Mapping our tsconfig paths to jest
  • Mocking styles to prevent Jest to throw an error


We need to apply the polyfill for the fetch, set environment variables to use in tests and setup nock to prevent memory leaks. We'll place this setup in test/jest.setup.js with the following code;

import "isomorphic-unfetch";
import nock from "nock";
import dotenv from "dotenv";
dotenv.config({ path: ".env.test" });
afterAll(() => {

Running Tests#

We need to specify our config file when running jest, we can do this with jest -c test/jest.config.js. We will add this command to package.json as a test script.

"scripts": {
"test": "jest -c test/jest.config.js"

Example Test#

// This is an example function to test
export const const add = (...nums: number[]) => {
return nums.reduce((acc, curr) => acc + curr, 0);
import { add } from "./add";
describe("Add Function", () => {
it("1 + 2 + 3 = 6", () => {

Adding Jest to your project later#


All this work will be handled automatically by superplate, so you don’t need to do anything extra as long as you choose Jest as testing plugin during the project creation phase.

If you want to add Jest to your existing project first install dependencies:

npm i -D jest ts-jest identity-obj-proxy nock isomorphic-unfetch dotenv

Then you can follow documentations for detail usage

Last updated on by Ali Emir Şen