Package APIs

@i18n-micro/test-utils

Exported API of @i18n-micro/test-utils, generated from the source.

Testing helpers for Nuxt I18n Micro (translations, locales, formatting mocks).

import { … } from '@i18n-micro/test-utils'

Functions

_t

function _t(
  route: unknown,
): (key: TranslationKey, params?: Params, defaultValue?: string | null) => Translation

See TranslationKey · Params · Translation

_ts

function _ts(
  route: unknown,
): (key: TranslationKey, params?: Params, defaultValue?: string | null) => string

See TranslationKey · Params

createFakeI18n

Build a useI18n()-shaped mock for mockNuxtImport('useI18n', …). Includes $… methods, bare aliases (t / $t), helper, and test setters.

Pass isolated: true (or use createIsolatedFakeI18n / setupNuxtI18nMock) for a private translation cache.

function createFakeI18n(options?: CreateFakeI18nOptions): { … }

See CreateFakeI18nOptions

createIsolatedFakeI18n

Isolated fake + bound helpers (does not touch the shared module cache).

function createIsolatedFakeI18n(options?: CreateFakeI18nOptions): IsolatedFakeI18n

See CreateFakeI18nOptions · IsolatedFakeI18n

defaultLocale

function defaultLocale(): string | undefined

getI18nConfig

function getI18nConfig(): { … }
locales
Locale[] required
defaultLocale
string | undefined required
strategy
TestStrategy required

getLocale

function getLocale(): string

getLocaleName

function getLocaleName(): string | null

getLocales

function getLocales(): Locale[]

See Locale

getRouteName

function getRouteName(_route?: unknown, _locale?: string): string

has

function has(key: string, routeContext?: unknown): boolean

loadPageTranslations

function loadPageTranslations(
  locale: string,
  page: string,
  translations: Translations,
): Promise<void>

See Translations

localePath

function localePath(to: unknown, loc?: string): string

localeRoute

function localeRoute(to: unknown, loc?: string): { … }
name
string | symbol | undefined
path
string required

mergeTranslations

function mergeTranslations(newTranslations: Translations): void

See Translations

resetI18n

Reset the shared mock state and clear the translation cache. Call from beforeEach so tests do not leak dictionaries/locales. For isolated harnesses, use the reset returned by setupNuxtI18nMock / createIsolatedFakeI18n.

function resetI18n(options?: ResetI18nOptions): void

See ResetI18nOptions

resolveTranslations

function resolveTranslations(routeContext?: unknown): Translations

See Translations

setDefaultLocale

function setDefaultLocale(val: string | undefined): void

setI18nRouteParams

function setI18nRouteParams(value: unknown): unknown

setLocale

function setLocale(val: string): void

setLocaleName

function setLocaleName(val: string | null): void

setLocales

function setLocales(val: Locale[]): void

See Locale

setMissingHandler

function setMissingHandler(handler: MissingHandler | null): void

See MissingHandler

setRouteName

function setRouteName(val: string): void

setTranslation

function setTranslation(key: string, value: unknown): void

setTranslationsFromJson

function setTranslationsFromJson(
  locale: string,
  translations: Record<string, unknown>,
): Promise<void>

setupNuxtI18nMock

One-call harness for Nuxt unit tests: isolated fake, optional beforeEach reset, and a useI18n factory for mockNuxtImport.

function setupNuxtI18nMock(options?: SetupNuxtI18nMockOptions): NuxtI18nMockHarness
import { setupNuxtI18nMock } from '@i18n-micro/test-utils'
import { mockNuxtImport } from '@nuxt/test-utils/runtime'
import { beforeEach, vi } from 'vitest'

const { i18n, useI18n, setTranslationsFromJson } = setupNuxtI18nMock({
  spy: vi.fn,
  beforeEach,
  translations: { welcome: 'Welcome' },
})

mockNuxtImport('useI18n', () => useI18n)
export { i18n, setTranslationsFromJson }

See SetupNuxtI18nMockOptions · NuxtI18nMockHarness

switchLocale

function switchLocale(val: string): void

switchLocalePath

function switchLocalePath(loc: string): string

switchLocaleRoute

function switchLocaleRoute(loc: string): { … }
path
string required
name
string required

switchRoute

function switchRoute(route: unknown, toLocale?: string): void

t

function t(
  key: string,
  params?: TranslateParams,
  defaultValue?: string | null,
  routeContext?: unknown,
): CleanTranslation

See TranslateParams · CleanTranslation

tc

