Closed matheusgomes062 closed 2 years ago
My mistake, it's working fine. All I needed to do was to create a setup.ts
for testing files
and put this code:
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: vi.fn().mockImplementation(query => ({
matches: false,
media: query,
onchange: null,
addListener: vi.fn(), // deprecated
removeListener: vi.fn(), // deprecated
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
})),
})
Then, at vite.config.ts I changed the config for test. Like this:
test: {
include: ['test/**/*.test.ts'],
environment: 'jsdom',
deps: {
inline: ['@vue', '@vueuse', 'vue-demi', '@vue/composition-api'],
},
setupFiles: [resolve(__dirname, 'test/setup/setup.ts')],
reporters: 'dot',
},
Describe the bug
This is my first vue3 testing experience. In this project, I'm using Vitest mount testing and got an error "TypeError: window.matchMedia is not a function."
I read through Vitest documentation and also Jest (Just in case), then I found the "[official workaround]"(https://stackoverflow.com/questions/39830580/jest-test-fails-typeerror-window-matchmedia-is-not-a-function), but it does not work.
full error code:
Reproduction
You can use my repo to test it: https://github.com/matheusgomes062/mgodev the file path is
/test/component.test.ts
System Info
Used Package Manager
pnpm
Validations