Skip to main content
RapidDev - Software Development Agency
replit-tutorial

How to run tests automatically in Replit

Automate tests in Replit by creating a test script in package.json (for Node.js) or a pytest configuration, then wiring it into your workflow. You can run tests before every run via the .replit run command, set up a Git pre-push hook to block pushes with failing tests, or configure the deployment build step to run tests before deploying. This catches bugs early without requiring manual test execution.

What you'll learn

  • Configure the .replit run command to run tests before starting your app
  • Create Git pre-push hooks that block pushes when tests fail
  • Add tests to the deployment build step as a production gate
  • Use watch mode for continuous test feedback during development
Book a free consultation
4.9Clutch rating
600+Happy partners
17+Countries served
190+Team members
Beginner8 min read15-20 minutesAll Replit plans. Works with Jest (Node.js), pytest (Python), and any CLI-based testing framework.March 2026RapidDev Engineering Team
TL;DR

Automate tests in Replit by creating a test script in package.json (for Node.js) or a pytest configuration, then wiring it into your workflow. You can run tests before every run via the .replit run command, set up a Git pre-push hook to block pushes with failing tests, or configure the deployment build step to run tests before deploying. This catches bugs early without requiring manual test execution.

Run Tests Automatically on Every Commit and Save in Replit

This tutorial shows you how to set up automated test execution in Replit so your tests run without manual intervention. You will configure test-on-run, Git hooks for test-on-commit, and deployment gates that block untested code from reaching production. This guide is for beginners who want a safety net that catches bugs automatically.

Prerequisites

  • A Replit account (any plan)
  • A Replit App with an existing test suite (Jest, pytest, or similar)
  • At least one test file in your project
  • Basic familiarity with the Replit Shell and .replit configuration

Step-by-step guide

1

Verify your test suite runs manually

Before automating, confirm your tests work when run manually from Shell. Open Shell (Tools > Shell) and run your test command: 'npm test' for Node.js or 'pytest' for Python. All tests should pass. If any tests fail, fix them first since automating broken tests will block your workflow. Note the exact command that runs your tests because you will use it in automation scripts.

typescript
1# For Node.js projects:
2npm test
3
4# For Python projects:
5pytest -v
6
7# If no test script exists in package.json, add one first:
8# "scripts": { "test": "jest --verbose --forceExit" }

Expected result: All tests pass when run manually from Shell. You see green checkmarks or PASSED indicators for each test.

2

Add tests to the .replit run command

The simplest form of test automation is running tests every time you press the Run button. Open your .replit file (enable 'Show hidden files' in the file tree menu) and modify the run command to execute tests before starting the application. Use the && operator so the app only starts if all tests pass. If any test fails, the run stops and you see the failure output in the Console without the app starting.

typescript
1# .replit - Run tests before starting the app
2entrypoint = "index.js"
3run = "npm test && node index.js"

Expected result: Pressing the Run button first executes your test suite. If all tests pass, the app starts normally. If any test fails, the Console shows the failure and the app does not start.

3

Set up a Git pre-push hook

A Git pre-push hook runs your test suite before every push to GitHub. If tests fail, the push is blocked, preventing broken code from reaching the remote repository. Create a pre-push hook file in the .git/hooks directory from Shell. This is a standard Git feature that works in Replit's Shell environment. The hook runs automatically whenever you push via Shell or the Git pane.

typescript
1# Create the pre-push hook
2mkdir -p .git/hooks
3cat > .git/hooks/pre-push << 'HOOK'
4#!/bin/sh
5echo "Running tests before push..."
6npm test
7if [ $? -ne 0 ]; then
8 echo "Tests failed. Push blocked. Fix the failing tests and try again."
9 exit 1
10fi
11echo "All tests passed. Pushing..."
12HOOK
13chmod +x .git/hooks/pre-push

Expected result: When you run 'git push' in Shell, the test suite runs first. If tests pass, the push proceeds. If tests fail, the push is blocked with a clear error message.

4

Configure test-on-deploy as a production gate

Add your test command to the deployment build step in .replit so tests must pass before code can be deployed to production. This is the most critical automation point since it prevents untested code from reaching users. Open .replit and modify the [deployment] build command to include your test step before the build step. RapidDev recommends this as a mandatory practice for any production application.

typescript
1[deployment]
2run = ["sh", "-c", "node index.js"]
3build = ["sh", "-c", "npm install && npm test && npm run build"]
4deploymentTarget = "cloudrun"

Expected result: Attempting to deploy runs the test suite during the build phase. If any test fails, the deployment is blocked and the failure output appears in the deployment logs.

5

Use watch mode for continuous test feedback

During active development, run tests in watch mode so they automatically re-execute whenever you save a file. Open a second Shell tab (or split the Shell pane) and run your test framework in watch mode. Jest has built-in watch mode; pytest can use the pytest-watch plugin. This gives you instant feedback on whether your changes break existing tests, without manually running the test command each time.

typescript
1# Jest watch mode (Node.js):
2npx jest --watch
3
4# For pytest, install pytest-watch first:
5pip install pytest-watch
6ptw -- -v

Expected result: Tests re-run automatically whenever you save a file. The Shell shows updated pass/fail results within seconds of each save.

6

Create a comprehensive test-and-commit script

Combine test execution with Git operations in a single Shell script. Create a script that runs tests, and only if they pass, stages all changes, commits with a message, and pushes to GitHub. This workflow ensures every commit in your Git history has passing tests. Save the script in your project and run it from Shell whenever you are ready to commit.

