Skip to content

Instantly share code, notes, and snippets.

@vqc1909a
Last active October 13, 2025 06:58
Show Gist options
  • Select an option

  • Save vqc1909a/935e46daef1274a3960e1af7172e837a to your computer and use it in GitHub Desktop.

Select an option

Save vqc1909a/935e46daef1274a3960e1af7172e837a to your computer and use it in GitHub Desktop.
SETUP_JEST_IN_NODEJS.md

Node JS TDD (Test Driven Development)

  1. Install the following dependencies

      yarn add express mongoose dotenv 
  2. Install the following development dependencies

      yarn add nodemon mongodb-memory-server jest supertest -D
  3. Generate a basic configuration file of eslint (OPTIONAL)

    Follow the getting started documentation of eslint

    Or execute the next command answering all the questions to make automatically the file eslint for your project

      yarn create @eslint/config
    

    If you have your tests folder into another folder except the root, you have to add the following in eslint.config.js to avoid no-undef errors from ESLint, which doesn't know about the Jest globals. Ej: Into folder src

      .
      .
      .
      {
        files: ["src/tests/**/*"],
        env: {
          jest: true,
        },
      }
  4. Generate a basic configuration file of jest

    Follow the getting started documentation of jest

    Or execute the next command answering all the questions to make automatically the file jest for your project

      yarn add babel-jest @babel/core @babel/preset-env -D
      &
      yarn create jest
    

    NOTE: Try to verify the name of you jest file just created, it should be jest.config.mjs (ESM modules)

    If you are using ESModules to import modules, you have to set up babel in your project, so, you have to create the file babel.config.cjs (Recommended) or babel.config.js

      module.exports = {
        presets: [["@babel/preset-env", {targets: {node: "current"}}]],
      };
  5. Setting up environment variables for testing with jest

    For this, we need to install dotenv package

        yarn add dotenv

    Then we need to set up this package into the file jest.setup.js just created

        import dotenv from "dotenv";
        import { closeDB, connectDB, getUri } from "./backend/src/config/db";
        import { seederBuilder } from "./backend/src/builder/seeder-builder";
        dotenv.config({ path: ".env.test" });
    
        beforeAll(async () => {
          const uri = await getUri();
          await connectDB({uri});
          await seederBuilder();
        });
    
        afterAll(async () => {
          await closeDB();
        });
    
        console.log({
            PORT: process.env.PORT,
        });

    NOTE: Here we don't need to mock the environment variables because always we work with process.env unlike vite in the frontend

  6. Make sure to have this setting up into jest.config.mjs

        .
        .
        .
        setupFilesAfterEnv: ["<rootDir>/jest.setup.js"],
  7. This is a basic configuration for your scripts in package.json

      "scripts": {
        "dev": "nodemon server.js",
        "start": "node server.js",
        "test": "jest --watchAll --detectOpenHandles",
        "lint": "yarn eslint"
      }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment