Giter VIP home page Giter VIP logo

jest-extended's Introduction

jest-extended

๐Ÿƒ๐Ÿ’ช

Additional Jest matchers


Build Status Code Coverage version downloads MIT License PRs Welcome Examples

Problem

Jest is an amazing test runner and has some awesome assertion APIs built in by default. However, there are times when having more specific matchers (assertions) would be far more convenient.

Solution

jest-extended aims to add additional matchers to Jest's default ones making it easy to test everything ๐Ÿ™Œ

Contributing

If you've come here to help contribute - Thanks! Take a look at the contributing docs as a way of getting started.

Installation

See the Installation Guide on Jest Extended documentation site.

Setup

See the Setup instructions on Jest Extended documentation site.

Matchers

See all available matchers and interactive repl on Jest Extended documentation site.

LICENSE

MIT

jest-extended's People

Contributors

benjaminkay93 avatar btnwtn avatar danielr18 avatar deerawan avatar dependabot[bot] avatar dharkness avatar ecollis6 avatar g-rath avatar github-actions[bot] avatar githug avatar greenkeeper[bot] avatar hansal7014 avatar idan-at avatar keeganwitt avatar lindgr3n avatar martiuslim avatar mattphillips avatar mprencipe avatar olsio avatar overlookmotel avatar renovate-bot avatar renovate[bot] avatar rickhanlonii avatar rluvaton avatar silverwind avatar simenb avatar stephtr avatar stofolus avatar tejasbubane avatar thiagodp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jest-extended's Issues

New Matcher: .toBeArray()

Feature Request

.toBeArray()

Use .toBeArray when checking if a value is an Array.

test('passes when value is an array', () => {
  expect([]).toBeArray();
  expect([1]).toBeArray();
  expect(true).not.toBeArray();
});

New Matcher: .toBeNaN()

Feature Request

Description:

.toBeNaN()

Use .toBeNaN when checking a value is NaN.

test('passes when value is NaN', () => {
  expect(NaN).toBeNaN();
  expect(1).not.toBeNaN();
});

New matcher: .toHaveBeenCalledBefore(mock)

Feature Request

.toHaveBeenCalledBefore(mock)

Use .toHaveBeenCalledBefore when checking if a mock has been called before another mock.

it('calls mock1 before mock2', () => {
  const mock1 = jest.fn();
  const mock2 = jest.fn();

  mock1();
  mock2();
  mock1();

  expect(mock1).toHaveBeenCalledBefore(mock2);
});

New Matcher: .toContainEntry([key, value])

Feature Request

.toContainEntry([key, value])

Use .toContainEntry when checking if an object contains the provided entry.

test('passes when object contains given entry', () => {
  const o = { a: 'foo', b: 'bar', c: 'baz' };
  expect(o).toContainEntry(['a', 'foo']);
  expect(o).toContainEntry(['b', 'bar']);
  expect(o).toContainEntry(['c', 'baz']);
  expect(o).not.toContainEntry(['a', 'qux']);
});

New Matcher: .toBeExtensible()

Feature Request

.toBeExtensible()

Use .toBeExtensible when checking if an object is extensible.

test('passes when value is extensible', () => {
  expect({a: 1}).toBeExtensible();
  expect(1).not.toBeExtensible();
});

New Matcher: .toContainAllEntries([[key, value]])

Feature Request

.toContainAllEntries([[key, value]])

Use .toContainAllEntries when checking if an object only contains all of the provided entries.

test('passes when object only contains all of the given entries', () => {
  const o = { a: 'foo', b: 'bar', c: 'baz' };
  expect(o).toContainAllEntries([['a', 'foo'], ['b', 'bar'], ['c', 'baz']]);
  expect(o).not.toContainAllEntries([['a', 'foo'], ['b', 'bar']]);
});

Add asymmetric number matchers

