expect-puppeteer is an assertion toolkit for Puppeteer that extends Jest's expect API with high-level, asynchronous matchers designed specifically for integration testing. It provides Puppeteer-specific assertions that automatically wait for elements to be present before performing actions, solving common timing issues in asynchronous web testing.
npm install expect-puppeteerimport { expect, setDefaultOptions, getDefaultOptions } from "expect-puppeteer";For CommonJS:
const { expect, setDefaultOptions, getDefaultOptions } = require("expect-puppeteer");// In setupFilesAfterEnv or Jest configuration
import "expect-puppeteer";
// Or for direct setup file import
import "expect-puppeteer/setup";// CommonJS setup
require("expect-puppeteer");
// Or for direct setup
require("expect-puppeteer/setup");import "expect-puppeteer"; // Sets up global expect enhancement
// Or with Jest setupFilesAfterEnv: ["expect-puppeteer"]
// Basic element assertions
await expect(page).toMatchElement("button", { text: "Submit" });
await expect(page).toClick("button", { text: "Submit" });
// Form interactions
await expect(page).toFill('input[name="email"]', "user@example.com");
await expect(page).toSelect('select[name="country"]', "United States");
// Content validation
await expect(page).toMatchTextContent("Welcome to our site");
await expect(page).not.toMatchTextContent("Error occurred");expect-puppeteer extends Jest's expect functionality with Puppeteer-specific matchers:
Core matchers for interacting with page elements including clicking, filling forms, and file uploads.
// Click on elements
function toClick(
instance: PuppeteerInstance,
selector: Selector | string,
options?: ToClickOptions
): Promise<void>;
// Fill form fields
function toFill(
instance: PuppeteerInstance,
selector: Selector | string,
value: string,
options?: ToFillOptions
): Promise<void>;
// Fill entire forms
function toFillForm(
instance: PuppeteerInstance,
selector: Selector | string,
values: Record<string, string>,
options?: ToFillFormOptions
): Promise<void>;
// Select options
function toSelect(
instance: PuppeteerInstance,
selector: Selector | string,
valueOrText: string,
options?: ToMatchElementOptions
): Promise<void>;
// Upload files
function toUploadFile(
instance: PuppeteerInstance,
selector: Selector | string,
filePaths: string | string[],
options?: ToMatchElementOptions
): Promise<void>;Matchers for asserting element presence, text content, and other element states.
// Assert element exists and return it
function toMatchElement(
instance: PuppeteerInstance,
selector: Selector | string,
options?: ToMatchElementOptions
): Promise<ElementHandle<Element>>;
// Assert text content matches
function toMatchTextContent(
instance: PuppeteerInstance,
matcher: SearchExpression,
options?: ToMatchOptions
): Promise<void>;
// Assert elements do not exist
function notToMatchElement(
instance: PuppeteerInstance,
selector: Selector | string,
options?: NotToMatchElementOptions
): Promise<void>;
// Assert text content does not match
function notToMatchTextContent(
instance: PuppeteerInstance,
matcher: SearchExpression,
options?: NotToMatchOptions
): Promise<void>;Special matcher for handling browser dialogs (alerts, confirms, prompts).
// Capture dialogs triggered by actions (Page instances only)
function toDisplayDialog(
page: Page,
block: () => Promise<void>
): Promise<Dialog>;Global configuration options for timeout and polling behavior.
// Set global default options
function setDefaultOptions(options: Options): void;
// Get current default options
function getDefaultOptions(): Options;
interface Options extends FrameWaitForFunctionOptions {
timeout?: number;
polling?: string | number;
traverseShadowRoots?: boolean;
}import type { Page, Frame, ElementHandle, Dialog, FrameWaitForFunctionOptions } from "puppeteer";
// Puppeteer instances that can be used with matchers
type PuppeteerInstance = Page | Frame | ElementHandle;
// Selector definition supporting CSS and XPath
interface Selector {
type: "css" | "xpath";
value: string;
}
// Search expressions for text matching
type SearchExpression = RegExp | string | undefined;
// Base element options
interface GetElementOptions {
text?: string | RegExp;
visible?: boolean;
}
// Base text matching options
interface MatchTextContentOptions extends Options {
traverseShadowRoots?: boolean;
}
// Element matching options
interface ToMatchElementOptions extends GetElementOptions {
timeout?: number;
polling?: string | number;
traverseShadowRoots?: boolean;
}