Add Vitest
Here's how we added Vitest to Kitbook written here for maintenance reference.
pnpm -F kitbook add -D vitest
- turn on
globals
option and In-source testing by creating avitest.config.ts
file:
vitest.config.tsts
import {defineConfig } from 'vitest/config'export defaultdefineConfig ({test : {globals : true,includeSource : ['src/**/*.ts'],},})
- Acquaint Typescript:
tsconfig.jsonjson
{"compilerOptions": {"types": ["vitest/globals","vitest/importMeta"]}}
- Add an inline test
ts
if (import.meta.vitest) {test('capitalize turns bar into Bar', () => {expect(capitalize('bar')).toMatchInlineSnapshot('"Bar"')})}
- For a REPL like experience turn on VSCode autosave and run Vitest in update mode with the
-u
flag to have the inline snapshot update as you type.