Skip to content

Testing with vue-18n and script setup #302

Open
@Explicit12

Description

@Explicit12

I use vue-i18n in my component, which is written using script setup syntax. When I try to render component in my test I'm getting the error
TypeError: $setup.t is not a function

The test:

import { describe, test } from "vitest";
import { render } from "@testing-library/vue";
import { createI18n } from "vue-i18n";

import TheHeader from "@/components/TheHeader.vue";

const i18n = createI18n({
  legacy: false,
  locale: "en",
  messages: { ... }
});

describe("TheHeader", () => {
  test("Toggles the search input", async () => {
    const wrapper = render(TheHeader, {
      global: {
        plugins: [i18n],
      },
    });
  });
});

I've solved this problem in another test by this way:

const i18n = createI18n({
 legacy: false,
 locale: "en",
 messages: {
   en: {
     error: "Error",
   },
   uk: {
     error: "Помилка",
   },
   ru: {
     error: "Ошибка",
   },
 },
});

const wrapper = render(
 {
   ...TheError,
   $setup() {
     const { t } = useI18n();
     return { t };
   },
 },
 {
   props: { message },
   global: {
     plugins: [i18n],
   },
 },
);

But It doesn't work in this case. I've seen same problem in stack overflow several times, but no answers , so I consider it as bug.
Component I try to test: https://github.com/Explicit12/utube/blob/main/src/components/TheHeader.vue

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions