Registry / testing / playwright-expect

playwright-expect

JSON →
library0.1.2jsnpmunverified

playwright-expect is an assertion library designed for enhancing end-to-end testing with Playwright Test and Jest. Currently at version 0.1.2, it significantly extends the default `expect` API with a richer set of matchers compared to similar libraries like `expect-playwright`, aiming to provide a more comprehensive and robust testing experience. Key differentiators include an extensive collection of easy-to-use assertions, exhaustive failure messages with diff highlights, options for case-insensitive matching and trimming values before assertion, and the ability to automatically wait for expectations to succeed. It seamlessly integrates with both Playwright Test and Jest and ships with built-in TypeScript types for improved developer experience and autocompletion. The project maintains an active patch release cadence, focusing on stability and feature enhancements.

npm install playwright-expect
INSTALL
IMPORT
SIG · PLAYWRIGHT-EXPECT
P
playwright-expect
testingjavascriptv0.1.2
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

matchers
import { matchers } from 'playwright-expect';
import playwrightExpect from 'playwright-expect';
The primary export is a named `matchers` object, which is then passed to `expect.extend`.
matchers (CommonJS)
const { matchers } = require('playwright-expect');
const playwrightExpect = require('playwright-expect');
For CommonJS environments, destructure `matchers` from the `require` call.
expect.extend
import { expect } from '@playwright/test'; expect.extend(matchers);
This library extends Playwright's `expect` object. It's crucial to import `expect` from `@playwright/test` first and then apply the `matchers`.

Demonstrates how to install playwright-expect, extend Playwright's `expect` object, and use various custom matchers with options like `ignoreCase`, `trim`, and `timeout` in a Playwright Test setup.

import { test, expect } from '@playwright/test'; import { matchers } from 'playwright-expect'; // Extend Playwright's expect with playwright-expect's custom matchers expect.extend(matchers); test('has title and interactive elements', async ({ page }) => { await page.goto('https://playwright.dev/'); // Using toContainTitle matcher (from playwright-expect) with options await expect([page, 'title']).toContainTitle('Playwright', { ignoreCase: true, timeout: 5000 }); // Using toHaveText matcher (from playwright-expect) on a locator const header = page.locator('h1'); await expect(header).toHaveText('Playwright', { trim: true }); // Interact with an element and assert its new state await page.locator('.get-started').click(); // toHaveText can wait for the element to have the text await expect(page.locator('h1')).toHaveText('Installation', { timeout: 7000 }); // Verify a checkbox is not checked initially, then check it and assert const checkbox = page.locator('input[type="checkbox"]'); await expect(checkbox).not.toBeChecked(); await checkbox.check(); await expect(checkbox).toBeChecked(); });
Debug
Known issues
breakingVersion 0.0.9 removed specific Jest logic. Users who were relying on internal Jest integrations in earlier versions might experience breaking changes and need to reconfigure their Jest setups to use `expect.extend` directly with `playwright-expect`'s matchers.
fix
Ensure you are extending Jest's `expect` (if using Jest) or Playwright's `expect` (if using Playwright Test) with `expect.extend(matchers)` in your configuration file.
affects: >=0.0.9
gotchaEarly versions (0.0.3-0.0.5) had issues with type definitions and package configuration, leading to potential type errors like 'Cannot find module' or incorrect autocompletion. These were addressed in subsequent patch releases.
fix
Upgrade to version 0.0.6 or newer to resolve type resolution issues. Ensure your `tsconfig.json` or `jsconfig.json` is correctly configured for module resolution.
affects: <0.0.6
gotchaThis library is presented as a feature-rich alternative to `expect-playwright`. While both serve similar purposes, `playwright-expect` offers a distinct set of matchers and additional options. Users migrating from `expect-playwright` should review the API documentation carefully as not all matchers or behaviors may be identical.
fix
Consult the API documentation for `playwright-expect` to understand the available matchers and their specific options, especially when migrating from other assertion libraries.
affects: all
Errors
Common errors & fixes
ReferenceError: expect is not defined
This typically occurs in early versions or incorrect setup where `playwright-expect`'s matchers are used without extending the base `expect` object from the test runner.
fix
Ensure you have `import { expect } from '@playwright/test';` (or Jest equivalent) and `expect.extend(matchers);` in your Playwright configuration file or global setup.
TS2307: Cannot find module 'playwright-expect' or its corresponding type declarations.
This error often indicates a missing installation (`npm install -D playwright-expect`), or in older versions, issues with the package's `types` property or file extensions.
fix
Verify `playwright-expect` is installed. If using an older version (pre-0.0.6), consider upgrading. Check your `tsconfig.json` for correct `moduleResolution` settings.
Upgrade
Version history
0.1.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
playwright-expect — npm install playwright-expect · libregistry