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
Verify your test suite runs manually
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.
1# For Node.js projects:2npm test34# For Python projects:5pytest -v67# 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.
Add tests to the .replit run command
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.
1# .replit - Run tests before starting the app2entrypoint = "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.
Set up a Git pre-push hook
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.
1# Create the pre-push hook2mkdir -p .git/hooks3cat > .git/hooks/pre-push << 'HOOK'4#!/bin/sh5echo "Running tests before push..."6npm test7if [ $? -ne 0 ]; then8 echo "Tests failed. Push blocked. Fix the failing tests and try again."9 exit 110fi11echo "All tests passed. Pushing..."12HOOK13chmod +x .git/hooks/pre-pushExpected 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.
Configure test-on-deploy as a production gate
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.
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.
Use watch mode for continuous test feedback
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.
1# Jest watch mode (Node.js):2npx jest --watch34# For pytest, install pytest-watch first:5pip install pytest-watch6ptw -- -vExpected result: Tests re-run automatically whenever you save a file. The Shell shows updated pass/fail results within seconds of each save.
Create a comprehensive test-and-commit script
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.
1#!/bin/bash2# scripts/commit.sh - Test, commit, and push in one step34echo "Running test suite..."5npm test67if [ $? -ne 0 ]; then8 echo "Tests failed. Commit aborted."9 echo "Fix the failing tests and try again."10 exit 111fi1213echo "All tests passed."14echo "Staging and committing changes..."1516git add .17git commit -m "$1"1819if [ -z "$1" ]; then20 echo "No commit message provided. Usage: bash scripts/commit.sh 'Your message'"21 exit 122fi2324git push origin main25echo "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
1# .replit configuration with automated testing at every stage23entrypoint = "index.js"45# Run tests before starting the app on every Run button press6run = "npm test && node index.js"78# Install dependencies on workspace boot9onBoot = "npm install"1011[nix]12channel = "stable-24_05"1314[deployment]15# Tests must pass before deployment proceeds16run = ["sh", "-c", "node index.js"]17build = ["sh", "-c", "npm install && npm test"]18deploymentTarget = "cloudrun"1920[[ports]]21localPort = 300022externalPort = 802324hidden = [".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.
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.
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.
Talk to an Expert
Our team has built 600+ apps. Get personalized help with your project.
Book a free consultation