Introduction
Manual testing can quickly become a problem in your development workflow. Every time you need to remember to run tests. This process would not only waste valuable time but also increase the risk of human error; it's quite very common to forget a test or accidentally merge faulty code when you're in a hurry.
That's why this article is important because it introduces how you integrate Jasmine tests with GitHub Actions to create a fully automated testing pipeline, and walks through how to write sample tests and automate them to run on every push or pull request made in your future projects. So you no longer manually run your test, you push with a crossed finger, hoping you pass.
Prerequisites
Before diving into this tutorial, you'll need:
- Node.js (v16+) and npm installed on your machine
- Basic JavaScript knowledge - You should be comfortable writing functions and understanding basic syntax
- Git fundamentals - Know how to commit, push, and work with branches
- A GitHub account - Required for setting up GitHub Actions
- Command line basics - You'll be running npm commands and navigating directories
Optional but helpful:
- Familiarity with testing concepts (though we'll cover the basics)
- Previous experience with any testing framework
If you're missing any of these, take some time to set them up first. Everything else we'll build together from scratch.
Setting up your project
First, we need to set up a new Node.js project. This will create a package.json
file, which keeps track of all dependencies and scripts for our project.
Open your terminal (or command prompt) and follow these steps:
Create a new folder for the project using the following command
mkdir project
Navigate into the project folder using the following command
cd project
Initialize a new Node.js project inside the folder using the following command
npm init -y
This command initializes a Node.js project with default settings and automatically generates a package.json
file inside your project directory.
Next, we will need to install Jasmine as a development dependency. To do that, run this command in the project folder.
npm install jasmine --save-dev
To initialize Jasmine, you will need to run this command in the project folder.
npx jasmine init
When you do, you will see a default configuration file named jasmine.json.
Now that we have successfully installed and initialized Jasmine, we need to create a simple test to confirm that Jasmine is working correctly in our project. To do that, follow these steps:
Create a new folder called spec
inside your project directory. This is where Jasmine expects your test files to be stored. Use this command to create the spec folder
mkdir spec
Navigate into the spec
folder using the following command
cd spec
Inside the spec folder, create a new test file named sampleSpec.js
using this command
touch sampleSpec.js
If you’re on Windows and touch
doesn’t work, use:
echo > sampleSpec.js
Open the sampleSpec.js
file in your code editor and add the following test code:
describe("Sample Test", function() {
it("adds numbers correctly", function() {
expect(1 + 1).toBe(2);
});
});
This simple test checks if 1 + 1
equals 2
. If Jasmine is set up correctly, this test will pass when you run it.
Finally, run your test locally. This verifies that Jasmine is correctly installed and configured. Let’s run the following command in our terminal:
npx jasmine
A passing test confirms that your setup is complete and ready for CI integration. When your tests run successfully, Jasmine will display green indicators showing that all checks have passed, just like in the image below.
However, not all tests are meant to pass immediately. Sometimes, developers intentionally write failing tests to verify that Jasmine correctly detects errors or to test incomplete features under development. These failing tests serve as a guide, reminding you of areas that still need attention or improvement. Once the issue is resolved or the feature is implemented, those tests should then pass, confirming that your fix or new functionality works as expected.
This balance between passing and expected failing tests is a key part of effective test-driven development, ensuring your project grows with confidence and precision.
Example folder structure
Now that you’ve successfully set up and tested your Jasmine environment, it’s important to organize your files for better clarity and maintainability.
Use this structure as a reference:
project/
│
├── spec/
│ └── sampleSpec.js
├── src/
│ └── app.js
├── package.json
├── jasmine.json
└── .github/
└── workflows/
└── test.yml
This layout separates your test files, source code, and CI configurations clearly. It helps you and your team maintain order as your project grows.
Now that we have neatly organized files, you can proceed to automate the testing process using GitHub Actions.
Add GitHub Actions workflow
With your project structure in place, the next step is to automate your testing process. By using GitHub Actions, you can ensure your Jasmine tests run automatically whenever you push new code or open a pull request.
Create a workflow file at .github/workflows/test.yml.
This file tells GitHub Actions what to do during each run.
name: Run Jasmine Tests
on:
push:
pull_request:
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Install dependencies
run: npm install
- name: Run tests
run: npx jasmine
Each step performs a key task:
-
Checkout code
downloads your repository to the workflow runner. -
Set up Node.js
ensure the correct runtime version. -
Install dependencies
gets all required packages. -
Run tests
executes Jasmine to validate your code.
This setup guarantees that your code is tested automatically on every commit.
Now that the automation workflow is ready, the next step is to push your code to GitHub and trigger the workflow in action.
Trigger the workflow
You’ve defined your CI workflow, now it’s time to see it work. Committing and pushing your files to GitHub connects your project to the automated testing pipeline.
Commit your files and push them to the main branch:
git add .
git commit -m "Set up Jasmine CI workflow"
git push origin main
Each push triggers the workflow defined in test.yml.
GitHub Actions installs dependencies and runs your tests automatically. Now that your workflow has been triggered, let’s verify that it runs successfully and that your tests pass as expected.
Verify the workflow
At this point, your tests should be running automatically in GitHub Actions. It’s important to confirm that everything executes correctly and that your CI pipeline is stable.
Follow these steps to verify:
- Open your repository on GitHub.
- Click the Actions tab.
- Select your workflow from the list.
- Check the log to confirm that all steps completed and tests passed.
The image below shows what it will look like:
If any step fails, review the logs to identify configuration or test issues. Fixing them early prevents broken code from merging.
Once you’ve confirmed that your workflow works, you can optionally add a visual indicator to your repository, a status badge that shows whether your tests are passing or failing.
Add test status badges (optional)
Now that your CI pipeline is up and running, adding a status badge provides instant visibility into your project’s health. Badges are especially useful for open-source projects and team collaboration.
Add this line to your README.md
file: https://github.com/your-username/your-repo/actions/workflows/test.yml/badge.svg. This will show the status of the test if passed or failed:
The badge updates automatically after every workflow run. This adds visibility and helps collaborators monitor build status at a glance. With your automated workflow complete, let’s look at a few best practices to make your testing process more reliable and efficient.
Tips for reliable testing
Now that your Jasmine tests run automatically with each commit, maintaining consistency and reliability becomes crucial. Following these best practices ensures that your CI pipeline stays fast, dependable, and easy to maintain over time.
1. Keep tests small and independent
Write focused tests that check one specific functionality at a time. This makes it easier to identify which part of your code is failing when an error occurs. Independent tests can run in any order without affecting each other, which improves accuracy and speeds up debugging.
2. Avoid relying on external data sources
Tests should not depend on external APIs, databases, or network connections. These can introduce delays or failures unrelated to your actual code. Instead, use mock data or stubs to simulate external responses, ensuring that your tests remain stable even when you’re offline.
3. Run tests locally before committing
Always run your Jasmine tests locally before pushing code to your repository. This helps you catch issues early, saving time and preventing unnecessary CI failures. A quick local test run ensures that only clean, working code reaches your remote branch.
4. Cache dependencies in CI for faster execution
Most CI systems (like GitHub Actions) allow you to cache dependencies such as node_modules
. Caching means that repeated builds won’t reinstall all packages from scratch, significantly reducing test run times and improving overall pipeline efficiency.
By applying these practices, your automated testing workflow becomes more reliable, efficient, and scalable. You’ll spend less time fixing broken builds and more time focusing on writing high-quality, maintainable code.
Conclusion
You now understand how to install essential dependencies, configure Jasmine for your project, and create a workflow that runs tests automatically with every push or pull request. More importantly, you’ve seen how continuous integration helps detect bugs early, keeps your code clean, and verifies every change before merging all without manual intervention.
Applying what you’ve learned will greatly improve your workflow. Automated testing reduces errors, accelerates feedback, and boosts confidence in your codebase. With Jasmine and GitHub Actions working together, you’ve built a strong foundation for reliability and efficiency in every update.
Top comments (0)