npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

x-ui-test-library

v1.0.2

Published

# Custom Jest/Vitest Matchers Library

Downloads

58

Readme

README.md

Custom Jest/Vitest Matchers Library

A collection of custom matchers for use with Jest and Vitest to enhance your testing capabilities. These matchers allow you to write more descriptive and powerful test assertions for DOM elements and other JavaScript objects.

Features

  • Custom matchers to assert DOM roles, text content, and more.
  • Compatible with Jest and Vitest testing frameworks.
  • TypeScript support with proper type definitions for all matchers.
  • Modular: Import all matchers or just the ones you need.

Installation

Using npm

npm install @your-username/custom-matchers

Using Yarn

yarn add @your-username/custom-matchers

Direct Tarball Install (if hosted privately)

npm install https://github.com/your-username/custom-matchers/releases/download/v1.0.0/custom-matchers-1.0.0.tgz

Usage

Extending Jest or Vitest with Custom Matchers

You can extend Jest or Vitest with all the custom matchers from this library by adding them to your test setup file.

import { matchers } from '@your-username/custom-matchers'

expect.extend(matchers)

Now you can use the custom matchers in your tests.

Example Matchers

toContainRole

Check if a container contains a specific number of elements with a given role.

test('should contain the specified role', () => {
  const container = render(<MyComponent />)
  expect(container).toContainRole('button', 2) // Expects 2 buttons in the DOM
})

toHaveText

Check if a container has the specified text content.

test('should contain the expected text', () => {
  const container = render(<MyComponent />)
  expect(container).toHaveText('Submit') // Expects the text 'Submit' to be present
})

Available Matchers

1. toContainRole(container, role, quantity = 1)

Asserts whether the container has a specific number of elements with the provided role.

Parameters:

  • container: The DOM element or container to search within.
  • role: The ARIA role to search for (e.g., button, textbox).
  • quantity: The number of elements with the specified role (default: 1).

Example:

expect(container).toContainRole('button', 2)

2. toHaveText(container, expectedText)

Asserts whether the container contains the specified text.

Parameters:

  • container: The DOM element or container to search within.
  • expectedText: The expected text to be found.

Example:

expect(container).toHaveText('Hello World')

Writing Your Own Matchers

You can add more matchers by following the pattern in src/matchers/. Each matcher function should return an object with a pass boolean indicating whether the test passed, and an optional message function to provide a descriptive error message if the test fails.

Example Custom Matcher

export function toBeCustom(value) {
  const pass = value === 'custom'
  return {
    pass,
    message: () =>
      pass ? `Expected value not to be custom` : `Expected value to be custom`,
  }
}

Development

Running Tests

To run tests for the matchers, use either Vitest or Jest:

# With Vitest
npm run test

# With Jest
npm run jest

Adding New Matchers

  1. Create a new file in the src/matchers/ directory.
  2. Write the matcher function and export it.
  3. Add tests for the matcher in the tests/ directory.

Building the Library

If you're using Vite or Rollup for building the library, simply run:

npm run build

This will bundle your matchers for use in other projects.

Contributing

Contributions are welcome! Please follow the contribution guidelines outlined in the CONTRIBUTING.md file.

License

This project is licensed under the MIT License - see the LICENSE file for details.