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
Implementation Analysis
Technical Details
Best Practices Demonstrated
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'",
);
});
});