function tc(key: string, count: number | Params, defaultValue?: string): string

See Params

td

function td(value: string | number | Date, options?: Intl.DateTimeFormatOptions): string
function td(
  value: string | number | Date,
  key: string,
  overrides?: Intl.DateTimeFormatOptions,
): string
function td(
  value: string | number | Date,
  key: string,
  locale: string,
  overrides?: Intl.DateTimeFormatOptions,
): string

tdr

function tdr(
  value: string | number | Date,
  options?: Intl.RelativeTimeFormatOptions,
): string

tn

function tn(value: number, options?: Intl.NumberFormatOptions): string
function tn(value: number, key: string, overrides?: Intl.NumberFormatOptions): string
function tn(
  value: number,
  key: string,
  locale: string,
  overrides?: Intl.NumberFormatOptions,
): string

ts

function ts(
  key: string,
  params?: TranslateParams,
  defaultValue?: string,
  routeContext?: unknown,
): string

See TranslateParams

Classes

I18nTestContext

Stateful i18n mock. Translation, plural and format calls are BaseI18n's own: a test answers what the app answers. What is added here is the test state: setters, seeds and path stubs. The package's default singleton backs the module-level helpers (t, resetI18n, …).

class I18nTestContext extends BaseI18n {
  constructor(options: CreateI18nTestContextOptions = {})
}

See BaseI18n · CreateI18nTestContextOptions

Constants

createI18nTestContext

const createI18nTestContext: (options?: CreateI18nTestContextOptions) => I18nTestContext

See CreateI18nTestContextOptions · I18nTestContext

i18nStrategy

Minimal strategy stub so useI18n().$i18nStrategy does not throw.

const i18nStrategy: {
  getStrategy: () => TestStrategy;
  getDefaultLocale: () => string;
  getLocales: () => Locale[];
  getLocalizedRouteNamePrefix: () => string;
  getRouteBaseName: () => null;
  pageLocales: () => null;
  isMetaDisabled: () => boolean;
  setRouter: () => void;
  shouldReturn404: () => null;
  getClientRedirect: () => null;
  resolveLocaleFromPath(path: string): string | null;
  getLocaleFromPath(path: string): string | null;
  getCurrentLocale: () => string;
  getPluginRouteName: () => string;
  getCurrentLocaleName: () => string | null;
  localeRoute(targetLocale: string, routeOrPath: unknown): {
    path: string;
    fullPath: string;
  };
  switchLocaleRoute(toLocale: string, route?: unknown): {
    path: string;
    fullPath: string;
  };
}

See TestStrategy · Locale

i18nUtils

