/replit-tutorials

How to run backend unit tests in Replit

Learn how to run backend unit tests in Replit with simple steps to speed up debugging and streamline your development workflow.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free No-Code consultation

How to run backend unit tests in Replit

You run backend unit tests in Replit by installing your test framework (like Jest for Node or pytest for Python), creating a test command in your package.json or running the test command directly in the Replit shell, and making sure your backend code does not depend on the Replit “Run” button process. In Replit, the shell is where tests actually run reliably. The “Run” button is only for your app/server, not for tests.

 

What “running backend unit tests” really means in Replit

 

In Replit, your backend code usually lives in a folder like src/ or server/. Unit tests are just normal code files that test those functions, but they’re executed through a test runner. Replit doesn’t add anything special — you run tests the same way you would locally, but you use the Shell instead of the “Run” button.

The most important habit: treat tests as a separate command. Don’t attach them to the Run button because the Run button is meant to start your server, not execute a test suite.

 

Node.js example (Jest)

 

This is the most common pattern in Replit for Node backends.

  • Create a test file like sum.test.js.
  • Install Jest.
  • Add a test script to package.json.
  • Run tests from the Shell.

 

npm install --save-dev jest

 

{
  "name": "my-backend",
  "type": "module",
  "scripts": {
    "test": "jest"
  }
}

 

// sum.js
export function sum(a, b) {
  return a + b
}

 

// sum.test.js
import { sum } from "./sum.js"

test("adds two numbers", () => {
  expect(sum(2, 3)).toBe(5)
})

 

npm test

 

Replit will show Jest’s output in the Shell panel exactly like local dev. If your Repl sleeps or restarts, your installed dev dependencies persist, so you don’t need to reinstall.

 

Python example (pytest)

 

If you’re using Flask, FastAPI, or a custom Python backend, pytest works smoothly in Replit.

  • Install pytest
  • Create test files ending in \_test.py
  • Run pytest in the Shell

 

pip install pytest

 

# utils.py
def add(a, b):
    return a + b

 

# test_utils.py
from utils import add

def test_add():
    assert add(2, 5) == 7

 

pytest

 

Replit’s Python environment behaves like a regular venv, so pytest just works. The only caveat is that if you rely on environment variables, set them in the Replit Secrets tab so tests can read them.

 

Important Replit-specific tips

 

  • Use the Shell, not the Run button, for running tests. The Run button launches your server and won’t run test commands correctly.
  • If tests need secrets, define them in the Replit Secrets manager. They’ll be available as process.env in Node or os.environ in Python.
  • Don’t store test results in files expecting persistence. Replit’s filesystem persists, but anything in /tmp does not.
  • Keep tests separate from server startup code. If importing your server file automatically starts the server, move startup logic into a “startServer” function so tests can import code without launching the server.
  • Avoid watch modes. Jest --watch or nodemon + tests can behave strangely in Replit’s container. Use single-run mode.

 

A common pitfall: tests triggering the server

 

Many beginners put their Express/FastAPI server startup directly at the top level of the main file. Then importing that file in a test accidentally starts the whole server. The fix is simple: export the app definition, and start the server only when running the actual server script.

 

// server.js
import express from "express"

export const app = express()

app.get("/", (req, res) => {
  res.send("ok")
})

// Only start server if this file is run directly
if (process.env.START_SERVER === "true") {
  app.listen(3000, () => console.log("running on 3000"))
}

 

Now tests can import app without starting the server. To run the server with the Run button, just set START\_SERVER in Secrets.

 

Bottom line

 

Running backend tests in Replit is basically the same as local dev — install your test framework, create test files, and run the test command in the Shell. The only “Replit-specific” rule you must follow is to avoid using the Run button for tests and keep server startup separate so tests can import your backend code cleanly.

Still stuck?
Copy this prompt into ChatGPT and get a clear, personalized explanation.

This prompt helps an AI assistant understand your setup and guide you through the fix step by step, without assuming technical knowledge.

AI AI Prompt

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022