(Copied from jestjs/jest#5880)

Do you want to request a feature or report a bug?
A feature

I dumped the template as it doesn't fit feature requests very well. Hope you are okay with that.

Today we have asymmetric matchers like expect.stringMatching which are awesome for testing dynamic data or only check data that important for the current test. However, there exists no equivalent for numbers.

I propose something like

expect.numberNear(expected, precision)

which would check that the actual value is in the range expected ยฑ precision.

A common use case for this would be checking a timestamp in a generated object

expect(object).toEqual({type: 'log', timestamp: expect.numberNear(Date.now(), 20)})

Maybe precision could default to something like Math.log(expected) so that it could be optional, however this is (maybe a big) risk that that's more confusing than helpful.

New Matcher: .toIncludeRepeated(substring, times)

Feature Request

.toIncludeRepeated(substring, times)

Use .toIncludeRepeated when checking if a String includes the given String substring the correct number of times.

test('passes when value includes substring n times', () => {
  expect('hello hello world').toIncludeRepeated('hello', 2);
  expect('hello hello world').not.toIncludeRepeated('hello', 1);
});

Date API Proposal

Feature Request

Description:
API for Date matchers. Some matchers are inspired by Jasmine Matchers.

Possible solution:

Jasmine-like matchers:

expect(date).toBeDate(); // Matcher added
expect(date).toBeValidDate();  // Matcher added

expect(date).toBeAfter(otherDate); // Matcher added
expect(date).toBeBefore(otherDate); // Matcher added

expect(object).toHaveDate(memberName);
expect(object).toHaveDateAfter(memberName, date);
expect(object).toHaveDateBefore(memberName, date);

Additional matchers:

expect(date).toBeAfterOrEqualTo(otherDate); // or toBeGreaterThanOrEqualTo
expect(date).toBeBeforeOrEqualTo(otherDate); // or toBeLessThanOrEqualTo
expect(date).toBeBetween(startDate, endDate);
expect(date).toDiffInDays(otherDate, days); // e.g., expect( '12/25/2018' ).toDiffInDays( '12/24/2018', 1 );

expect( date ).toHaveDay( day );
expect( date ).toHaveMonth( month );
expect( date ).toHaveYear( year );

New Matcher: .toBeWithin(start, end)

Feature Request

Description:

.toBeWithin(start, end)

Use .toBeWithin when checking if a number is in between the given bounds of: start (inclusive) and end (exclusive).

test('passes when number is within given bounds', () => {
  expect(1).toBeWithin(1, 3);
  expect(2).toBeWithin(1, 3);
  expect(3).not.toBeWithin(1, 3);
});

New Matcher: .toBeEmpty()

Feature Request

Description:

.toBeEmpty()

Use .toBeEmpty when checking if a String '', Array [] or Object {} is empty.

test('passes when given an empty string', () => {
  expect('').toBeEmpty();
  expect('hello').not.toBeEmpty();
});

test('passes when given an empty array', () => {
  expect([]).toBeEmpty();
  expect(['hello']).not.toBeEmpty();
});

test('passes when given an empty object', () => {
  expect({}).toBeEmpty();
  expect({ hello: 'world' }).not.toBeEmpty();
});

New Matcher: .toBeFunction()

Feature Request

.toBeFunction()

Use .toBeFunction when checking if a value is a Function.

test('passes when value is a function', () => {
  function noop = () {};
  expect(() => {}).toBeFunction();
  expect(function() {}).not.toBeFunction();
  expect(noop).toBeFunction();
  expect(true).not.toBeFunction();
});

New matcher: toContainValues

Feature Request

Description:

.toContainValues([values])

Use .toContainValues when checking if an object contains all of the provided values.

test('passes when object contains all of the given values', () => {
  const o = { a: 'foo', b: 'bar', c: 'baz' };
  expect(o).toContainValues(['foo']);
  expect(o).toContainValues(['baz', 'bar']);
  expect(o).not.toContainValues(['qux', 'foo']);
});

New matcher: .toHaveBeenCalledTimely(array)

Feature Request

Follow-up of: jestjs/jest#5363

.toHaveBeenCalledTimely accepts an array of temporal differences in milliseconds which specify the maximum time in between invocations to a mock. The matcher fails when at least one of the specified timing constraints is not fulfilled.

Example:

it('should retry 2 times with exponential back-off with a factor of 2', async () => {
  jest.doMock('foo-api', jest.fn(() => fooApiMocks.helloSuccessAsync()));
  const fooApi = require('foo-api');  

  myModuleWhichDependsOnFooApi.execute(); 

  expect(fooApi.hello).toHaveBeenCalledTimely([  
      2000, // 1st retry - time between 1st and 2nd call to fooApi.hello()
      4000 // 2nd retry - time between 2nd and 3rd call to fooApi.hello()
    ]);
});

Following the reasoning outlined here and issue reported in #98, would the above be possible before #98 is addressed and resolved?

If possible, I'd very much like to work on a PR for this. ๐Ÿ˜„
Thanks!

New Matcher: .toBeString()

Feature Request

.toBeString()

Use .toBeString when checking if a value is a String.

test('passes when value is a string', () => {
  expect('').toBeString();
  expect('hello').toBeString();
  expect(true).not.toBeString();
});

New Matcher: .toBeObject()

Feature Request

.toBeObject()

Use .toBeObject when checking if a value is an Object.

test('passes when value is an object', () => {
  expect({}).toBeObject();
  expect({ a: 'hello' }).toBeObject();
  expect(true).not.toBeObject();
});

New Matcher: .toContainAllValues([values])

Feature Request

.toContainAllValues([values])

Use .toContainAllValues when checking if an object only contains all of the provided values.

test('passes when object only contains all of the given values', () => {
  const o = { a: 'foo', b: 'bar', c: 'baz' };
  expect(o).toContainAllValues(['foo', 'bar', 'baz']);
  expect(o).toContainAllValues(['baz', 'bar', 'foo']);
  expect(o).not.toContainAllValues(['bar', 'foo']);
});

New Matcher: .toBeNumber()

Feature Request

.toBeNumber()

Use .toBeNumber when checking if a value is a Number.

test('passes when value is a number', () => {
  expect(1).toBeNumber();
  expect(NaN).toBeNumber();
  expect(Infinity).toBeNumber();
  expect(true).not.toBeNumber();
});

.toContainValue

Feature Request

Description:

toContainValue(value)

Use .toContainValue when checking if an object contains the provided value.

test('passes when object contains given value', () => {
  const o = { a: 'foo', b: 'bar', c: 'baz' };
  expect(o).toContainValue('foo');
  expect(o).toContainValue('bar');
  expect(o).not.toContainValue('qux');
});

New Matcher: .toBeSealed()

Feature Request

.toBeSealed()

Use .toBeSealed when checking if an object is sealed.

test('passes when value is sealed', () => {
  expect(Object.seal({})).toBeSealed();
  expect({}).not.toBeSealed();
  expect(1).not.toBeSealed();
});

A matcher that checks that two arrays contain the same elements

Feature Request

Description: A matcher that checks that two arrays contain the same elements. Examples:

expect([1, 2]).toMatchArray([2, 1]) // order doesn't matter!
expect([1, 2, 2]).toMatchArray([2, 1, 2]) // same number of 2s, all good
expect([1, 2]).not.toMatchArray([1, 2, 2]) // one extra 2, FAIL
expect([1, 2, 2]).not.toMatchArray([1, 2]) // one extra 2, FAIL

Possible solution: Given arrays A and B, check that they are equal in length. For each element in A, check that the element is equal to any element in B. If it is, remove the element from both arrays. Loop until both arrays are empty.

New Matcher: .toContainAnyKeys([keys])

Feature Request

.toContainAnyKeys([keys])

Use .toContainAnyKeys when checking if an object contains at least one of the provided keys.

test('passes when object contains at least one matching key', () => {
  const o = { a: 'hello', b: 'world' };
  expect(o).toContainAnyKeys(['a']);
  expect(o).toContainAnyKeys(['b']);
  expect(o).toContainAnyKeys(['b', 'c']);
  expect(o).not.toContainAnyKeys(['c']);
});

New Matcher: .toEqualIgnoringCase(string)

Feature Request

.toEqualIgnoringCase(string)

Use .toEqualIgnoringCase when checking if a string is equal (===) to another ignoring the casing of both strings.

test('passes when strings are equal ignoring case', () => {
  expect('hello world').toEqualIgnoringCase('hello world');
  expect('hello WORLD').toEqualIgnoringCase('HELLO world');
  expect('HELLO WORLD').toEqualIgnoringCase('hello world');
  expect('hello world').toEqualIgnoringCase('HELLO WORLD');
  expect('hello world').not.toEqualIgnoringCase('hello');
});

New Matcher: .toContainKey(key)

Feature Request

.toContainKey(key)

Use .toContainKey when checking if an object contains the provided key.

test('passes when object contains the given key', () => {
  const o = { a: 'foo', b: 'bar', c: 'baz' };
  expect(o).toContainKey('a');
  expect(o).toContainKey('b');
  expect(o).toContainKey('c');
  expect(o).not.toContainKey('d');
});

New Matcher: .toBeOdd()

Feature Request

.toBeOdd()

Use .toBeOdd when checking if a value is an odd Number.

test('passes when value is an odd number', () => {
  expect(1).toBeOdd();
  expect(2).not.toBeOdd();
  expect(NaN).not.toBeOdd();
});

New Matcher: .toInclude(substring)

Feature Request

.toInclude(substring)

Use .toInclude when checking if a String includes the given String substring.

test('passes when value includes substring', () => {
  expect('hello world').toInclude('ell');
  expect('hello world').not.toInclude('bob');
});

New Matcher: .toContainAnyValues([values])

Feature Request

.toContainAnyValues([values])

Use .toContainAnyValues when checking if an object contains at least one of the provided values.

test('passes when object contains at least one of the given values', () => {
  const o = { a: 'foo', b: 'bar', c: 'baz' };
  expect(o).toContainAnyValues(['qux', 'foo']);
  expect(o).toContainAnyValues(['qux', 'bar']);
  expect(o).toContainAnyValues(['qux', 'baz']);
  expect(o).not.toContainAnyValues(['qux']);
});

New Matcher: .toContainAnyEntries([[key, value]])

Feature Request

.toContainAnyEntries([[key, value]])

Use .toContainAnyEntries when checking if an object contains at least one of the provided entries.

test('passes when object contains at least one of the given entries', () => {
  const o = { a: 'foo', b: 'bar', c: 'baz' };
  expect(o).toContainAnyEntries([['a', 'qux'], ['a', 'foo']]);
  expect(o).toContainAnyEntries([['a', 'qux'], ['b', 'bar']]);
  expect(o).toContainAnyEntries([['a', 'qux'], ['c', 'baz']]);
  expect(o).not.toContainAnyEntries([['d', 'qux']]);
});

New Matcher: .toBePositive()

Feature Request

.toBePositive()

Use .toBePositive when checking if a value is a positive Number.

test('passes when value is a positive number', () => {
  expect(1).toBePositive();
  expect(Infinity).not.toBePositive();
  expect(-1).not.toBePositive();
  expect(NaN).not.toBePositive();
});

New Matcher: .toContainEntries([[key, value]])

Feature Request

.toContainEntries([[key, value]])

Use .toContainEntries when checking if an object contains all of the provided entries.

test('passes when object contains all of the given entries', () => {
  const o = { a: 'foo', b: 'bar', c: 'baz' };
  expect(o).toContainEntries([['a', 'foo']]);
  expect(o).toContainEntries([['c', 'baz'], ['a', 'foo']]);
  expect(o).not.toContainEntries([['b', 'qux'], ['a', 'foo']]);
});

New Matcher: .toBeEven()

Feature Request

.toBeEven()

Use .toBeEven when checking if a value is an even Number.

test('passes when value is an even number', () => {
  expect(2).toBeEven();
  expect(1).not.toBeEven();
  expect(NaN).not.toBeEven();
});

New Matcher: .toContainAllKeys([keys])

Feature Request

.toContainAllKeys([keys])

Use .toContainAllKeys when checking if an object only contains all of the provided keys.

test('passes when object only contains all keys', () => {
  const o = { a: 'hello', b: 'world' };
  expect(o).toContainAllKeys(['a', 'b']);
  expect(o).toContainAllKeys(['b', 'a']);
  expect(o).not.toContainAllKeys(['b']);
});

New Matcher: .toBeFinite()

Feature Request

.toBeFinite()

Use .toBeFinite when checking if a value is a Number, not NaN or Infinity.

test('passes when value is a finite number', () => {
  expect(1).toBeFinite();
  expect(Infinity).not.toBeFinite();
  expect(NaN).not.toBeFinite();
});

New Matcher: .toSatisfy(predicate)

Feature Request

.toSatisfy(predicate)

Use .toSatisfy when you want to use a custom matcher by supplying a predicate function that returns a Boolean.

test('passes when value passes given predicate', () => {
  const greaterThanOneButNotThree = n => n > 1 && n !== 3;
  expect(100).toSatisfy(greaterThanOneButNotThree);
  expect(0).not.toSatisfy(greaterThanOneButNotThree);
  expect(3).not.toSatisfy(greaterThanOneButNotThree);
});

New Matcher: .toHaveSomeMembers([members])

Feature Request

.toHaveSomeMembers([members])

Use .toHaveSomeMembers when checking if an Array contains some of the members of a given set.

test('passes when given array values match the members of the set', () => {
  expect([1, 2, 3]).toHaveSomeMembers([2, 1, 3]);
  expect([1, 2, 2]).toHaveSomeMembers([2]);
  expect([1, 2, 2]).not.toHaveSomeMembers([3]);
});

`toContainAllKeys` will pass when tested against an empty object

Bug

  • package version: 0.6.0
  • node version: 9.6.1
  • npm version: 5.6.0

toContainAllKeys will always pass when testing an empty object.
Here's a simple example to reproduce the issue.

test('toContainAllKeys should fail', () => {
  expect({}).toContainAllKeys(['foo'])
})

I'll open a PR to fix this shortly

Use `test` instead of `it`

Feature Request

  • Description:
    Our README uses test, but our codebase has it at a few places.
    The Jest docs use test everywhere. Although it is just an alias of test, should we switch to using test everywhere in our codebase to make it consistent with our README (as well as the Jest docs)?

  • Possible solution:
    Use test instead of it

Let me know if you are ok with the change and I will send a PR.

New Matcher: .toBeNil()

Feature Request

Description:

.toBeNil()

Use .toBeNil when checking a value is null or undefined.

test('passes when value is null or undefined', () => {
  expect(null).toBeNil();
  expect(undefined).toBeNil();
  expect(true).not.toBeNil();
});

New Matcher: .toIncludeMultiple([substring])

Feature Request

.toIncludeMultiple([substring])

Use .toIncludeMultiple when checking if a String includes all of the given substrings.

test('passes when value includes all substrings', () => {
  expect('hello world').toInclude(['world', 'hello']);
  expect('hello world').not.toInclude(['world', 'hello', 'bob']);
});

New Matcher: .toBeOneOf([members])

Feature Request

.toBeOneOf([members])

Use .toBeOneOf when checking if a value is a member of a given Array.

test('passes when value is in given array', () => {
  expect(1).toBeOneOf([1, 2, 3]);
  expect(4).not.toBeOneOf([1, 2, 3]);
});

New Matcher: .toHaveAllMembers([members])

Feature Request

.toHaveAllMembers([members])

Use .toHaveAllMembers when checking if an Array contains all of the same members of a given set.

test('passes when given array values match the members of the set', () => {
  expect([1, 2, 3]).toHaveAllMembers([2, 1, 3]);
  expect([1, 2, 2]).toHaveAllMembers([2, 1]);
});

New Matcher: .toBeFrozen()

Feature Request

.toBeFrozen()

Use .toBeFrozen when checking if an object is frozon.

test('passes when value is frozen', () => {
  expect(Object.frozen({})).toBeFrozen();
  expect({}).not.toBeFrozen();
  expect(1).not.toBeFrozen();
});

New Matcher: toBeFalse

Feature Request

Description:

Add new matcher expect(boolean).toBeFalse()

Possible solution:

const predicate = given => given === false;

New Matcher: .toContainKeys([keys])

Feature Request

.toContainKeys([keys])

Use .toContainKeys when checking if an object has all of the provided keys.

test('passes when object contains all keys', () => {
  const o = { a: 'foo', b: 'bar', c: 'baz' };
  expect(o).toContainKeys(['a', 'b']);
  expect(o).toContainKeys(['b', 'c']);
  expect(o).not.toContainKeys(['d']);
});

New Matcher: .toBeNegative()

Feature Request

.toBeNegative()

Use .toBeNegative when checking if a value is a negative Number.

test('passes when value is a negative number', () => {
  expect(-1).toBeNegative();
  expect(-Infinity).not.toBeNegative();
  expect(1).not.toBeNegative();
  expect(NaN).not.toBeNegative();
});

New Matcher: .toBeBoolean()

Feature Request

.toBeBoolean()

Use .toBeBoolean when checking if a value is a Boolean.

test('passes when value is a boolean', () => {
  expect(false).toBeBoolean();
  expect(true).toBeBoolean();
  expect(1 === 1).toBeBoolean();
  expect(1).not.toBeBoolean();
});

New Matcher: .toEndWith(suffix)

Feature Request

.toEndWith(suffix)

Use .toEndWith when checking if a String ends with a given String suffix.

test('passes when value is ends with given string', () => {
  expect('hello world').toEndWith('world');
  expect('hello world').not.toEndWith('hello');
});

New Matcher: .toStartWith(prefix)

Feature Request

.toStartWith(prefix)

Use .toStartWith when checking if a String starts with a given String prefix.

test('passes when value is starts with given string', () => {
  expect('hello world').toStartWith('hello');
  expect('hello world').not.toStartWith('world');
});

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.