Back to Repositories

Testing I18n Translation System in github-readme-stats

This test suite validates the internationalization (I18n) functionality of the github-readme-stats repository, focusing on string translations and error handling for different locales. It ensures proper translation lookups and appropriate error messaging for missing translations.

Test Coverage Overview

The test suite provides comprehensive coverage of the I18n module’s core functionality.

  • Tests successful translation string retrieval
  • Validates error handling for missing translation strings
  • Verifies proper handling of non-existent locales
  • Covers edge cases with invalid translation keys

Implementation Analysis

The testing approach utilizes Jest’s describe/it pattern for organizing test cases. The implementation leverages Jest’s expect assertions with both positive and negative test scenarios.

Key patterns include:
  • Instance-based testing of I18n class
  • Error assertion testing using expect().toThrow()
  • Modular test organization with statCardLocales integration

Technical Details

Testing stack includes:
  • Jest testing framework
  • ES6 module imports
  • @jest/globals for test utilities
  • Custom I18n implementation
  • Translation configuration via statCardLocales

Best Practices Demonstrated

The test suite exemplifies several testing best practices in its implementation.

  • Isolated test cases with clear descriptions
  • Comprehensive error case coverage
  • Consistent test structure and naming
  • Proper setup of test dependencies
  • Clear separation of test scenarios

anuraghazra/github-readme-stats

tests/i18n.test.js

            
import { expect, it, describe } from "@jest/globals";
import { I18n } from "../src/common/I18n.js";
import { statCardLocales } from "../src/translations.js";

describe("I18n", () => {
  it("should return translated string", () => {
    const i18n = new I18n({
      locale: "en",
      translations: statCardLocales({ name: "Anurag Hazra", apostrophe: "s" }),
    });
    expect(i18n.t("statcard.title")).toBe("Anurag Hazra's GitHub Stats");
  });

  it("should throw error if translation string not found", () => {
    const i18n = new I18n({
      locale: "en",
      translations: statCardLocales({ name: "Anurag Hazra", apostrophe: "s" }),
    });
    expect(() => i18n.t("statcard.title1")).toThrow(
      "statcard.title1 Translation string not found",
    );
  });

  it("should throw error if translation not found for locale", () => {
    const i18n = new I18n({
      locale: "asdf",
      translations: statCardLocales({ name: "Anurag Hazra", apostrophe: "s" }),
    });
    expect(() => i18n.t("statcard.title")).toThrow(
      "'statcard.title' translation not found for locale 'asdf'",
    );
  });
});