Skip to content

This notebook belongs to

Sadat Jubayer

Senior Software Engineer at Premio

If found, please return to hello@jubayer.dev

Vol. 7Seven years of building

Started
January 2020
Everyday tools
TypeScript, Vue, React, Next.js, NestJS, React Native
Education
B.Sc. CSSE, AIUB, 2020
2 min readTesting

Choose jsdom or Browser Mode in Vitest 4

Keep DOM assertions in jsdom and move layout checks to a Chromium project in Vitest 4, using one toolbar test to see the difference.

You want a test that fails if two toolbar buttons stop wrapping. A jsdom test can find both buttons and read their labels. It gives their layout rectangles zeroes, so it cannot tell you whether the second button moved to the next row.

Give layout tests a browser project

Keep your DOM tests in jsdom and move the layout assertion to a Chromium project. Vitest 4 made Browser Mode stable and uses a separate provider package to launch the browser.

In a project with Vitest 4 and compatible Vite, add jsdom and the matching @vitest/browser-playwright version. Install Chromium for that provider with pnpm exec playwright install chromium. Save this config as vitest.config.mjs.

import { defineConfig } from 'vitest/config'
import { playwright } from '@vitest/browser-playwright'

export default defineConfig({
  test: {
    projects: [
      {
        test: {
          name: 'dom',
          include: ['**/*.test.js'],
          environment: 'jsdom',
        },
      },
      {
        test: {
          name: 'browser',
          include: ['**/*.browser.js'],
          browser: {
            enabled: true,
            headless: true,
            provider: playwright(),
            instances: [{ browser: 'chromium' }],
          },
        },
      },
    ],
  },
})

Files ending in .test.js now go to jsdom, and .browser.js files run in headless Chromium.

diagram

One test command, two environments

The label test runs in jsdom, and the wrap test uses Chromium's layout.

If the browser project fails before it runs a test, check the provider version and the Chromium install. Keep each file pattern exclusive so the same test does not run in both projects. The Vitest 4 browser guide shows how to configure provider instances.

Move the assertion that needs layout

Save this as toolbar.browser.js. The toolbar is 140 pixels wide, and each of its two buttons needs 100 pixels.

import { expect, test } from 'vitest'

test('the toolbar wraps at 140 pixels', () => {
  const toolbar = document.createElement('div')
  toolbar.style.cssText = 'display:flex; flex-wrap:wrap; width:140px'
  const buttons = ['Save', 'Cancel'].map((label) => {
    const button = document.createElement('button')
    button.textContent = label
    button.style.flex = '0 0 100px'
    return button
  })
  toolbar.append(...buttons)
  document.body.append(toolbar)
  try {
    const [save, cancel] = buttons.map((button) => button.getBoundingClientRect())
    expect(cancel.top).toBeGreaterThan(save.top)
  } finally {
    toolbar.remove()
  }
})

Chromium places Cancel lower than Save, which makes the assertion pass.

pnpm exec vitest run --project browser

Vitest reports the toolbar wrap test under the Chromium project.

Try the same assertion in a file named toolbar-layout.test.js. Both top values read as 0 in jsdom, and the test fails. Instead of mocking those values to make it pass, let the browser measure the toolbar you rendered.

Use the browser project for questions that need a rendering engine, such as whether a real stylesheet makes the buttons wrap. Keep the label assertion with the DOM logic in jsdom. In your component suite, bring the component's real CSS into the browser test and measure the same two elements at a width at which you expect them to wrap.