Guide › Site quality

On this page

Testing

A component is HTML from the server plus, where it's interactive, a little JavaScript in the browser. Test the HTML with Vitest, the interactive parts in a browser with Playwright, and let clarusjs check catch type and template errors.

The examples on this page are the files this site runs in its own tests, so they work as shown.

Set up #

Install Vitest and add the clarusjs plugin to its config: the plugin compiles the .tscl files your tests import, the same way Vite does for your site.

sh
pnpm add -D vitest

vitest.config.ts

ts
// vitest.config.ts: the clarusjs plugin compiles the .tscl files your tests import.
import { defineConfig } from 'vitest/config'
import clarusjs from 'clarusjs/vite'

export default defineConfig({
  plugins: [clarusjs()],
  // Where your unit tests live; Playwright's e2e/*.spec.ts stay out of Vitest.
  test: { include: ['src/**/*.test.ts'] },
})

Keep Vitest's default node environment for these tests (no jsdom or happy-dom): the plugin gives a test the server side of a component, the one with render, when it runs where a server would.

Test what a component renders #

Import a component and call its render(props, slots, options): it returns the HTML the server would send. Check what matters to you: the text, the attributes, what's escaped.

greeting.tscl

tscl
{{-- A greeting: a name, a tone, and anything you put inside it. --}}
@input name: string
@input tone: 'plain' | 'loud' = 'plain'

<p class="greeting" [data-tone]="tone">Hello, {{ name }}<slot /></p>

greeting.test.ts

ts
// The testing guide's example (/guide/testing): import a component and check what it renders.
import { expect, test } from 'vitest'
import { render } from './greeting.tscl'

test('renders its input', async () => {
  const html = await render({ name: 'Ada' })
  expect(html).toContain('Hello, Ada')
  expect(html).toContain('data-tone="plain"')
})

test('escapes what it renders', async () => {
  const html = await render({ name: '<b>x</b>' })
  expect(html).toContain('&lt;b&gt;x&lt;/b&gt;')
  expect(html).not.toContain('<b>x</b>')
})

test('puts slot content at <slot />', async () => {
  expect(await render({ name: 'Ada' }, { default: '!' })).toContain('Hello, Ada!')
})

test('takes attributes written on its tag', async () => {
  // id, aria-* and data-* written on its tag arrive with the props; class comes in the third argument.
  const html = await render({ name: 'Ada', id: 'welcome', 'data-test': 'greeting' }, undefined, { class: 'hero' })
  expect(html).toMatch(/<p [^>]*class="[^"]*\bhero\b/)
  expect(html).toContain('id="welcome"')
  expect(html).toContain('data-test="greeting"')
})

Test islands in a browser #

An island's behaviour (clicks, typing, two-way binding, keyboard) runs in the browser, so test it in one. Playwright drives a real browser against your built site; @axe-core/playwright checks each page for accessibility problems while you're there (see Accessibility).

sh
pnpm add -D @playwright/test @axe-core/playwright
pnpm exec playwright install chromium

Point Playwright at your built site: it builds and serves it before the tests run.

playwright.config.ts

ts
// playwright.config.ts: build your site, serve it with vite preview, and run the tests in e2e/ against it.
import { defineConfig, devices } from '@playwright/test'

export default defineConfig({
  testDir: 'e2e',
  use: { baseURL: 'http://localhost:4173' },
  projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }],
  webServer: {
    command: 'vite build && vite preview --port 4173 --strictPort',
    url: 'http://localhost:4173',
  },
})

Then test what a visitor does. Here counter is an island on this site's home page; use one of yours:

e2e/counter.spec.ts

ts
// An island works when clicked, and the page passes axe. (counter is this site's island: use one of yours.)
import AxeBuilder from '@axe-core/playwright'
import { expect, test } from '@playwright/test'

test('counter: clicking counts up, and the page passes axe', async ({ page }) => {
  await page.goto('/')
  const counter = page.locator('cl-island[c="counter"]')
  await expect(counter).toHaveAttribute('data-cl-ready', '') // the island has attached
  await counter.getByRole('button').click()
  await expect(counter.getByRole('button')).toContainText('Clicked 1 time')

  const { violations } = await new AxeBuilder({ page }).withTags(['wcag2a', 'wcag2aa', 'wcag21aa', 'wcag22aa']).analyze()
  expect(violations).toEqual([])
})

In CI #

Run all three on every change:

sh
pnpm exec clarusjs check   # type and template errors, at file.tscl:line:col
pnpm exec vitest run       # what components render
pnpm exec playwright test  # islands and accessibility, in a browser

Not covered yet #

There's no helper yet for clicking an island without a browser: a component's browser code isn't available to Vitest. Test interactive behaviour with Playwright as above. If you'd use a faster way, say so in an issue.