Skip to content
New issue

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

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

Already on GitHub? # to your account

How to mock mitt to run tests in Jest? #199

Open
victormedeiros1 opened this issue Jul 8, 2024 · 1 comment
Open

How to mock mitt to run tests in Jest? #199

victormedeiros1 opened this issue Jul 8, 2024 · 1 comment

Comments

@victormedeiros1
Copy link

victormedeiros1 commented Jul 8, 2024

Hi, I'm trying to create a mock of mitt for the unit tests of my components in Vue 3, however, I'm not succeeding, can you help?

mitt instance file (in root):

import mitt from 'mitt'

export const emitirGlobal = mitt()

In my componente A:

import { emitirGlobal } from "@/mitt"

{ ... }

emitirGlobal.emit("recarregarAtividades")

In my componente B:

import { emitirGlobal } from "@/mitt"

{...}

emitirGlobal.on("recarregarAtividades", async (): Promise<void> => {
  await recarregarAtividades()
})

My archive jest-setup.ts:

import '@testing-library/jest-dom'
import { config } from '@vue/test-utils'
import i18n from '@/i18n'

config.global.plugins = [i18n]

jest.mock('mitt', () => {
  const emitirGlobal = {
    on: jest.fn(),
    emit: jest.fn(),
  }
  return { emitirGlobal }
})

global.console = {
  ...console,
  warn: jest.fn(),
  error: jest.fn(),
}

module.exports = {
  // Outras configurações do Jest...
  moduleNameMapper: {
    '^@/(.*)$': '<rootDir>/src/$1',
  },
  setupFilesAfterEnv: ['<rootDir>/jest-setup.ts'],
};

I've also tried adding "mitt" to Jest's "plugins", but it didn't work:

import '@testing-library/jest-dom'
import { config } from '@vue/test-utils'
import i18n from '@/i18n'
import mitt from 'mitt'

config.global.plugins = [i18n, mitt()]

global.console = {
  ...console,
  warn: jest.fn(),
  error: jest.fn(),
}
@stoyan-scava
Copy link

https://youtu.be/GOYmouFrGrE?si=Qmz7oD3fUpYuQGuo&t=3272

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants