Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing with vue-18n and script setup #302

Open
Explicit12 opened this issue Mar 8, 2023 · 5 comments
Open

Testing with vue-18n and script setup #302

Explicit12 opened this issue Mar 8, 2023 · 5 comments
Labels
bug Something isn't working

Comments

@Explicit12
Copy link

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

@Explicit12 Explicit12 added the bug Something isn't working label Mar 8, 2023
@fabio984
Copy link

I'm using Vue 3 with script setup (Single File Component) and Jest.

my_i18n_lib_path/i18n.ts:

import { I18n, createI18n } from 'vue-i18n';

const i18n: I18n = createI18n({
	legacy: false,
	locale: 'en',
	messages: {},
	missing: (locale, key) =>
		console.error('Translation not found! Locale:', locale, 'translation key:', key),
});

the SFC myComponent.vue file:

import { useI18n } from 'vue-i18n';

<script setup lang="ts">
const { t } = useI18n();

then the test:

import { shallowMount } from '@vue/test-utils';
import { i18n } from 'my_i18n_lib_path/i18n';

describe('myComponent.vue', () => {
	it('Should eat onions like apples', () => {
		// Arrange
		const wrapper = shallowMount(myComponent, {
			global: {
				plugins: [i18n],
				mocks: { t: (key: string) => key },
			},
		});
	});
});

Hope this helps someone, or if it can be improved, just share.

@Korny666
Copy link

Korny666 commented Jul 5, 2023

Has anyone a solution yet? - Struggling with the same problem

@PanDymitr
Copy link

PanDymitr commented Sep 18, 2023

Vitest setup files to the rescue:

// vite.config.ts
export default defineConfig({
  test: {
    root: './src/',
    setupFiles: ['./setup.ts'],
  }
})
// src/plugins/i18n.ts
import { createI18n } from 'vue-i18n';

export default createI18n({
  // ...
});
// src/setup.ts
import i18n from '@/plugins/i18n';
import { config } from '@vue/test-utils';

// @ts-expect-error type
if (!globalThis.defined) {
  config.global.plugins = [i18n];
  // @ts-expect-error type
  globalThis.defined = true;
}

@cbush06
Copy link

cbush06 commented Feb 27, 2024

I added config.global.plugins = [VueI18n] to setup.ts.

Then, I used this in my test:

        const { getByTestId } = render({
            ...ResultsForAttemptVue,
            setup() {
                return { t: useI18n().t };
            },
        });

This seems very hackish, but nothing else would work.

@isimehmeti-bga
Copy link

if anyone still having the issue

import { vi } from 'vitest';

vi.mock('vue-i18n', () => ({
  useI18n: () => ({
    t: (key: string) => key,
    d: (key: string) => key,
  }),
}));

fond solution from here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

6 participants