Learn Testing i18n

Unit tests for formatting pin the locale and timeZone, and write Intl's spaces as they are ('USD\u00a070.00'); without that, a test passes on a laptop in the…

A developer on Mrs. Sri's team looks at the booking card in English every day. The bugs hide in the languages they rarely open. Testing i18n makes sure the card is right in every language: prices and dates formatted correctly, every key translated, the right plural form, long text that fits, and an Arabic layout that mirrors correctly. It's like a dress rehearsal with costumes made 40% bigger on purpose. If that costume still fits through the stage door, the real one will. Pseudo-localisation is that big costume, and automated tests are the rehearsal, repeated on every pull request.

Unit tests for formatting: pin the locale and the time zone

// src/format.js
export const money = (locale, n) =>
  new Intl.NumberFormat(locale, { style: 'currency', currency: 'USD', currencyDisplay: 'code' }).format(n);
export const checkIn = (locale, date, timeZone) =>
  new Intl.DateTimeFormat(locale, { dateStyle: 'medium', timeStyle: 'short', timeZone }).format(date);

// src/format.test.js (Vitest)
import { it, expect } from 'vitest';
import { money, checkIn } from './format.js';
const CHECK_IN = new Date('2026-12-12T06:00:00Z'); // Saturday 14:00 at the homestay (Asia/Singapore)

it('en-US price (ordinary space)', () => expect(money('en-US', 70)).toBe('USD 70.00'));
it('en-US price (U+00A0)', () => expect(money('en-US', 70)).toBe('USD\u00a070.00'));
it('ja-JP check-in, zone pinned', () => expect(checkIn('ja-JP', CHECK_IN, 'Asia/Singapore')).toBe('2026/12/12 14:00'));
it('ja-JP check-in, zone from the machine', () => expect(checkIn('ja-JP', CHECK_IN)).toBe('2026/12/12 14:00'));

money('en-US', 70) writes "USD 70.00" on the screen. Before you look at the result, guess: does the first test, whose expected value has the ordinary space you typed, pass or fail? Here is the real Vitest 5.0.3 output with TZ=UTC (the zone of GitHub Actions runners):

 × en-US price (ordinary space)
AssertionError: expected 'USD 70.00' to be 'USD 70.00' // Object.is equality
Expected: "USD 70.00"
Received: "USD 70.00"

 × ja-JP check-in, zone from the machine
AssertionError: expected '2026/12/12 6:00' to be '2026/12/12 14:00' // Object.is equality

The price test fails, even though Expected and Received look the same. Intl puts a no-break space, U+00A0, between "USD" and the number, not an ordinary space. Write 'USD\u00a070.00' so the character is visible in the code.

A snapshot from Node is not a browser snapshot

A snapshot (toMatchSnapshot, toMatchInlineSnapshot) stores a result once, then compares against it on every run. The catch: Vitest runs in Node, guests use browsers, and their ICU data differs. The rupiah (IDR), a currency the engines disagree on, for the page that guests from Indonesia see:

Intl.NumberFormat('id-ID', { style: 'currency', currency: 'IDR' }).format(700000) Result
Node 22.22.1 (CLDR 48), where Vitest runs Rp 700.000
Chrome 147, Firefox 146 Rp 700.000,00
Chrome 156 (measured 29 October 2026) Rp 700.000
Safari 26.4 Rp700.000,00

The "Rp 700.000" snapshot passes forever in CI, while the card in Firefox, Safari and Chrome 147 writes ",00". And Safari still writes "Rp700.000" with no space even with maximumFractionDigits: 0. So: don't snapshot Intl output across engines. Test the options you pass in unit tests, and test the final text in the engine that renders it (Playwright in Chromium, Firefox and WebKit).

Pseudo-localisation: the exaggerated costume

Pseudo-localisation makes a fake language out of the source language: every letter becomes an accented letter that is still readable, the text gets longer, and it gets square brackets. The convention is the en-XA locale tag, as on Android.

const ACCENT = { a: 'å', b: 'ƀ', c: 'ç', d: 'ð', e: 'é', f: 'ƒ', g: 'ĝ', h: 'ĥ', i: 'î', j: 'ĵ', k: 'ķ', l: 'ļ', m: 'ɱ', n: 'ñ', o: 'ö', p: 'þ', q: 'ǫ', r: 'ŕ', s: 'š', t: 'ţ', u: 'û', v: 'ṽ', w: 'ŵ', x: 'ẋ', y: 'ý', z: 'ž', A: 'Å', B: 'Ɓ', C: 'Ç', D: 'Ð', E: 'É', F: 'Ƒ', G: 'Ĝ', H: 'Ĥ', I: 'Î', J: 'Ĵ', K: 'Ķ', L: 'Ļ', M: 'Ṁ', N: 'Ñ', O: 'Ö', P: 'Þ', Q: 'Ǫ', R: 'Ŕ', S: 'Š', T: 'Ţ', U: 'Û', V: 'Ṽ', W: 'Ŵ', X: 'Ẋ', Y: 'Ý', Z: 'Ž' };

// Accented letters, +40% length, square brackets. {{...}} slots are left alone.
function pseudo(text) {
  const parts = text.split(/(\{\{[^{}]+\}\})/);
  const body = parts.map((p, i) => (i % 2 ? p : [...p].map((c) => ACCENT[c] ?? c).join(''))).join('');
  const letters = [...parts.filter((_, i) => i % 2 === 0).join('')].length;
  return `[${body} ${'~'.repeat(Math.max(2, Math.ceil(letters * 0.4)))}]`;
}
pseudo('Book now, pay at the homestay'); // '[Ɓööķ ñöŵ, þåý åţ ţĥé ĥöɱéšţåý ~~~~~~~~~~~~]', 29 becomes 44 characters
pseudo('Total for {{count}} nights: {{price}}'); // '[Ţöţåļ ƒöŕ {{count}} ñîĝĥţš: {{price}} ~~~~~~~~]'

