expect-element

raw JSON →
1.1.1 verified Fri May 01 auth: no javascript maintenance

expect-element is an extension for the expect assertion library that provides DOM-specific assertions for testing DOM nodes. Current stable version is 1.1.1, released under the MIT license. It adds assertions like toHaveAttribute, toHaveText, etc. This package is lightweight, requires expect as a peer dependency, and is designed for unit testing in browser or jsdom environments. It is not actively maintained (last release in 2016). Compared to alternatives like jest-dom, expect-element is simpler but lacks TypeScript support and modern updates.

error Cannot find module 'expect-element'
cause Package not installed or npm install missing expect-element.
fix
npm install expect expect-element
error expect(...).toHaveAttribute is not a function
cause Did not call expect.extend(expectElement) before using matchers.
fix
import expectElement from 'expect-element'; expect.extend(expectElement);
deprecated This package is no longer actively maintained. The last release was in 2016.
fix Consider using jest-dom or @testing-library/jest-dom for modern DOM assertions.
gotcha Must call expect.extend(expectElement) before using assertions; otherwise no custom matchers.
fix import expectElement from 'expect-element'; expect.extend(expectElement);
gotcha expect package version must be ^1.15.1. Using a different major version may break.
fix Ensure expect@^1.15.1 is installed.
npm install expect-element
yarn add expect-element
pnpm add expect-element

Demonstrates basic setup and usage of expect-element with DOM assertions.

import expect from 'expect';
import expectElement from 'expect-element';
expect.extend(expectElement);

// Create a DOM element
document.body.innerHTML = '<div id="foo" class="bar">Hello</div>';
const el = document.getElementById('foo');

// Assertions
expect(el).toHaveAttribute('id', 'foo');
expect(el).toHaveText('Hello');
expect(el).toNotHaveAttribute('style');