包详细信息

vitest-canvas-mock

wobsoriano1.9mMIT1.1.2

🌗 A module used to mock canvas in Vitest.

mock, canvas, vitest, test

自述文件

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

更新日志

vitest-canvas-mock

1.1.2

Patch Changes

  • 44699e0: Fix missing types export

1.1.1

Patch Changes

  • 4dbcffb: Remove node import

1.1.0

Minor Changes

  • bc7dc3a: This project is now established as a direct fork of jest-canvas-mock, migrated to work with Vitest.

    Changes

    • Updated function name references and documentation to use Vitest terminology
    • Updated type definitions and comments to reference vitest-canvas-mock instead of jest-canvas-mock
    • No API changes - the function has always been setupVitestCanvasMock() in this fork

1.0.1

Patch Changes

  • 98d45ea: Export importMockWindow helper

1.0.0

Major Changes

  • 844d2fe: - Breaking: Peer dependency now requires Vitest 3.x or 4.x (previously allowed any version)
    • Added setupVitestCanvasMock() export function for manual setup (similar to jest-canvas-mock's setupJestCanvasMock())
    • Improved test coverage