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/test
bash
$ yarn add -D @playwright/test
bash
$ pnpm add -D @playwright/test
bash
$ 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.js
andutils.js
files. Wrap the preview code insidevite.js
in a conditional.
txt
├─ src/
| ├─ ...
| ├─ utils.js
| └─ vite.js
├─ ...
└─ playwright.config.js
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.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 '@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 test
bash
$ yarn test
bash
$ pnpm test
bash
$ bun test
TIP
For more details about Playwright refer to the Playwright docs.