developit / developit/mitt

How to mock mitt to run tests in Jest?

Open
#199 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
11.9k
Forks
488
PR merge metrics
No merged PRs in 30d

Description

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):
```js
import mitt from 'mitt'

export const emitirGlobal = mitt()

```

In my componente A:
```js
import { emitirGlobal } from "@/mitt"

{ ... }

emitirGlobal.emit("recarregarAtividades")
```

In my componente B:
```js
import { emitirGlobal } from "@/mitt"

{...}

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

My archive ```jest-setup.ts```:
```js
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: {
'^@/(.*)$': '/src/$1',
},
setupFilesAfterEnv: ['/jest-setup.ts'],
};

```

---------------------------------------------
I've also tried adding "mitt" to Jest's "plugins", but it didn't work:
```js
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(),
}
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.