typescript
1#!/bin/bash
2# scripts/commit.sh - Test, commit, and push in one step
3
4echo "Running test suite..."
5npm test
6
7if [ $? -ne 0 ]; then
8 echo "Tests failed. Commit aborted."
9 echo "Fix the failing tests and try again."
10 exit 1
11fi
12
13echo "All tests passed."
14echo "Staging and committing changes..."
15
16git add .
17git commit -m "$1"
18
19if [ -z "$1" ]; then
20 echo "No commit message provided. Usage: bash scripts/commit.sh 'Your message'"
21 exit 1
22fi
23
24git push origin main
25echo "Changes committed and pushed successfully."

Expected result: Running the script executes tests, and only if they all pass, commits and pushes the code. Failed tests abort the entire process.

Complete working example

.replit
1# .replit configuration with automated testing at every stage
2
3entrypoint = "index.js"
4
5# Run tests before starting the app on every Run button press
6run = "npm test && node index.js"
7
8# Install dependencies on workspace boot
9onBoot = "npm install"
10
11[nix]
12channel = "stable-24_05"
13
14[deployment]
15# Tests must pass before deployment proceeds
16run = ["sh", "-c", "node index.js"]
17build = ["sh", "-c", "npm install && npm test"]
18deploymentTarget = "cloudrun"
19
20[[ports]]
21localPort = 3000
22externalPort = 80
23
24hidden = [".config", "package-lock.json", "node_modules", ".git"]

Common mistakes when running tests automatically in Replit

Why it's a problem: Adding test automation before having any tests, which causes the run command to fail with 'no test specified'

How to avoid: Create at least one test file before adding test automation. Even a simple health check test that verifies your app responds with 200 on GET / is sufficient to start.

Why it's a problem: Using slow test suites in the run command, making every Run button press take minutes

How to avoid: Split tests into 'quick' and 'full' suites. Use 'npm run test:quick' in the run command and 'npm test' (full suite) in the deployment build step.

Why it's a problem: Git hooks being lost when cloning the project to a new Repl

How to avoid: Store hook scripts in a tracked directory like scripts/ and document installation in your README. Run 'cp scripts/pre-push .git/hooks/ && chmod +x .git/hooks/pre-push' after cloning.

Why it's a problem: Tests passing in development but failing in the deployment build due to missing secrets

How to avoid: Mock environment variables in tests instead of relying on live Secrets. This makes tests portable across environments.

Best practices

  • Add tests to the deployment build command as the most critical automation point
  • Use the && operator in run commands so the app only starts if all tests pass
  • Run tests in watch mode during active development for instant feedback
  • Create a Git pre-push hook to block pushes with failing tests
  • Keep test execution under 30 seconds for the run command to avoid slowing down development
  • Run the full test suite in the deployment build step even if the run command uses a subset
  • Commit Git hook scripts to a scripts/ directory since .git/hooks is not tracked by Git
  • Start with one automated test and add more over time rather than trying to achieve full coverage immediately

Still stuck?

Copy one of these prompts to get a personalized, step-by-step explanation.

ChatGPT Prompt

I have a Node.js Express app on Replit with Jest tests. I want to automate test execution so tests run before every commit and before deployment. Help me set up a Git pre-push hook, modify the .replit run command, and add tests to the deployment build step.

Replit Prompt

Set up automated testing for this project. Modify .replit so tests run before the app starts on every Run. Create a Git pre-push hook in .git/hooks/pre-push that runs npm test and blocks the push if tests fail. Add npm test to the deployment build command.

Frequently asked questions

Not natively. Replit does not have a built-in test-on-save feature. The closest alternative is running your test framework in watch mode (npx jest --watch) in a separate Shell tab, which re-runs tests whenever a file changes.

It depends on the size of your test suite. Small suites (under 20 tests) typically add 2-5 seconds. For larger suites, create a quick-test script that runs only critical tests on Run, and save the full suite for deployment builds.

Yes. Git hooks stored in .git/hooks/ work in Replit's Shell environment. You can create pre-commit, pre-push, and other hooks using standard shell scripts. They execute when you use Git commands in Shell or the Git pane.

If the test command in the deployment build step returns a non-zero exit code, the deployment stops and shows the failure in the deployment logs. Your current production deployment continues running the previous version.

Yes. Prompt Agent with: 'Set up automated testing for this project. Create a pre-push Git hook that runs tests, add tests to the .replit run command, and configure the deployment build to run the full test suite.' Agent v4 can create all the necessary scripts and configuration.

Test output appears in the Console when run via the Run button, in the Shell when run via Git hooks, and in the deployment logs when run during the build step. All three locations show the same pass/fail output from your test framework.

Yes. The RapidDev engineering team can help set up comprehensive test automation that includes unit tests, integration tests, pre-commit hooks, deployment gates, and CI/CD pipeline integration tailored to your Replit project.

Pre-push hooks are recommended for Replit because they only run when pushing to GitHub, not on every commit. This keeps the commit process fast while still catching broken code before it reaches the remote repository. Pre-commit hooks can feel slow if your test suite takes more than a few seconds.

RapidDev

Talk to an Expert

Our team has built 600+ apps. Get personalized help with your project.

Book a free consultation

Need help with your project?

Our experts have built 600+ apps and can accelerate your development. Book a free consultation — no strings attached.

Book a free consultation

We put the rapid in RapidDev

Need a dedicated strategic tech and growth partner? Discover what RapidDev can do for your business! Book a call with our team to schedule a free, no-obligation consultation. We'll discuss your project and provide a custom quote at no cost.