Skip to content

Cannot use vue-i18n plugin with current rendering API #47

Closed
@brennj

Description

@brennj

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 };
    },
  );
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions