vitest
A blazing fast test runner powered by Vite.
Features
- Vite's config, transformers, resolvers, and plugins. Powered by vite-node
- Jest Snapshot
- Chai for assertions
- Sinon for mocking
- Async suite / test, top level await
- ESM friendly
- Out-of-box TypeScript support
- Suite and Test filtering (skip, only, todo)
import { it, describe, expect, assert } from 'vitest'
describe('suite name', () => {
it('foo', () => {
assert.equal(Math.sqrt(4), 2)
})
it('bar', () => {
expect(1 + 1).eq(2)
})
it('snapshot', () => {
expect({ foo: 'bar' }).toMatchSnapshot()
})
})
$ npx vitest
Configuration
vitest
will read your root vite.config.ts
when it present to match with the plugins and setup as your Vite app. If you want to it to have a different configuration for testing, you could either:
- Create
vitest.config.ts
, which will have the higher priority - Pass
--config
option to CLI, e.g. vitest --config ./path/to/vitest.config.ts
- Use
process.env.VITEST
to conditionally apply differnet configuration in vite.config.ts
To configure vitest
itself, add test
property in your Vite config
import { defineConfig } from 'vite'
export default defineConfig({
test: {
}
})
Filtering
Skipping suites and tasks
Use .skip
to avoid running certain suites or tests
describe.skip('skipped suite', () => {
it('task', () => {
assert.equal(Math.sqrt(4), 3)
})
})
describe('suite', () => {
it.skip('skipped task', () => {
assert.equal(Math.sqrt(4), 3)
})
})
Selecting suites and tests to run
Use .only
to only run certain suites or tests
describe.only('suite', () => {
it('task', () => {
assert.equal(Math.sqrt(4), 3)
})
})
describe('another suite', () => {
it('skipped task', () => {
assert.equal(Math.sqrt(4), 3)
})
it.only('task', () => {
assert.equal(Math.sqrt(4), 2)
})
})
Unimplemented suites and tests
Use .todo
to stub suites and tests that should be implemented
describe.todo('unimplemented suite')
describe('suite', () => {
it.todo('unimplemented task')
})
TODO
License
MIT License © 2021 Anthony Fu