question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot use vue-i18n plugin with current rendering API

See original GitHub issue

The goal

I want to get i18n capabilities in my test using this library, with the popular community vue solution for i18n: vue-i18n.

The issue

In order to make i18n possible using that package, it is currently not possible as far as I can tell. The idea would be that you could pass it to the mountOptions (i.e. second argument of render function), it will not work because it depends on the localVue/Vue being available.

One may think, why not just use the third callback argument and register it there? Agree with you! But there is the problem that you must create the i18n like so in @vue/test-utils:

const localVue = createLocalVue();
localVue.use(VueI18n);
const i18n = new VueI18n({ locale: "en" });

it("Should translate", () => {
  const wrapper = mount(App, { i18n, localVue });
  expect(wrapper.text()).toEqual("Hello World!");
});

The thing to note is that the i18n variable passed into mount. In the current setup of the function, it is impossible to pass this i18n variable in the second argument of render without having a localVue instance available.

Perhaps the third argument could be changed to return a value that is passed in as additional options so that they fall through to the mountOptions? i.e. I imagine passing it as:

import { render as r } from '@testing-library/vue';
import VueI18n from 'vue-i18n';

const messages = {
  en: {
    permission_groups_add_member: 'Add',
  },
};

export function render(ui, { ...opts } = {}) {
  return r(
    ui,
    {
      ...opts,
    },
    vue => {
      vue.use(VueI18n);
      const i18n = new VueI18n({ locale: 'en', messages });
      return { i18n };
    },
  );
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
brennjcommented, Jun 28, 2019

Thanks for the merge on this, super delighted. 🔥

1reaction
brennjcommented, Jun 25, 2019

Hi @afontcu I tried my hand at a PR for this, let me know if this would cause any consequences or if there’s anything additional you think could be an issue?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Vue.js 3 - Error while using Vue I18n plugin in vue - Cannot ...
I've just started working with Vue.js and learning it through some online code snippet and tutorials. I'm trying to implement ...
Read more >
Composition API - Vue I18n
We have been describing the features of Vue I18n using the Legacy API, which is compatible with vue-i18n v8.x. Now let's take a...
Read more >
The Ultimate Vue Localization Guide | Phrase
In this hands-on guide, we'll use Vue I18n to internationalize a little demo ... Mushahed, is based on data from the Open Notify...
Read more >
Manage Vue i18n with Typescript - Medium
It's a very handy and easy-to-use library to manage a multi-language app. Vue I18n. Vue I18n is internationalization plugin for Vue.js.
Read more >
Vue i18n: Building a multi-language app with locale switcher
I've chosen to use the default preset for this app (Babel and ESLint). Lastly, cd into the app folder and install the Vue...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found