What to look for on the pseudo screen: text that overflows its box, text that is truncated (a missing ], ... in the middle of a word), and a broken layout (a button on three lines, an icon pushed away). The 29-character "Book now, pay at the homestay" button becomes 44, past its limit of 32.

Every key renders, no raw keys

// test/i18n.js: in tests, a key missing from every language throws at once.
i18next.init({
  /* ...the card's setup... */
  saveMissing: true,
  missingKeyHandler: (lngs, ns, key) => { throw new Error(`Missing key: ${ns}:${key}`); },
});

const KEYS = ['room', 'perNight', 'checkIn', 'group', 'nights', 'total', 'book', 'cancel', 'breakfast', 'thanks'];
it.each(['en-US', 'id-ID', 'ja-JP', 'ar-EG'])('every key is translated in %s itself', (locale) => {
  const t = i18next.getFixedT(locale);
  for (const key of KEYS) expect(t(key, { count: 2, returnDetails: true }).usedLng).toBe(locale);
});

Arabic plurals: test every category

it.each([[0, 'zero'], [1, 'one'], [2, 'two'], [3, 'few'], [11, 'many'], [100, 'other']])('ar-EG %i nights uses nights_%s', (n, category) => {
  const d = i18next.getFixedT('ar-EG')('nights', { count: n, returnDetails: true });
  expect(d.exactUsedKey).toBe(`nights_${category}`);
  expect(d.usedLng).toBe('ar-EG');
});

The results with our dictionary: 0 "0 ليلة", 1 "ليلة واحدة", 2 "ليلتان", 3 "3 ليالٍ", 11 "11 ليلة", 100 "100 ليلة". Delete nights_few, and since i18next 27.1, 3 nights becomes "3 ليلة" from nights_other (version 26: "3 nights" from en-US): the few test fails on its exactUsedKey. One number per category is enough, as long as every category gets one.

RTL: a screenshot per direction with Playwright

// tests/card-rtl.spec.mjs (Playwright)
import { test, expect } from '@playwright/test';

for (const [locale, dir, path] of [['en-US', 'ltr', '/en/terrace-room'], ['ar-EG', 'rtl', '/ar/غرفة-التراس']]) {
  test(`card ${locale}`, async ({ page }) => {
    await page.goto(path); // baseURL in playwright.config
    await expect(page.locator('html')).toHaveAttribute('dir', dir);
    const stripe = await page.locator('article').evaluate((el) => getComputedStyle(el).borderRightWidth);
    expect(stripe).toBe(dir === 'rtl' ? '4px' : '0px');   // the green stripe moves to the right
    await expect(page.locator('article')).toHaveScreenshot(`card-${locale}.png`);
  });
}

The first run fails with "A snapshot doesn't exist at .../card-ar-EG-chromium-darwin.png, writing actual.", and the second run passes. The baseline's file name carries the browser and the operating system (chromium-darwin), because fonts render differently on each OS. Make baselines on the same OS as CI (Linux), for example in Playwright's official Docker image. We ran it with Playwright 1.59.1; the latest version as of October 2026 is 1.64.0.

⚠️ Tests that pass while the card is wrong

TL;DR: Unit tests for formatting pin the locale and timeZone, and write Intl's spaces as they are ('USD\u00a070.00'); without that, a test passes on a laptop in the homestay's zone and fails on a UTC CI, or fails with an Expected and a Received that look the same. Don't snapshot Intl output across engines: Node 22 writes "Rp 700.000", Firefox "Rp 700.000,00", Safari "Rp700.000,00". Pseudo-localisation (en-XA: accented letters, +40% length, square brackets, slots left alone) gives hardcoded text away because it stays plain, and shows text that overflows, gets truncated, or breaks the layout (the IBM table: short text can grow 2 to 3 times). In tests, a missingKeyHandler that throws and returnDetails.usedLng make sure no raw key shows and nothing falls to another language. Test ar-EG plurals at 0, 1, 2, 3, 11, 100. For RTL, Playwright checks dir="rtl", the stripe's position and toHaveScreenshot per locale, with baselines made on the same OS as CI.

⚡ Write Intl's spaces as they are

Write:

it('en-US price (U+00A0)', () => expect(money('en-US', 70)).toBe('USD\u00a070.00'));
Guess first: if the expected value is written 'USD 70.00' with an ordinary space, does the test pass or fail?

It fails, even though Expected and Received look the same. Intl puts a no-break space, U+00A0, between "USD" and the number.

Meaning: Write \u00a0 so the character is visible in the code, and always pass timeZone. Without it, an hour test passes on a laptop in the homestay's zone and fails in a CI that runs on UTC.

⚡ Pseudo-localisation: the exaggerated costume

Write:

pseudo('Book now, pay at the homestay'); // '[Ɓööķ ñöŵ, þåý åţ ţĥé ĥöɱéšţåý ~~~~~~~~~~~~]', 29 becomes 44 characters

Meaning: Every letter becomes a look-alike accented Unicode letter, the text gets 40% longer, and it gets square brackets. Hardcoded text shows up at once because it stays plain, and a missing ] means the text is cut off.

⚡ Every category, both directions

Meaning: Test ar-EG plurals at 0, 1, 2, 3, 11 and 100: one number per category. For RTL, Playwright checks dir="rtl" and a screenshot per locale, with baselines made on the same OS as CI.

What you will learn