add-matchers

WebJar for add-matchers

License

License

MIT
GroupId

GroupId

org.webjars.npm
ArtifactId

ArtifactId

add-matchers
Last Version

Last Version

0.5.0
Release Date

Release Date

Type

Type

jar
Description

Description

add-matchers
WebJar for add-matchers
Project URL

Project URL

http://webjars.org
Source Code Management

Source Code Management

https://github.com/JamieMason/add-matchers

Download add-matchers

How to add to project

<!-- https://jarcasting.com/artifacts/org.webjars.npm/add-matchers/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>add-matchers</artifactId>
    <version>0.5.0</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/add-matchers/
implementation 'org.webjars.npm:add-matchers:0.5.0'
// https://jarcasting.com/artifacts/org.webjars.npm/add-matchers/
implementation ("org.webjars.npm:add-matchers:0.5.0")
'org.webjars.npm:add-matchers:jar:0.5.0'
<dependency org="org.webjars.npm" name="add-matchers" rev="0.5.0">
  <artifact name="add-matchers" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='add-matchers', version='0.5.0')
)
libraryDependencies += "org.webjars.npm" % "add-matchers" % "0.5.0"
[org.webjars.npm/add-matchers "0.5.0"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

There are no modules declared in this project.

add-matchers

Write useful test matchers compatible with Jest and Jasmine.

NPM version NPM downloads Build Status Maintainability

Table of 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.

๐Ÿ™‹๐Ÿฝโ€โ™‚๏ธ Getting Help

Get help with issues by creating a Bug Report or discuss ideas by opening a Feature Request.

๐Ÿ‘€ Other Projects

If you find my Open Source projects useful, please share them โค๏ธ

๐Ÿค“ Author

I'm Jamie Mason from Leeds in England, I began Web Design and Development in 1999 and have been Contracting and offering Consultancy as Fold Left Ltd since 2012. Who I've worked with includes Sky Sports, Sky Bet, Sky Poker, The Premier League, William Hill, Shell, Betfair, and Football Clubs including Leeds United, Spurs, West Ham, Arsenal, and more.

Follow JamieMason on GitHub      Follow fold_left on Twitter

Versions

Version
0.5.0
0.4.0