Mock canvas
when running unit tests with vitest. Uses jest-canvas-mock.
npm install vitest-canvas-mock -D
- 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'
- In your
vitest.config.ts
file, create asetupFiles
array and add that file:
import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
setupFiles: ['./vitest.setup.ts'],
environment: 'jsdom',
deps: {
// vitest < 0.34
inline: ['vitest-canvas-mock'],
// >= 0.34
optimizer: {
web: {
include: ['vitest-canvas-mock']
}
}
},
// For this config, check https://github.com/vitest-dev/vitest/issues/740
// Vitest < 0.1.x
threads: false,
// >= 0.1.0
poolOptions: {
threads: {
singleThread: true,
},
},
environmentOptions: {
jsdom: {
resources: 'usable',
},
},
},
})
View mock strategy doc here.
View snapshots doc here
MIT