@i18n-micro/test-utils
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-dev
Quick 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 locale
See the testing guide and the example/ Nuxt app.
License
MIT License. See the LICENSE file for details.
Author
- Name: s00d
- Email: Virus191288@gmail.com
- Website: https://s00d.github.io/
Related Packages
nuxt-vitest-shared-app
Run Nuxt component tests in Vitest on one shared app per worker, with per-test c...
546
0
Version History
| Version | Released | License |
|---|---|---|
| 1.2.19 | MIT | |
| 1.2.18 | MIT | |
| 1.2.17 | MIT | |
| 1.2.16 | MIT | |
| 1.2.15 | MIT | |
| 1.2.14 | MIT | |
| 1.2.12 | MIT | |
| 1.2.11 | MIT | |
| 1.2.10 | MIT | |
| 1.2.9 | MIT | |
| 1.2.8 | MIT | |
| 1.2.7 | MIT | |
| 1.2.6 | MIT | |
| 1.2.2 | MIT | |
| 1.2.1 | MIT | |
| 1.2.0 | MIT | |
| 1.1.1 | MIT | |
| 1.1.0 | MIT | |
| 1.0.8 | MIT | |
| 1.0.7 | MIT |