Testing
import { render } from '@markless/vitest-browser';
import { expect, test } from 'vite-plus/test';
import { page } from 'vite-plus/test/browser';
import TodoItem from './TodoItem.tsrx';
test('a click marks it done', async () => {
await render(TodoItem);
const item = page.getByRole('button', { name: /buy milk/i });
await item.click();
await expect.element(item).toHaveTextContent('Buy milk: done');
});render(TodoItem) puts the component on a real page. page.getByRole finds the button the way a visitor would, by its role and name. click() presses it, and expect.element waits until it reads Buy milk: done.
A test clicks a real button
A browser test runs in Chromium, so a click runs your real handler. Press Run, then click the handler to break it and run again:
import { state } from '@markless/core';
export default function TodoItem() @{
let done = state(false);
<button
class={done ? 'todo done' : 'todo'}
>
Buy milk: {done ? 'done' : 'to do'}
</button>
}
import { render } from '@markless/vitest-browser';
import { expect, test } from 'vite-plus/test';
import { page } from 'vite-plus/test/browser';
import TodoItem from './TodoItem.tsrx';
test('a click marks it done', async () => {
await render(TodoItem);
const item = page.getByRole('button', { name: /buy milk/i });
await item.click();
await expect.element(item).toHaveTextContent('Buy milk: done');
});
○a click marks it done
Error: expect(element).toHaveTextContent() Expected element to have text content: Buy milk: done Received: Buy milk: to do
Press Run. Click the handler to break the component, then run it again.
- With the handler working, the row goes green.
- With it broken, the text never changes and the test fails at its
expect, printing what it expected and what it found.
The component is plain Markless
This is the TodoItem the test clicks. Change it, and watch the result:
- To do: Press Frodough’s to-do, the way the test’s
click()does. It reads what the test expects, and is stamped DONE. - To do: Rename the item to
Buy bread.
Rename it, and the test above fails too: it still expects Buy milk: done.
Set it up once
The starters don't include this yet. In an app from npm create markless, add the packages and a browser:
npm install -D @markless/vitest-browser playwright \
@vitest/browser-playwright@5.0.5
npx playwright install chromiumA new app's vite-plus 1.3.0 runs Vitest 5.0.5, and @vitest/browser-playwright must match it exactly.
import { markless } from '@markless/core/vite';
import { testSSR } from '@markless/vitest-browser/ssr-plugin';
import { playwright } from 'vite-plus/test/browser-playwright';
import { defineProject } from 'vite-plus/test/config';
export default defineProject({
plugins: [testSSR(), markless()],
test: {
include: ['src/**/*.browser.ts'],
browser: {
enabled: true,
headless: true,
provider: playwright(),
instances: [{ browser: 'chromium' }],
},
},
});Put both files in src/, and run npx vp test run --config vitest.config.ts.
expect.elementkeeps checking until the text matches or it times out, so a change that lands a moment after the click still passes.- Each test starts on an empty page: the package removes what it mounted after every test.
Server pages use renderSSR
If your app renders its pages on a server, use renderSSR(TodoItem) instead of render. It renders the component to HTML on the test server and puts that HTML in the page, and your test clicks the same button.
testSSR()must come beforemarkless()in the config. Without it,renderSSR()throwswas not transformed.- Write
renderSSR(TodoItem)by name, with a component from its own.tsrxfile and no props. A helper that getsrenderSSRpassed in as a value skips the rewrite and throws.
Diagnostics reads what Markless says when something fails to build.