Package detail

vitest-canvas-mock

wobsoriano1.9mMIT1.1.2

🌗 A module used to mock canvas in Vitest.

mock, canvas, vitest, test

readme

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

  1. Create a new setup file that imports vitest-canvas-mock or add it to an existing setup file.
// vitest.setup.ts
import 'vitest-canvas-mock'
  1. In your vitest.config.ts file, create a setupFiles array 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