npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jest-ci-setup_2

v1.0.0

Published

A simple project setup to demonstrate Jest CI testing with GitHub Actions.

Downloads

63

Readme

GitHub Actions and Jest Testing

Introduction

This project demonstrates the use of GitHub CLI and GitHub Actions to automate Jest tests. It is part of an assignment to practice creating YAML files and automating workflows. The repository includes the setup of Jest for unit testing and the creation of a GitHub Actions workflow to automatically run tests on every push to the main branch.

Prerequisites

Before setting up the project, ensure the following tools are installed:

  • GitHub CLI: To manage repositories and authenticate with GitHub directly from the terminal.
  • Node.js: Required for running Jest and other dependencies.
  • Jest: The testing framework used in this project.

Steps to Set Up

1. Install GitHub CLI

To install the GitHub CLI, follow the instructions on the official website: GitHub CLI Installation. Once installed, verify by running:

gh --version
This should display the version of the GitHub CLI installed.

2. Authenticate GitHub CLI
Authenticate the GitHub CLI to connect it to your GitHub account:
gh auth login
Select HTTPS when prompted, and paste your Personal Access Token (PAT) when asked for the password.

3. Clone the Repository
Clone your repository locally using the following commands:
gh repo clone <username>/<repository-name>
cd <repository-name>

4. Set Up Jest
Initialize a Node.js project and install Jest:
npm init -y
npm install jest --save-dev
mkdir __tests__
touch __tests__/example.test.js
In the __tests__/example.test.js file, add the following test:

test('adds 1 + 2 to equal 3', () => {
  expect(1 + 2).toBe(3);
});

Run the tests locally to ensure Jest is set up correctly:
npx jest

5. Commit and Push Code to GitHub
Add, commit, and push the changes to GitHub:
git add .
git commit -m "Initial project setup"
git push -u origin main

6. Set Up GitHub Actions Workflow
Create a .github/workflows/ci.yml file to automate Jest tests on every push:

name: Jest CI

on:
  push:
    branches: [main]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Setup Node.js
        uses: actions/setup-node@v2
        with:
          node-version: '16'
      - run: npm install
      - run: npm test

Commit and push the workflow file:
git add .github/workflows/ci.yml
git commit -m "Add CI workflow"
git push

7. Test the Workflow
To test the workflow, make a small change in your code and push:
echo "// Test change" >> index.js
git add .
git commit -m "Test workflow"
git push
Visit the Actions tab in your GitHub repository to see the workflow run and check the Jest test results.

Screenshots
1. CLI Installation and Authentication:
CLI Installation: ![image](https://github.com/user-attachments/assets/802f1cbc-2774-460e-be9e-4206e5d85d39)
CLI Authentication: ![image](https://github.com/user-attachments/assets/6841d61b-57d5-4cef-8230-62c6afeb4fdb)

2. Passing Tests:
Passing Tests: ![image](https://github.com/user-attachments/assets/6dbc362a-c558-436f-b1a0-db01530b101b)
               ![image](https://github.com/user-attachments/assets/c152d8f5-3aff-46e7-9689-bc571754a7ed)


Challenges Faced
Permission Denied Error: While running Jest tests in the workflow, I encountered a "Permission denied" error. This was resolved by ensuring that the necessary permissions were granted to the Node modules directory using the chmod +x command.

Conclusion
This project successfully demonstrates the integration of GitHub CLI, Jest, and GitHub Actions to automate testing workflows. The workflow runs Jest tests on every push to the main branch, ensuring that tests are always up-to-date and pass successfully.