vitest-canvas-mock
Mock canvas when running unit tests with vitest.
This is a fork of jest-canvas-mock, migrated to work with Vitest instead of Jest. All Jest-specific code has been replaced with Vitest equivalents.
Install
npm install vitest-canvas-mock -D
Usage
- Create a new setup file that imports
vitest-canvas-mockor add it to an existing setup file.
// vitest.setup.ts
import 'vitest-canvas-mock'
- In your
vitest.config.tsfile, create asetupFilesarray and add that file:
import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
setupFiles: ['./vitest.setup.ts'],
environment: 'jsdom',
},
})
Reset
If you reset the vitest mocks (for example, with vi.resetAllMocks()), you can call setupVitestCanvasMock() to re-create it.
import { setupVitestCanvasMock } from 'vitest-canvas-mock'
beforeEach(() => {
vi.resetAllMocks()
setupVitestCanvasMock()
})
Mock Strategy
View mock strategy doc here.
Snapshots
View snapshots doc here
License
MIT