# How to automate tests in Replit

- Tool: Replit
- Difficulty: Beginner
- Time required: 15-20 minutes
- Compatibility: All Replit plans (Starter, Core, Pro). Works with JavaScript/TypeScript (Jest, Vitest) and Python (pytest).
- Last updated: March 2026

## TL;DR

Automate tests in Replit by configuring npm test or pytest scripts in your package.json or .replit file, then wiring them into the run command so tests execute automatically before your app starts. You can run Jest for JavaScript, pytest for Python, and use the .replit run command or deployment build step to ensure tests pass before every run or deploy.

## Automate Your Testing Workflow in Replit

Running tests manually is easy to forget. This tutorial shows you how to wire test scripts into your Replit workflow so they run automatically — either before every app start (via the Run button) or before every deployment (via the build step). You will set up a test framework, write test scripts, configure your .replit file to run them, and add a deployment build gate that prevents broken code from going live.

## Before you start

- A Replit account on any plan
- A Replit App with existing code you want to test
- Basic knowledge of JavaScript or Python
- Familiarity with the Replit Shell and .replit file

## Step-by-step guide

### 1. Install a testing framework via Shell

Open the Shell tab from the Tools dock and install a testing framework. For JavaScript or TypeScript projects, Jest is the most widely used framework and requires minimal configuration. For Python projects, pytest is the standard choice. Both frameworks auto-discover test files based on naming conventions, so you do not need to manually register each test.

```
# For JavaScript/TypeScript projects:
npm install --save-dev jest

# For Python projects:
pip install pytest
```

**Expected result:** The testing framework installs successfully. Verify by running 'npx jest --version' or 'pytest --version' in Shell.

### 2. Create your first test file

Create a tests directory in your project and add a test file. Start with a simple test that verifies basic functionality — a function that returns the right value, or an API endpoint that responds with the expected status code. This confirms your testing setup works before adding more complex tests. Place test files in a tests/ directory to keep them separate from source code.

```
// tests/app.test.js
const { add, multiply } = require('../utils/math');

describe('Math utilities', () => {
  test('adds two numbers correctly', () => {
    expect(add(2, 3)).toBe(5);
    expect(add(-1, 1)).toBe(0);
  });

  test('multiplies two numbers correctly', () => {
    expect(multiply(3, 4)).toBe(12);
    expect(multiply(0, 100)).toBe(0);
  });
});
```

**Expected result:** A test file exists in the tests/ directory. Running it manually with 'npx jest' shows passing or failing tests.

### 3. Add test scripts to package.json

Open package.json and add scripts for running tests. The test script is the standard entry point that npm expects. Add a test:watch script for continuous testing during development — it re-runs tests whenever files change. The test:coverage script generates a coverage report showing which lines of code are tested.

```
{
  "scripts": {
    "test": "jest",
    "test:watch": "jest --watch",
    "test:coverage": "jest --coverage",
    "start": "node index.js"
  }
}
```

**Expected result:** Running 'npm test' in Shell executes all test files. Running 'npm run test:coverage' shows a coverage report.

### 4. Wire tests into the .replit run command

Open the .replit file (enable 'Show hidden files' if needed) and modify the run command to execute tests before starting the application. The double-ampersand (&&) ensures the app only starts if all tests pass. If any test fails, the run stops and you see the failure output in the Console. This is the simplest way to enforce testing discipline in Replit — every press of the Run button verifies your code first.

```
# .replit file
run = "npm test && node index.js"
```

**Expected result:** Pressing the Run button now runs all tests first. If tests pass, the application starts normally. If tests fail, the Console shows the failure details and the app does not start.

### 5. Add tests to the deployment build step

To prevent broken code from reaching production, add the test command to the deployment build configuration in your .replit file. The build command runs before every deployment. If tests fail during the build step, the deployment is aborted and the failing code never goes live. This is the Replit equivalent of a CI/CD gate.

```
[deployment]
build = ["sh", "-c", "npm install && npm test && npm run build"]
run = ["sh", "-c", "node index.js"]
deploymentTarget = "cloudrun"
```

**Expected result:** Deployments only succeed when all tests pass. Failed tests abort the deployment and show error details in the deployment logs.

### 6. Set up pytest for Python projects

If your project uses Python instead of JavaScript, the workflow is similar but with different tools. Create a tests directory with test files following pytest's naming convention (test_*.py). Configure the .replit run command to run pytest before starting the application. Pytest auto-discovers test files and provides clear pass/fail output with detailed error messages.