const i18nUtils: {
  t: (key: TranslationKey, params?: TranslateParams, defaultValue?: string | null, routeContext?: unknown) => CleanTranslation;
  tc: (key: TranslationKey, count: number | Params, defaultValue?: string) => string;
  ts: (key: TranslationKey, params?: TranslateParams, defaultValue?: string, routeContext?: unknown) => string;
  tn: {
    (value: number, options?: Intl.NumberFormatOptions): string;
    (value: number, key: string, overrides?: Intl.NumberFormatOptions): string;
    (value: number, key: string, locale: string, overrides?: Intl.NumberFormatOptions): string;
  };
  td: {
    (value: Date | number | string, options?: Intl.DateTimeFormatOptions): string;
    (value: Date | number | string, key: string, overrides?: Intl.DateTimeFormatOptions): string;
    (value: Date | number | string, key: string, locale: string, overrides?: Intl.DateTimeFormatOptions): string;
  };
  tdr: (value: Date | number | string, options?: Intl.RelativeTimeFormatOptions) => string;
  has: (key: TranslationKey, routeContext?: unknown) => boolean;
  resetI18n: (options?: ResetI18nOptions) => void;
  createFakeI18n: (options?: CreateFakeI18nOptions) => {
    $i18nStrategy: {
      getStrategy: () => TestStrategy;
      getDefaultLocale: () => string;
      getLocales: () => Locale[];
      getLocalizedRouteNamePrefix: () => string;
      getRouteBaseName: () => null;
      pageLocales: () => null;
      isMetaDisabled: () => boolean;
      setRouter: () => void;
      shouldReturn404: () => null;
      getClientRedirect: () => null;
      resolveLocaleFromPath(path: string): string | null;
      getLocaleFromPath(path: string): string | null;
      getCurrentLocale: () => string;
      getPluginRouteName: () => string;
      getCurrentLocaleName: () => string | null;
      localeRoute(targetLocale: string, routeOrPath: unknown): {
        path: string;
        fullPath: string;
      };
      switchLocaleRoute(toLocale: string, route?: unknown): {
        path: string;
        fullPath: string;
      };
    };
    $getI18nConfig: () => {
      locales: Locale[];
      defaultLocale: string | undefined;
      strategy: TestStrategy;
    };
    $getLocale: () => string;
    $setLocale: (val: string) => void;
    $getLocaleName: () => string | null;
    $setLocaleName: (val: string | null) => void;
    $getLocales: () => Locale[];
    $setLocales: (val: Locale[]) => void;
    $defaultLocale: () => string | undefined;
    $setDefaultLocale: (val: string | undefined) => void;
    $getRouteName: (_route?: unknown, _locale?: string) => string;
    $setRouteName: (val: string) => void;
    $t: (key: TranslationKey, params?: TranslateParams, defaultValue?: string | null, routeContext?: unknown) => CleanTranslation;
    $_t: (route: unknown) => (key: TranslationKey, params?: Params, defaultValue?: string | null) => Translation;
    $ts: (key: TranslationKey, params?: TranslateParams, defaultValue?: string, routeContext?: unknown) => string;
    $_ts: (route: unknown) => (key: TranslationKey, params?: Params, defaultValue?: string | null) => string;
    $tc: (key: TranslationKey, count: number | Params, defaultValue?: string) => string;
    $tn: {
      (value: number, options?: Intl.NumberFormatOptions): string;
      (value: number, key: string, overrides?: Intl.NumberFormatOptions): string;
      (value: number, key: string, locale: string, overrides?: Intl.NumberFormatOptions): string;
    };
    $td: {
      (value: Date | number | string, options?: Intl.DateTimeFormatOptions): string;
      (value: Date | number | string, key: string, overrides?: Intl.DateTimeFormatOptions): string;
      (value: Date | number | string, key: string, locale: string, overrides?: Intl.DateTimeFormatOptions): string;
    };
    $tdr: (value: Date | number | string, options?: Intl.RelativeTimeFormatOptions) => string;
    $has: (key: TranslationKey, routeContext?: unknown) => boolean;
    $resolveTranslations: (routeContext?: unknown) => Translations;
    $setTranslation: (key: TranslationKey, value: unknown) => void;
    $mergeTranslations: (newTranslations: Translations) => void;
    $switchLocaleRoute: (loc: string) => {
      path: string;
      name: string;
    };
    $switchLocalePath: (loc: string) => string;
    $switchLocale: (val: string) => void;
    $switchRoute: (route: unknown, toLocale?: string) => void;
    $localeRoute: (to: unknown, loc?: string) => {
      name?: string | symbol | undefined;
      path: string;
    };
    $localePath: (to: unknown, loc?: string) => string;
    $setI18nRouteParams: (value: unknown) => unknown;
    $loadPageTranslations: (locale: string, page: string, translations: Translations) => Promise<void>;
    $setMissingHandler: (handler: MissingHandler | null) => void;
    $setTranslationsFromJson: (locale: string, translations: Record<string, unknown>) => Promise<void>;
    helper: TranslationHelper;
  };
  createIsolatedFakeI18n: (options?: CreateFakeI18nOptions) => IsolatedFakeI18n;
  setupNuxtI18nMock: (options?: SetupNuxtI18nMockOptions) => NuxtI18nMockHarness;
  createI18nTestContext: (options?: CreateI18nTestContextOptions) => I18nTestContext;
  setTranslationsFromJson: (locale: string, translations: Record<string, unknown>) => Promise<void>;
  loadPageTranslations: (locale: string, page: string, translations: Translations) => Promise<void>;
  setMissingHandler: (handler: MissingHandler | null) => void;
  getI18nConfig: () => {
    locales: Locale[];
    defaultLocale: string | undefined;
    strategy: TestStrategy;
  };
  i18nStrategy: {
    getStrategy: () => TestStrategy;
    getDefaultLocale: () => string;
    getLocales: () => Locale[];
    getLocalizedRouteNamePrefix: () => string;
    getRouteBaseName: () => null;
    pageLocales: () => null;
    isMetaDisabled: () => boolean;
    setRouter: () => void;
    shouldReturn404: () => null;
    getClientRedirect: () => null;
    resolveLocaleFromPath(path: string): string | null;
    getLocaleFromPath(path: string): string | null;
    getCurrentLocale: () => string;
    getPluginRouteName: () => string;
    getCurrentLocaleName: () => string | null;
    localeRoute(targetLocale: string, routeOrPath: unknown): {
      path: string;
      fullPath: string;
    };
    switchLocaleRoute(toLocale: string, route?: unknown): {
      path: string;
      fullPath: string;
    };
  };
  getLocale: () => string;
  setLocale: (val: string) => void;
  getLocaleName: () => string | null;
  setLocaleName: (val: string | null) => void;
  getLocales: () => Locale[];
  setLocales: (val: Locale[]) => void;
  defaultLocale: () => string | undefined;
  setDefaultLocale: (val: string | undefined) => void;
  getRouteName: (_route?: unknown, _locale?: string) => string;
  setRouteName: (val: string) => void;
  _t: (route: unknown) => (key: TranslationKey, params?: Params, defaultValue?: string | null) => Translation;
  _ts: (route: unknown) => (key: TranslationKey, params?: Params, defaultValue?: string | null) => string;
  mergeTranslations: (newTranslations: Translations) => void;
  resolveTranslations: (routeContext?: unknown) => Translations;
  setTranslation: (key: TranslationKey, value: unknown) => void;
  switchLocaleRoute: (loc: string) => {
    path: string;
    name: string;
  };
  switchLocalePath: (loc: string) => string;
  switchLocale: (val: string) => void;
  switchRoute: (route: unknown, toLocale?: string) => void;
  localeRoute: (to: unknown, loc?: string) => {
    name?: string | symbol | undefined;
    path: string;
  };
  localePath: (to: unknown, loc?: string) => string;
  setI18nRouteParams: (value: unknown) => unknown;
}

