How can I access the real i18n d()
function in Nuxt Test Utils, and is it bad practice to use it in tests?
#1316
Unanswered
loic-bellinger
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi everyone,
I’m writing tests for a Nuxt 3 project that uses @nuxtjs/i18n] (vue-i18n under the hood). In my components, I format dates with the i18n
d()
helper, for example:In my test suite (using Vitest + Nuxt Test Utils), I need to assert that the rendered
<time>
text matches the formatted date from i18n’s “short” datetime format. So far, I’ve been doing something like this:This works fine in my local tests. My question is twofold:
Is there a “better” or more idiomatic way to access the real i18n
d()
function inside Nuxt Test Utils?Right now, I’m calling
useNuxtApp()
within the test to grabi18n.d()
directly, but I haven’t found explicit guidance in the Nuxt Test Utils docs about this pattern. Is it considered bad practice?Should I avoid using
d()
in my tests and instead hard-code the expected formatted string?For example:
I worry that hard-coding the date format defeats the purpose of verifying i18n integration, and it also makes the test fragile if I ever tweak my locale or date-format configuration. On the other hand, relying directly on
d()
in the test means I’m basically testing “i18n’s implementation” rather than my own component’s behavior...Thanks in advance for any guidance or examples!
Beta Was this translation helpful? Give feedback.
All reactions