Back to Repositories

Testing GitHub Activity Rank Calculator in github-readme-stats

This test suite validates the calculateRank function in github-readme-stats, which determines user ranking levels based on GitHub activity metrics. The tests cover various user profiles from new users to expert contributors, ensuring accurate rank calculation and percentile assignment.

Test Coverage Overview

The test suite provides comprehensive coverage of the ranking system’s functionality, testing seven distinct user profiles:
  • New user (C rank)
  • Beginner user (B- rank)
  • Median user (B+ rank)
  • Average user with all commits (B+ rank)
  • Advanced user (A rank)
  • Expert user (A+ rank)
  • Top contributor (S rank)

Implementation Analysis

The tests utilize Jest’s describe/it pattern for structured test organization. Each test case validates both the assigned rank level and precise percentile calculations using toStrictEqual assertions. The implementation handles various GitHub metrics including commits, PRs, issues, reviews, stars, and followers.

Technical Details

Testing Framework: Jest
Testing Libraries: @testing-library/jest-dom
Test Structure: Modular describe blocks with individual test cases
Assertion Method: toStrictEqual for exact matching
Input Parameters: GitHub activity metrics including commits, PRs, issues, reviews, stars, and followers

Best Practices Demonstrated

The test suite exemplifies several testing best practices:
  • Comprehensive edge case coverage
  • Clear test case descriptions
  • Consistent test structure
  • Precise assertions
  • Isolated test cases
  • Representative test data

anuraghazra/github-readme-stats

tests/calculateRank.test.js

            
import "@testing-library/jest-dom";
import { calculateRank } from "../src/calculateRank.js";
import { expect, it, describe } from "@jest/globals";

describe("Test calculateRank", () => {
  it("new user gets C rank", () => {
    expect(
      calculateRank({
        all_commits: false,
        commits: 0,
        prs: 0,
        issues: 0,
        reviews: 0,
        repos: 0,
        stars: 0,
        followers: 0,
      }),
    ).toStrictEqual({ level: "C", percentile: 100 });
  });

  it("beginner user gets B- rank", () => {
    expect(
      calculateRank({
        all_commits: false,
        commits: 125,
        prs: 25,
        issues: 10,
        reviews: 5,
        repos: 0,
        stars: 25,
        followers: 5,
      }),
    ).toStrictEqual({ level: "B-", percentile: 65.02918514848255 });
  });

  it("median user gets B+ rank", () => {
    expect(
      calculateRank({
        all_commits: false,
        commits: 250,
        prs: 50,
        issues: 25,
        reviews: 10,
        repos: 0,
        stars: 50,
        followers: 10,
      }),
    ).toStrictEqual({ level: "B+", percentile: 46.09375 });
  });

  it("average user gets B+ rank (include_all_commits)", () => {
    expect(
      calculateRank({
        all_commits: true,
        commits: 1000,
        prs: 50,
        issues: 25,
        reviews: 10,
        repos: 0,
        stars: 50,
        followers: 10,
      }),
    ).toStrictEqual({ level: "B+", percentile: 46.09375 });
  });

  it("advanced user gets A rank", () => {
    expect(
      calculateRank({
        all_commits: false,
        commits: 500,
        prs: 100,
        issues: 50,
        reviews: 20,
        repos: 0,
        stars: 200,
        followers: 40,
      }),
    ).toStrictEqual({ level: "A", percentile: 20.841471354166664 });
  });

  it("expert user gets A+ rank", () => {
    expect(
      calculateRank({
        all_commits: false,
        commits: 1000,
        prs: 200,
        issues: 100,
        reviews: 40,
        repos: 0,
        stars: 800,
        followers: 160,
      }),
    ).toStrictEqual({ level: "A+", percentile: 5.575988339442828 });
  });

  it("sindresorhus gets S rank", () => {
    expect(
      calculateRank({
        all_commits: false,
        commits: 1300,
        prs: 1500,
        issues: 4500,
        reviews: 1000,
        repos: 0,
        stars: 600000,
        followers: 50000,
      }),
    ).toStrictEqual({ level: "S", percentile: 0.4578556547153667 });
  });
});