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

add-matchers

v0.6.2

Published

Write useful test matchers compatible with Jest and Jasmine.

Downloads

179,346

Readme

add-matchers

NPM version npm downloads Dependency Status Join the chat at https://gitter.im/JamieMason/Jasmine-Matchers Analytics

What: A JavaScript library to write test Matchers compatible with all versions of Jest and Jasmine.

Why: The way you write tests in Jasmine and Jest is extremely similar, but the APIs for adding custom matchers vary wildly between Jasmine 1.x, Jasmine 2.x, and Jest. This library aims to remove those obstacles and encourage Developers to share useful matchers they've created with the community.

How: Developers use the API from this library, which converts them to be compatible with whichever test framework is running.

Contents

Installation

npm install --save-dev add-matchers

Include add-matchers after your test framework but before your tests, and register your matchers before your tests as well.

API

Add Custom Matchers

import { addMatchers } from 'add-matchers';

addMatchers({
  toBeFoo(value) {
    return value === 'foo';
  },
  toInclude(other, value) {
    return value.includes(other);
  }
});
expect('foo').toBeFoo();
expect('jamie').toInclude('jam');

Add Custom Asymmetric Matchers

import { addMatchers } from 'add-matchers';

addMatchers.asymmetric({
  toBeFoo(value) {
    return value === 'foo';
  },
  toInclude(other, value) {
    return value.includes(other);
  }
});
expect({ key: 'foo', prop: 'bar' }).toEqual({
  key: any.toBeFoo(),
  prop: any.toInclude('ar')
});

Writing Matchers

The argument passed to expect is always the last argument passed to your Matcher, with any other arguments appearing before it in the order they were supplied.

This means that, in the case of expect(received).toBeAwesome(arg1, arg2, arg3), your function will be called with fn(arg1, arg2, arg3, received).

Arguments are ordered in this way to support partial application and increase re-use of matchers.

Examples

If we wanted to use the following Matchers in our tests;

// matcher with 0 arguments
expect(4).toBeEvenNumber();

// matcher with 1 argument
expect({}).toBeOfType('Object');

// matcher with Many arguments
expect([100, 14, 15, 2]).toContainItems(2, 15, 100);

We would create them as follows;

import { addMatchers } from 'add-matchers';

addMatchers({
  // matcher with 0 arguments
  toBeEvenNumber: function(received) {
    // received : 4
    return received % 2 === 0;
  },
  // matcher with 1 argument
  toBeOfType: function(type, received) {
    // type     : 'Object'
    // received : {}
    return Object.prototype.toString.call(received) === '[object ' + type + ']';
  },
  // matcher with many arguments
  toContainItems: function(arg1, arg2, arg3, received) {
    // arg1     : 2
    // arg2     : 15
    // arg3     : 100
    // received : [100, 14, 15, 2]
    return (
      received.indexOf(arg1) !== -1 &&
      received.indexOf(arg2) !== -1 &&
      received.indexOf(arg3) !== -1
    );
  }
});

For more examples, see Jasmine Matchers which is built using this library.

Related Projects