Playwright
If you used the CLI method to install Minze you can extend your environment with e2e tests quite quickly by using Playwright.
The following guide is based on a fresh Minze CLI installation.
- Install dependencies.
bash
$ npm add -D @playwright/testbash
$ yarn add -D @playwright/testbash
$ pnpm add -D @playwright/testbash
$ bun add -D @playwright/test- Initialize Playwright.
bash
npx playwright install- Add test scripts to
package.json.
json
{
"scripts": {
// ...
"test": "npx playwright test",
"test-ui": "npx playwright test --ui",
"test-debug": "npx playwright test --debug"
}
}- Create and populate
playwright.config.jsandutils.jsfiles. Wrap the preview code insidevite.jsin a conditional.
txt
├─ src/
| ├─ ...
| ├─ utils.js // [!code ++]
| └─ vite.js // [!code warning]
├─ ...
└─ playwright.config.js // [!code ++]js
import { defineConfig } from '@playwright/test'
export default defineConfig({
webServer: {
command: 'vite -m testing',
port: 5173,
reuseExistingServer: !process.env.CI
}
})js
export async function setup(page, html) {
await page.goto('/')
await page.evaluate((html) => {
const app = document.querySelector('#app')
if (app) app.innerHTML = html
}, html)
}js
import './assets/vite.css'
import { modules, defineAll } from './main'
defineAll(modules)
if (import.meta.env.MODE !== 'testing') {
const previews = import.meta.glob('./*.html', { eager: true, query: '?raw', import: 'default' })
const preview = previews['./preview.dev.html'] ?? previews['./preview.html']
const app = document.querySelector('#app')
if (app) app.innerHTML = preview
}- Create a
my-button.test.jsfile inside thesrc/libdirectory.
src/
└─ lib/
├─ ...
├─ my-button.js
└─ my-button.test.js // [!code ++]- Add the following code to your newly created file:
js
import { test, expect } from '@playwright/test'
import { setup } from '@/utils'
test.describe('my-button', () => {
test.beforeEach(async ({ page }) => {
await setup(page, '<my-button>Hello Minze!</my-button>')
})
test('html', async ({ page }) => {
await expect(page.locator('my-button button')).toBeVisible()
})
// ...
})- Run the test script.
bash
$ npm testbash
$ yarn testbash
$ pnpm testbash
$ bun testTIP
For more details about Playwright refer to the Playwright docs.