Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

add-matchers

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

add-matchers

Write useful test matchers compatible with Jest and Jasmine.

  • 0.4.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
43K
decreased by-0.55%
Maintainers
1
Weekly downloads
 
Created
Source

add-matchers

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

WhatA JavaScript library to write test Matchers compatible with all versions of Jest and Jasmine.
WhyThe 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.
HowDevelopers use the API from this library, which converts them to be compatible with whichever test framework is running.

Installation

npm install --save-dev add-matchers

Usage

Include this library after your test framework but before your tests, and register your matchers before your tests as well.

API

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(recieved).toBeAwesome(arg1, arg2, arg3), your function will be called with fn(arg1, arg2, arg3, recieved).

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;

var addMatchers = require('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.

Keywords

FAQs

Package last updated on 18 Jan 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc