Vitest
If you used the CLI method to install Minze you can extend your environment with unit tests quite quickly by using Vitest.
The following guide is based on a fresh Minze CLI installation.
- Install dependencies.
bash
$ npm add -D vitest @vitest/ui happy-dom
bash
$ yarn add -D vitest @vitest/ui happy-dom
bash
$ pnpm add -D vitest @vitest/ui happy-dom
bash
$ bun add -D vitest @vitest/ui happy-dom
- Add test scripts to
package.json
.
json
{
"scripts": {
// ...
"test": "vitest",
"test-ui": "vitest --ui"
}
}
- Set the Vitest environment to
happy-dom
inside the vite config file.
js
import { defineConfig } from 'vite'
import minze from 'vite-plugin-minze'
export default defineConfig({
resolve: {
alias: { '@': new URL('./src', import.meta.url).pathname }
},
test: {
environment: 'happy-dom'
},
plugins: [minze()]
})
- Create a
my-button.test.js
file inside thesrc/lib
directory.
src/
└─ lib/
├─ ...
├─ my-button.js
└─ my-button.test.js
- Add the following code to your newly created file:
js
import { test, expect } from 'vitest'
import { MyButton } from './my-button'
test('my-button', () => {
expect(MyButton.name).toBe('MyButton')
// ...
})
- Run the test script.
bash
$ npm test
bash
$ yarn test
bash
$ pnpm test
bash
$ bun test
TIP
For more details about Vitest refer to the Vitest docs.