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.
Prerequisites
- 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
Install a testing framework via Shell
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.
1# For JavaScript/TypeScript projects:2npm install --save-dev jest34# For Python projects:5pip install pytestExpected result: The testing framework installs successfully. Verify by running 'npx jest --version' or 'pytest --version' in Shell.
Create your first test file
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.
1// tests/app.test.js2const { add, multiply } = require('../utils/math');34describe('Math utilities', () => {5 test('adds two numbers correctly', () => {6 expect(add(2, 3)).toBe(5);7 expect(add(-1, 1)).toBe(0);8 });910 test('multiplies two numbers correctly', () => {11 expect(multiply(3, 4)).toBe(12);12 expect(multiply(0, 100)).toBe(0);13 });14});Expected result: A test file exists in the tests/ directory. Running it manually with 'npx jest' shows passing or failing tests.
Add test scripts to package.json
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.
1{2 "scripts": {3 "test": "jest",4 "test:watch": "jest --watch",5 "test:coverage": "jest --coverage",6 "start": "node index.js"7 }8}Expected result: Running 'npm test' in Shell executes all test files. Running 'npm run test:coverage' shows a coverage report.
Wire tests into the .replit run command
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.
1# .replit file2run = "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.
Add tests to the deployment build step
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.
1[deployment]2build = ["sh", "-c", "npm install && npm test && npm run build"]3run = ["sh", "-c", "node index.js"]4deploymentTarget = "cloudrun"Expected result: Deployments only succeed when all tests pass. Failed tests abort the deployment and show error details in the deployment logs.
Set up pytest for Python projects
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.
1# tests/test_app.py2from utils.math import add, multiply34def test_add():5 assert add(2, 3) == 56 assert add(-1, 1) == 078def test_multiply():9 assert multiply(3, 4) == 1210 assert multiply(0, 100) == 01112# .replit run command for Python:13# 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 working example
1# .replit configuration with automated testing23entrypoint = "index.js"4modules = ["nodejs-20:v8-20230920-bd784b9"]56# Run tests before starting the app7# Tests must pass or the app will not start8run = "npm test && node index.js"910# Boot command — install dependencies when the Repl starts11onBoot = "npm install"1213[nix]14channel = "stable-24_05"1516[deployment]17# Tests run during the build step — deployment fails if tests fail18build = ["sh", "-c", "npm install && npm test && npm run build"]19run = ["sh", "-c", "node index.js"]20deploymentTarget = "cloudrun"2122[[ports]]23localPort = 300024externalPort = 802526hidden = [".config", "package-lock.json", "node_modules", "coverage"]Common mistakes when automating tests in Replit
Why it's a problem: Forgetting that npm install must run before npm test in the deployment build step
How to avoid: Chain the commands: build = ["sh", "-c", "npm install && npm test && npm run build"]. Dependencies must be installed before tests can run.
Why it's a problem: Naming test files without the .test.js or test_ prefix and wondering why tests are not found
How to avoid: Follow the framework's naming convention: Jest looks for *.test.js or *.spec.js. Pytest looks for test_*.py or *_test.py.
Why it's a problem: Using the single ampersand (&) instead of double (&&) in the run command
How to avoid: Single & runs commands in parallel (background). Double && runs them sequentially and stops if any command fails. Use && for test-then-run workflows.
Why it's a problem: Not hiding the coverage directory in the .replit file, cluttering the file tree
How to avoid: 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
Still stuck?
Copy one of these prompts to get a personalized, step-by-step explanation.
I have a JavaScript project in Replit. How do I set up Jest so that tests run automatically every time I press the Run button, and also run before every deployment? I need the .replit configuration and package.json scripts.
Set up automated testing for this project. Install Jest as a dev dependency. Create a tests/app.test.js file with basic tests for the existing utility functions. Add test, test:watch, and test:coverage scripts to package.json. Update the .replit run command to run tests before starting the app. Add tests to the deployment build step.
Frequently asked questions
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.
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.
Only if you add the test command to the [deployment] build step in your .replit file. Tests do not run during deployment by default.
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.
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.
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).
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.
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.
Talk to an Expert
Our team has built 600+ apps. Get personalized help with your project.
Book a free consultation