```
# tests/test_app.py
from utils.math import add, multiply

def test_add():
    assert add(2, 3) == 5
    assert add(-1, 1) == 0

def test_multiply():
    assert multiply(3, 4) == 12
    assert multiply(0, 100) == 0

# .replit run command for Python:
# run = "pytest && python main.py"
```

**Expected result:** Running 'pytest' in Shell discovers and runs all test_*.py files. The .replit run command executes pytest before starting the Python app.

## Complete code example

File: `.replit`

```toml
# .replit configuration with automated testing

entrypoint = "index.js"
modules = ["nodejs-20:v8-20230920-bd784b9"]

# Run tests before starting the app
# Tests must pass or the app will not start
run = "npm test && node index.js"

# Boot command — install dependencies when the Repl starts
onBoot = "npm install"

[nix]
channel = "stable-24_05"

[deployment]
# Tests run during the build step — deployment fails if tests fail
build = ["sh", "-c", "npm install && npm test && npm run build"]
run = ["sh", "-c", "node index.js"]
deploymentTarget = "cloudrun"

[[ports]]
localPort = 3000
externalPort = 80

hidden = [".config", "package-lock.json", "node_modules", "coverage"]
```

## Common mistakes

- **Forgetting that npm install must run before npm test in the deployment build step** — undefined Fix: Chain the commands: build = ["sh", "-c", "npm install && npm test && npm run build"]. Dependencies must be installed before tests can run.
- **Naming test files without the .test.js or test_ prefix and wondering why tests are not found** — undefined Fix: Follow the framework's naming convention: Jest looks for *.test.js or *.spec.js. Pytest looks for test_*.py or *_test.py.
- **Using the single ampersand (&) instead of double (&&) in the run command** — undefined Fix: Single & runs commands in parallel (background). Double && runs them sequentially and stops if any command fails. Use && for test-then-run workflows.
- **Not hiding the coverage directory in the .replit file, cluttering the file tree** — undefined Fix: Add 'coverage' to the hidden array in your .replit file: hidden = ["coverage", "node_modules"].

## Best practices

- Wire tests into the .replit run command with && so the app only starts when all tests pass
- Add tests to the deployment build step to prevent broken code from reaching production
- Follow naming conventions (*.test.js for Jest, test_*.py for pytest) so frameworks auto-discover tests
- Keep test files in a separate tests/ directory to avoid cluttering your source code
- Use the --bail flag to stop on the first failure during development for faster feedback
- Add a test:coverage script to track which parts of your code are tested
- Run 'npm test -- --watch' in a separate Shell during development for continuous test feedback
- Use the onBoot command in .replit to install dependencies automatically when the Repl starts

## Frequently asked questions

### Does Replit have built-in testing tools?

Replit does not have a built-in test runner UI, but you can install any testing framework (Jest, Vitest, pytest, etc.) via Shell and configure it through package.json scripts and the .replit run command.

### Can I run tests without blocking the Run button?

Yes. Run tests in a separate Shell instance while your app runs via the Run button. Use 'npm test' or 'npm run test:watch' in the Shell tab for on-demand or continuous testing.

### Will tests run during deployment?

Only if you add the test command to the [deployment] build step in your .replit file. Tests do not run during deployment by default.

### Can Replit Agent write tests for my code?

Yes. Prompt Agent v4: 'Write Jest tests for all functions in the utils/ directory. Add a test script to package.json and update the .replit run command to run tests before starting the app.' Agent will analyze your code and generate appropriate test cases.

### How do I test code that requires secrets or API keys?

Store test API keys in Tools > Secrets. Your test code accesses them via process.env.KEY_NAME. For tests that should not call real APIs, use mocking libraries like jest.mock() to simulate responses.

### Is there a way to run tests on a schedule?

Not directly through the Run button. You could create a Scheduled Deployment that runs your test suite on a cron-like schedule using Replit's Scheduled Deployments feature ($1/month base fee).

### What if test setup becomes more complex than what the .replit file supports?

For projects needing CI/CD pipelines, parallel test execution, or multi-environment testing, connect your Repl to GitHub and use GitHub Actions. The RapidDev team can help set up professional CI/CD pipelines that go beyond browser-based configuration.

### Can I generate test coverage reports in Replit?

Yes. Run 'npm run test:coverage' (with the coverage script configured) to generate an HTML coverage report. Open the coverage/lcov-report/index.html file in the Preview pane to view which lines are covered.

---

Source: https://www.rapidevelopers.com/replit-tutorial/how-to-automate-testing-workflows-with-replit-s-integrated-testing-tools
© RapidDev — https://www.rapidevelopers.com/replit-tutorial/how-to-automate-testing-workflows-with-replit-s-integrated-testing-tools
