Testing
The locale that messages and getLocale() read depends on where the test runs. Under Node, locales.run selects it; in a browser, the URL does.
On this page
Tests under Node
src/messages/nav.test.tsimport { expect, it } from 'vitest';
import { locales } from '../i18n';
import { home } from './nav';
it('renders in the default locale', () => {
expect(home()).toBe('ホーム');
});
it('renders in the locale run names', () => {
expect(locales.run('en', () => home())).toBe('Home');
});
it('keeps the locale across awaits', async () => {
const text = await locales.run('en', async () => {
await Promise.resolve();
return home();
});
expect(text).toBe('Home');
});With no locale named, a message returns the default locale’s text. To check another locale, call the message inside locales.run.
An async function keeps its locale across await. Concurrent run calls keep their locales apart.
Testing paramsSchema
src/i18n.test.tsimport { expect, it } from 'vitest';
import { locales } from './i18n';
it('accepts only the listed locales', () => {
const { validate } = locales.paramsSchema['~standard'];
expect(
locales.run('ja', () => validate({ locale: 'en' })),
).toStrictEqual({ value: { locale: 'en' } });
expect(validate({ locale: 'fr' })).toMatchObject({
issues: [{ path: ['locale'] }],
});
});When paramsSchema accepts a locale, it sets that locale for the rest of the caller’s async context. Called directly from a test, it leaves that locale set for the tests that follow.
Validate a locale that should be accepted inside run. The locale is restored when run returns, so it does not reach the tests after it.
A locale outside the list is refused and sets nothing, so that call needs no run.
Tests in a browser
src/messages/nav.browser.test.tsimport { afterEach, expect, it } from 'vitest';
import { locales } from '../i18n';
import { home } from './nav';
const initial = location.pathname;
afterEach(() => {
history.replaceState(null, '', initial);
});
it('renders in the locale the URL names', () => {
history.replaceState(null, '', '/en/cart');
expect(locales.getLocale()).toBe('en');
expect(home()).toBe('Home');
});In a real browser, such as Vitest’s browser mode, the first segment of location.pathname is the locale. Change it with history.replaceState, and restore it in afterEach.
Calling run in a browser fails with locales.run: in the browser the URL is the locale — navigate instead. An environment that defines document, such as jsdom or happy-dom, counts as a browser, so set the locale with the URL there too.
defineLocales inside a test
vitest.setup.tsimport { defineLocales } from '@k8ordo/i18n';
import { beforeEach } from 'vitest';
import { locales } from './src/i18n';
beforeEach(() => {
defineLocales(locales.definitions, { default: locales.default });
});Calling defineLocales in a test replaces the set every message reads; the last one defined wins.
Tests import the app’s locales. If some test defines a set of its own, define the app’s set again in a beforeEach in the setup file.
Type tests
src/messages/cart.test.tsimport { message } from '@k8ordo/i18n';
import { expectTypeOf, it } from 'vitest';
import { added } from './cart';
it('types the arguments of a message', () => {
expectTypeOf(added).parameters.toEqualTypeOf<
[name: string]
>();
});
it('refuses a message without every locale', () => {
// @ts-expect-error
message({ ja: '保存' });
});Type tests can check a message’s argument types and the type error for a missing locale.
The declaration under @ts-expect-error passes as long as the missing locale is a type error. If Register stops carrying the locale type and the line compiles, the unused @ts-expect-error fails the type check.