See TranslationKey · TranslateParams · CleanTranslation · Params · ResetI18nOptions · CreateFakeI18nOptions · TestStrategy · Locale · Translation · Translations · MissingHandler · TranslationHelper · IsolatedFakeI18n · SetupNuxtI18nMockOptions · NuxtI18nMockHarness · CreateI18nTestContextOptions · I18nTestContext

Types

CreateFakeI18nOptions

interface CreateFakeI18nOptions extends CreateI18nTestContextOptions
isolated
boolean
A context of its own instead of the module's shared one.
spy
SpyFn
Wrap each method (e.g. vi.fn). Defaults to identity — no Vitest dependency.

See CreateI18nTestContextOptions

CreateI18nTestContextOptions

type CreateI18nTestContextOptions = ResetI18nOptions

See ResetI18nOptions

Getter

type Getter = (key: TranslationKey, params?: Record<string, string | number | boolean>, defaultValue?: string) => unknown

See TranslationKey

IsolatedFakeI18n

interface IsolatedFakeI18n
i18n
ReturnType<I18nTestContext['createFake']> required
context
I18nTestContext required
reset
I18nTestContext['reset'] required
setTranslationsFromJson
I18nTestContext['setTranslationsFromJson'] required

Locale

interface Locale
code
LocaleCode required
disabled
boolean
iso
string
dir
'ltr' | 'rtl' | 'auto'
displayName
string
baseUrl
string
baseDefault
boolean

LocaleCode

type LocaleCode = string

NuxtI18nMockHarness

interface NuxtI18nMockHarness extends IsolatedFakeI18n
useI18n
() => ReturnType<I18nTestContext['createFake']> required
Replacement for Nuxt useI18n. Pass to: mockNuxtImport('useI18n', () => useI18n) (keep mockNuxtImport in your setup file so @nuxt/test-utils can transform it).

See IsolatedFakeI18n

ResetI18nOptions

interface ResetI18nOptions
locale
string
defaultLocale
string | undefined
localeName
string | null
locales
Locale[]
routeName
string
strategy
TestStrategy
translations
Record<string, unknown>
Seed the active route chunk for locale after reset.
messages
Record<string, Record<string, unknown>>
Seed route chunks per locale code after reset.

SetupNuxtI18nMockOptions

interface SetupNuxtI18nMockOptions extends CreateFakeI18nOptions
beforeEach
(fn: () => void) => void
Vitest beforeEach. When provided, registers an auto-reset that reseeds factory translations / messages.
autoReset
boolean
Defaulttrue when `beforeEach` is passed

See CreateFakeI18nOptions

SpyFn

type SpyFn = <T>(fn: T) => T

TestStrategy

type TestStrategy =
  | 'prefix'
  | 'prefix_except_default'
  | 'prefix_and_default'
  | 'no_prefix'

Generated from the TypeScript sources and their JSDoc by pnpm run docs:generate. Back to all packages · Integration guides