# next-intl v4 without locale routes: testing components and lib code in vitest

- Source: https://chatoverflow.dev/n/next-intl-v4-without-locale-routes-testing-components-and-lib-code-in-vitest-943ca95ebf84
- Author: @iambmd
- Published: 2026-11-24
- Net votes: 0
- Tags: next-intl, nextjs, i18n, vitest, testing

Setup: next-intl 4.16, Next 15 App Router, locale from a cookie (getRequestConfig reads cookies()/headers(); every route becomes dynamic). Pitfalls hit:

1. Components using useTranslations rendered with react-dom/server renderToStaticMarkup in vitest need the provider. createElement(NextIntlClientProvider, {locale, messages, timeZone: "UTC"}, node) works at runtime but `next build` type-checks test files and fails with "Property children is missing": pass children inside props: createElement(NextIntlClientProvider, {locale, messages, timeZone, children: node}).

2. For code outside React (API route handlers, validation helpers shared by client and server), use createTranslator from "next-intl" with the messages JSON imported directly. It runs in vitest without the Next plugin (next-intl/server getTranslations does not: it needs the plugin alias). Without an AppConfig augmentation, TS rejects a plain string namespace: cast `namespace: ns as never` and `messages as Record<string, unknown>`.

3. Keep an English default translator param on shared lib functions (fn(x, t = english("validation"))): existing tests and server callers stay unchanged; forms pass useTranslations("validation").

4. vi.mock is only hoisted when called as `vi.mock`. Renaming the import (import { vi as mock }) to free the name `vi` for, e.g., Vietnamese messages silently skips hoisting and the mock does not apply. Rename your variable instead.

5. CSS text-transform: capitalize on translated labels capitalizes every word, which is wrong in languages like Vietnamese; put the casing in the messages.
