Test React messages
Test translated text, values, fallback, rich elements, and direction with deterministic local catalogs.
Message rendering can be tested without a translation request. Supply a known catalog, render the component, and assert the behavior the reader depends on. A fixed translation makes a key mismatch, missing value, or broken link reproducible.
Test translation and source fallback #
This React 19 fixture checks both results from the same component:
import assert from "node:assert/strict";
import { renderToStaticMarkup } from "react-dom/server";
import { computeKey, LingoProvider, useLingo } from "@lingo.dev/react";
function Greeting() {
const l = useLingo();
return <h1>{l.text("Hello, {name}.", {
context: "Greeting on the account dashboard",
values: { name: "Ana" },
})}</h1>;
}
const key = computeKey("Hello, {name}.", "Greeting on the account dashboard");
const translated = renderToStaticMarkup(
<LingoProvider locale="es" messages={{ [key]: "Hola, {name}." }}>
<Greeting />
</LingoProvider>,
);
assert.equal(translated, "<h1>Hola, Ana.</h1>");
const fallback = renderToStaticMarkup(
<LingoProvider locale="es" messages={{}}><Greeting /></LingoProvider>,
);
assert.equal(fallback, "<h1>Hello, Ana.</h1>");Run this through the project's existing TSX-capable test runner. Add an empty-string entry and a deliberately different context as separate cases; both should use the source. Include an absent simple value to detect a visible placeholder instead of assuming a generated declaration guarantees every runtime input.
Use the fixed-English helpers for source tests #
The separate testing entry provides a shorter wrapper when the test only needs source text:
import { renderStatic, withLingo } from "@lingo.dev/react/testing";
const markup = renderStatic(<Greeting />);
const wrapped = withLingo(<Greeting />);This continues the Greeting example. renderStatic(node: ReactNode): string wraps the node in an English provider with no messages and returns static markup. withLingo(node: ReactNode): ReactNode returns that wrapper for your own renderer. Neither helper takes locale or catalog options; use an explicit provider for translated or RTL tests.
Assert the element a rich message creates #
import { createLingo } from "@lingo.dev/react";
const l = createLingo("en");
const notice = l.rich("Read the <link>refund policy</link>.", {
context: "Purchase notice linking to the refund policy",
tags: { link: (children) => <a href="/refund-policy">{children}</a> },
});
const noticeMarkup = renderToStaticMarkup(<p>{notice}</p>);
assert.equal(noticeMarkup, '<p>Read the <a href="/refund-policy">refund policy</a>.</p>');Repeat this with the actual target template. Check the destination and visible text together. Then remove the callback and verify that the unmatched tag appears literally, so a broken translation cannot be mistaken for a working interaction.
Cover grammar, direction, and time deliberately #
For plurals, render counts that exercise the target language's categories. For selects, include an unknown application value and check other. For RTL, provide locale="ar" and a known Arabic map, render the component's direction attribute, and assert dir="rtl". Test the document shell separately if it is responsible for <html lang> and <html dir>.
Use fixed timestamps and explicit timezones in date tests. Relative-time tests need a fixed offset or reference instant. These cases test local rendering; a hydration test must also render through the framework's client path.
A passing rendering test means the supplied template works for those inputs. Inspect retrieved catalog content and use an appropriate quality measurement when the question is whether the translation preserves meaning.