@i18n-micro/test-utils
v1.2.15
Published
Testing helpers for Nuxt I18n Micro (translations, locales, formatting mocks).
Readme
Nuxt I18n Micro Test Utils
Nuxt unit-test mocks for useI18n() — translations, locale state, and lightweight path stubs. Pair with @nuxt/test-utils + Vitest.
Features
setupNuxtI18nMock— one-call isolated harness +beforeEachreseed (keepmockNuxtImportin your setup file)createFakeI18n/createIsolatedFakeI18n—useI18n()-shaped object ($t/t,helper, optionalvi.fnspies)- Factory options —
locales,translations,messages,strategy, … resetI18n— clear the shared dictionary cache between tests- Translation helpers —
t,tc,ts,mergeTranslations,resolveTranslations,setTranslation - Path stubs —
localePath/switchLocalePath(prefix strategies; does not mutate locale)
Installation
npm install @i18n-micro/test-utils --save-devQuick setup
// tests/unit-setup.ts
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,
})
mockNuxtImport('useI18n', () => useI18n)
export { i18n, setTranslationsFromJson }Load dictionaries in a test:
await setTranslationsFromJson('en', { welcome: 'Welcome' })
expect(i18n.$t('welcome')).toBe('Welcome')
expect(i18n.$localePath('/about')).toBe('/en/about')
expect(i18n.$switchLocalePath('de')).toBe('/de/about')
expect(i18n.$getLocale()).toBe('en') // switchLocalePath does not change localeSee the testing guide and the example/ Nuxt app.
License
MIT License. See the LICENSE file for details.
Author
- Name: s00d
- Email: [email protected]
- Website: https://s00d.github.io/
