diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index 0f7e9d5b..7429c1fb 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -18,6 +18,7 @@ with the contributing guidelines (found in the CONTRIBUTING.md file). - [ ] Unit tests - [ ] Documentation is up to date +- [ ] Regenerate README if a new matcher has been added to `/docs/README.template.md` (`yarn generate-readme`) - [ ] No additional lint warnings - [ ] Add yourself to contributors list (`yarn contributor`) - [ ] [Typescript definitions](https://github.com/jest-community/jest-extended/blob/master/types/index.d.ts) are added/updated where relevant diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index feccb964..1a629ed3 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -34,7 +34,12 @@ Head over to [here](https://hacktoberfest.digitalocean.com/sign_up/register) to * See the Jest docs for an [example usage](https://facebook.github.io/jest/docs/en/expect.html#thisutils) - Jest's [`expect`](https://github.com/facebook/jest/tree/master/packages/expect) package is being used to access their deep `equals` function. * `import { equals } from 'expect/build/jasmine_utils';` - - Docs for the new matcher should be updated in the API section of the `README.md` to no longer say `Unimplemented`. + - Docs for new matchers should be added to `/docs/sandbox/matchers/[matcher.test.js]`, with a comment explaining the matcher and a code example of how to use the matcher. + - To add the matcher to the README you will need to link the docs in `docs/README.template.md` with the following syntax: + ``` + {{/sandbox/matchers/myNewMatcherName.test.js}} + ``` + - Once you have linked it in the template you can regenerate the README by running `yarn generate-readme`. - Type definitions for the new matchers should be added to `types/index.d.ts`. ## Committing and Pushing changes diff --git a/README.md b/README.md index 1fca507c..dd8822c9 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,7 @@ If you've come here to help contribute - Thanks! Take a look at the [contributin - [Contributing](#contributing) - [Installation](#installation) - [Setup](#setup) +- [jest-extended REPL](https://codesandbox.io/s/github/jest-community/jest-extended/tree/master/docs/sandbox?module=%2Fmatchers%2FtoBeEmpty.test.js) - [API](#api) - [.pass(message)](#passmessage) - [.fail(message)](#failmessage) @@ -188,6 +189,8 @@ test('passes when given an empty object', () => { }); ``` +[Open toBeEmpty in REPL](https://codesandbox.io/s/github/jest-community/jest-extended/tree/master/docs/sandbox?module=%2Fmatchers%2FtoBeEmpty.test.js) + #### .toBeOneOf([members]) Use `.toBeOneOf` when checking if a value is a member of a given `Array`. @@ -199,6 +202,8 @@ test('passes when value is in given array', () => { }); ``` +[Open toBeOneOf in REPL](https://codesandbox.io/s/github/jest-community/jest-extended/tree/master/docs/sandbox?module=%2Fmatchers%2FtoBeOneOf.test.js) + #### .toBeNil() Use `.toBeNil` when checking a value is `null` or `undefined`. @@ -211,6 +216,8 @@ test('passes when value is null or undefined', () => { }); ``` +[Open toBeNil in REPL](https://codesandbox.io/s/github/jest-community/jest-extended/tree/master/docs/sandbox?module=%2Fmatchers%2FtoBeNil.test.js) + #### .toSatisfy(predicate) Use `.toSatisfy` when you want to use a custom matcher by supplying a predicate function that returns a `Boolean`. @@ -224,6 +231,8 @@ test('passes when value passes given predicate', () => { }); ``` +[Open toSatisfy in REPL](https://codesandbox.io/s/github/jest-community/jest-extended/tree/master/docs/sandbox?module=%2Fmatchers%2FtoSatisfy.test.js) + ### Array #### .toBeArray() @@ -238,6 +247,8 @@ test('passes when value is an array', () => { }); ``` +[Open toBeArray in REPL](https://codesandbox.io/s/github/jest-community/jest-extended/tree/master/docs/sandbox?module=%2Fmatchers%2Farray%2FtoBeArray.test.js) + #### .toBeArrayOfSize() Use `.toBeArrayOfSize` when checking if a value is an `Array` of size x. @@ -250,6 +261,8 @@ test('passes when value is an array', () => { }); ``` +[Open toBeArrayOfSize in REPL](https://codesandbox.io/s/github/jest-community/jest-extended/tree/master/docs/sandbox?module=%2Fmatchers%2Farray%2FtoBeArrayOfSize.test.js) + #### .toIncludeAllMembers([members]) Use `.toIncludeAllMembers` when checking if an `Array` contains all of the same members of a given set. @@ -261,6 +274,8 @@ test('passes when given array values match the members of the set', () => { }); ``` +[Open toIncludeAllMembers in REPL](https://codesandbox.io/s/github/jest-community/jest-extended/tree/master/docs/sandbox?module=%2Fmatchers%2Farray%2FtoIncludeAllMembers.test.js) + #### .toIncludeAnyMembers([members]) Use `.toIncludeAnyMembers` when checking if an `Array` contains any of the members of a given set. @@ -273,6 +288,8 @@ test('passes when given array values match any of the members in the set', () => }); ``` +[Open toIncludeAnyMembers in REPL](https://codesandbox.io/s/github/jest-community/jest-extended/tree/master/docs/sandbox?module=%2Fmatchers%2Farray%2FtoIncludeAnyMembers.test.js) + #### .toIncludeSameMembers([members]) Use `.toIncludeSameMembers` when checking if two arrays contain equal values, in any order. @@ -284,6 +301,7 @@ test('passes when arrays match in a different order', () => { }); ``` +[Open toIncludeSameMembers in REPL](https://codesandbox.io/s/github/jest-community/jest-extended/tree/master/docs/sandbox?module=%2Fmatchers%2Farray%2FtoIncludeSameMembers.test.js) #### .toSatisfyAll(predicate) @@ -292,11 +310,13 @@ Use `.toSatisfyAll` when you want to use a custom matcher by supplying a predica ```js test('passes when all values in array pass given predicate', () => { const isOdd = el => el % 2 === 1; - expect([1,3,5,7]).toSatisfyAll(isOdd); - expect([1,3,4,5,7]).not.toSatisfyAll(isOdd); + expect([1, 3, 5, 7]).toSatisfyAll(isOdd); + expect([1, 3, 4, 5, 7]).not.toSatisfyAll(isOdd); }); ``` +[Open toSatisfyAll in REPL](https://codesandbox.io/s/github/jest-community/jest-extended/tree/master/docs/sandbox?module=%2Fmatchers%2Farray%2FtoSatisfyAll.test.js) + ### Boolean #### .toBeBoolean() @@ -312,33 +332,40 @@ test('passes when value is a boolean', () => { }); ``` +[Open toBeBoolean in REPL](https://codesandbox.io/s/github/jest-community/jest-extended/tree/master/docs/sandbox?module=%2Fmatchers%2Fboolean%2FtoBeBoolean.test.js) + #### .toBeTrue() Use `.toBeTrue` when checking a value is equal (===) to `true`. ```js test('is jest cool', () => { + const isJestCool = () => true; expect(isJestCool()).toBeTrue(); expect(false).not.toBeTrue(); }); ``` +[Open toBeTrue in REPL](https://codesandbox.io/s/github/jest-community/jest-extended/tree/master/docs/sandbox?module=%2Fmatchers%2Fboolean%2FtoBeTrue.test.js) + #### .toBeFalse() Use `.toBeFalse` when checking a value is equal (===) to `false`. ```js test('returns false', () => { - expect(areWeThereYet()).toBeFalse(); + expect(false).toBeFalse(); expect(true).not.toBeFalse(); }); ``` +[Open toBeFalse in REPL](https://codesandbox.io/s/github/jest-community/jest-extended/tree/master/docs/sandbox?module=%2Fmatchers%2Fboolean%2FtoBeFalse.test.js) + ### ~~Date~~ Proposal in #117 (*under development*) -### .toBeDate() +#### .toBeDate() Use `.toBeDate` when checking if a value is a `Date`. @@ -346,42 +373,55 @@ Use `.toBeDate` when checking if a value is a `Date`. test('passes when value is a date', () => { expect(new Date()).toBeDate(); expect('01/01/2018').not.toBeDate(); - expect(new Date('01/01/2018').toBeDate(); + expect(new Date('01/01/2018')).toBeDate(); expect(undefined).not.toBeDate(); }); ``` -### .toBeValidDate() +[Open toBeDate in REPL](https://codesandbox.io/s/github/jest-community/jest-extended/tree/master/docs/sandbox?module=%2Fmatchers%2Fdate%2FtoBeDate.test.js) + +#### .toBeValidDate() Use `.toBeValidDate` when checking if a given `Date` object is valid. -``` +```js test('passes when Date is valid', () => { expect(new Date()).toBeValidDate(); expect('01/01/2018').not.toBeValidDate(); - expect(new Date('01/01/2018').toBeValidDate(); - expect(new Date('01/90/2018').not.toBeValidDate(); + expect(new Date('01/01/2018')).toBeValidDate(); + expect(new Date('01/90/2018')).not.toBeValidDate(); expect(undefined).not.toBeValidDate(); }); ``` -### .toBeAfter(date) - Use `.toBeAfter` when checking if a date occurs after `date`. - ```js +[Open toBeValidDate in REPL](https://codesandbox.io/s/github/jest-community/jest-extended/tree/master/docs/sandbox?module=%2Fmatchers%2Fdate%2FtoBeValidDate.test.js) + +#### .toBeAfter(date) + +Use `.toBeAfter` when checking if a date occurs after `date`. + +```js test('passes when input is after date', () => { expect(new Date('01/01/2019')).toBeAfter(new Date('01/01/2018')); expect('01/01/2018').not.toBeAfter(new Date('01/01/2019')); }); ``` - ### .toBeBefore(date) - Use `.toBeBefore` when checking if a date occurs before `date`. - ```js + +[Open toBeAfter in REPL](https://codesandbox.io/s/github/jest-community/jest-extended/tree/master/docs/sandbox?module=%2Fmatchers%2Fdate%2FtoBeAfter.test.js) + +#### .toBeBefore(date) + +Use `.toBeBefore` when checking if a date occurs before `date`. + +```js test('passes when input is before date', () => { expect(new Date('01/01/2018')).toBeBefore(new Date('01/01/2019')); expect('01/01/2019').not.toBeBefore(new Date('01/01/2018')); }); ``` +[Open toBeBefore in REPL](https://codesandbox.io/s/github/jest-community/jest-extended/tree/master/docs/sandbox?module=%2Fmatchers%2Fdate%2FtoBeBefore.test.js) + ### Function #### .toBeFunction() @@ -390,14 +430,15 @@ Use `.toBeFunction` when checking if a value is a `Function`. ```js test('passes when value is a function', () => { - function noop = () {}; + function noop() {} expect(() => {}).toBeFunction(); - expect(function() {}).not.toBeFunction(); expect(noop).toBeFunction(); expect(true).not.toBeFunction(); }); ``` +[Open toBeFunction in REPL](https://codesandbox.io/s/github/jest-community/jest-extended/tree/master/docs/sandbox?module=%2Fmatchers%2Ffunction%2FtoBeFunction.test.js) + ### Mock #### .toHaveBeenCalledBefore() @@ -406,9 +447,7 @@ Use `.toHaveBeenCalledBefore` when checking if a `Mock` was called before anothe _Note: Required Jest version >=23_ - ```js - it('calls mock1 before mock2', () => { const mock1 = jest.fn(); const mock2 = jest.fn(); @@ -427,9 +466,7 @@ Use `.toHaveBeenCalledAfter` when checking if a `Mock` was called after another _Note: Required Jest version >=23_ - ```js - it('calls mock1 after mock2', () => { const mock1 = jest.fn(); const mock2 = jest.fn(); @@ -457,6 +494,8 @@ test('passes when value is a number', () => { }); ``` +[Open toBeNumber in REPL](https://codesandbox.io/s/github/jest-community/jest-extended/tree/master/docs/sandbox?module=%2Fmatchers%2Fnumber%2FtoBeNumber.test.js) + #### .toBeNaN() Use `.toBeNaN` when checking a value is `NaN`. @@ -468,6 +507,8 @@ test('passes when value is NaN', () => { }); ``` +[Open toBeNaN in REPL](https://codesandbox.io/s/github/jest-community/jest-extended/tree/master/docs/sandbox?module=%2Fmatchers%2Fnumber%2FtoBeNaN.test.js) + #### .toBeFinite() Use `.toBeFinite` when checking if a value is a `Number`, not `NaN` or `Infinity`. @@ -480,6 +521,8 @@ test('passes when value is a finite number', () => { }); ``` +[Open toBeFinite in REPL](https://codesandbox.io/s/github/jest-community/jest-extended/tree/master/docs/sandbox?module=%2Fmatchers%2Fnumber%2FtoBeFinite.test.js) + #### .toBePositive() Use `.toBePositive` when checking if a value is a positive `Number`. @@ -493,6 +536,8 @@ test('passes when value is a positive number', () => { }); ``` +[Open toBePositive in REPL](https://codesandbox.io/s/github/jest-community/jest-extended/tree/master/docs/sandbox?module=%2Fmatchers%2Fnumber%2FtoBePositive.test.js) + #### .toBeNegative() Use `.toBeNegative` when checking if a value is a negative `Number`. @@ -506,8 +551,9 @@ test('passes when value is a negative number', () => { }); ``` -#### .toBeEven() +[Open toBeNegative in REPL](https://codesandbox.io/s/github/jest-community/jest-extended/tree/master/docs/sandbox?module=%2Fmatchers%2Fnumber%2FtoBeNegative.test.js) +#### .toBeEven() Use `.toBeEven` when checking if a value is an even `Number`. @@ -519,6 +565,8 @@ test('passes when value is an even number', () => { }); ``` +[Open toBeEven in REPL](https://codesandbox.io/s/github/jest-community/jest-extended/tree/master/docs/sandbox?module=%2Fmatchers%2Fnumber%2FtoBeEven.test.js) + #### .toBeOdd() Use `.toBeOdd` when checking if a value is an odd `Number`. @@ -531,6 +579,8 @@ test('passes when value is an odd number', () => { }); ``` +[Open toBeOdd in REPL](https://codesandbox.io/s/github/jest-community/jest-extended/tree/master/docs/sandbox?module=%2Fmatchers%2Fnumber%2FtoBeOdd.test.js) + #### .toBeWithin(start, end) Use `.toBeWithin` when checking if a number is in between the given bounds of: start (inclusive) and end (exclusive). @@ -543,6 +593,8 @@ test('passes when number is within given bounds', () => { }); ``` +[Open toBeWithin in REPL](https://codesandbox.io/s/github/jest-community/jest-extended/tree/master/docs/sandbox?module=%2Fmatchers%2Fnumber%2FtoBeWithin.test.js) + ### Object #### .toBeObject() @@ -557,6 +609,8 @@ test('passes when value is an object', () => { }); ``` +[Open toBeObject in REPL](https://codesandbox.io/s/github/jest-community/jest-extended/tree/master/docs/sandbox?module=%2Fmatchers%2Fobject%2FtoBeObject.test.js) + #### .toContainKey(key) Use `.toContainKey` when checking if an object contains the provided key. @@ -571,6 +625,8 @@ test('passes when object contains the given key', () => { }); ``` +[Open toContainKey in REPL](https://codesandbox.io/s/github/jest-community/jest-extended/tree/master/docs/sandbox?module=%2Fmatchers%2Fobject%2FtoContainKey.test.js) + #### .toContainKeys([keys]) Use `.toContainKeys` when checking if an object has all of the provided keys. @@ -584,6 +640,8 @@ test('passes when object contains all keys', () => { }); ``` +[Open toContainKeys in REPL](https://codesandbox.io/s/github/jest-community/jest-extended/tree/master/docs/sandbox?module=%2Fmatchers%2Fobject%2FtoContainKeys.test.js) + #### .toContainAllKeys([keys]) Use `.toContainAllKeys` when checking if an object only contains all of the provided keys. @@ -597,6 +655,8 @@ test('passes when object only contains all keys', () => { }); ``` +[Open toContainAllKeys in REPL](https://codesandbox.io/s/github/jest-community/jest-extended/tree/master/docs/sandbox?module=%2Fmatchers%2Fobject%2FtoContainAllKeys.test.js) + #### .toContainAnyKeys([keys]) Use `.toContainAnyKeys` when checking if an object contains at least one of the provided keys. @@ -611,6 +671,8 @@ test('passes when object contains at least one matching key', () => { }); ``` +[Open toContainAnyKeys in REPL](https://codesandbox.io/s/github/jest-community/jest-extended/tree/master/docs/sandbox?module=%2Fmatchers%2Fobject%2FtoContainAnyKeys.test.js) + #### .toContainValue(value) Use `.toContainValue` when checking if an object contains the provided value. @@ -624,6 +686,8 @@ test('passes when object contains given value', () => { }); ``` +[Open toContainValue in REPL](https://codesandbox.io/s/github/jest-community/jest-extended/tree/master/docs/sandbox?module=%2Fmatchers%2Fobject%2FtoContainValue.test.js) + #### .toContainValues([values]) Use `.toContainValues` when checking if an object contains all of the provided values. @@ -637,6 +701,8 @@ test('passes when object contains all of the given values', () => { }); ``` +[Open toContainValues in REPL](https://codesandbox.io/s/github/jest-community/jest-extended/tree/master/docs/sandbox?module=%2Fmatchers%2Fobject%2FtoContainValues.test.js) + #### .toContainAllValues([values]) Use `.toContainAllValues` when checking if an object only contains all of the provided values. @@ -650,6 +716,8 @@ test('passes when object only contains all of the given values', () => { }); ``` +[Open toContainAllValues in REPL](https://codesandbox.io/s/github/jest-community/jest-extended/tree/master/docs/sandbox?module=%2Fmatchers%2Fobject%2FtoContainAllValues.test.js) + #### .toContainAnyValues([values]) Use `.toContainAnyValues` when checking if an object contains at least one of the provided values. @@ -664,6 +732,8 @@ test('passes when object contains at least one of the given values', () => { }); ``` +[Open toContainAnyValues in REPL](https://codesandbox.io/s/github/jest-community/jest-extended/tree/master/docs/sandbox?module=%2Fmatchers%2Fobject%2FtoContainAnyValues.test.js) + #### .toContainEntry([key, value]) Use `.toContainEntry` when checking if an object contains the provided entry. @@ -678,6 +748,8 @@ test('passes when object contains given entry', () => { }); ``` +[Open toContainEntry in REPL](https://codesandbox.io/s/github/jest-community/jest-extended/tree/master/docs/sandbox?module=%2Fmatchers%2Fobject%2FtoContainEntry.test.js) + #### .toContainEntries([[key, value]]) Use `.toContainEntries` when checking if an object contains all of the provided entries. @@ -691,6 +763,8 @@ test('passes when object contains all of the given entries', () => { }); ``` +[Open toContainEntries in REPL](https://codesandbox.io/s/github/jest-community/jest-extended/tree/master/docs/sandbox?module=%2Fmatchers%2Fobject%2FtoContainEntries.test.js) + #### .toContainAllEntries([[key, value]]) Use `.toContainAllEntries` when checking if an object only contains all of the provided entries. @@ -703,6 +777,8 @@ test('passes when object only contains all of the given entries', () => { }); ``` +[Open toContainAllEntries in REPL](https://codesandbox.io/s/github/jest-community/jest-extended/tree/master/docs/sandbox?module=%2Fmatchers%2Fobject%2FtoContainAllEntries.test.js) + #### .toContainAnyEntries([[key, value]]) Use `.toContainAnyEntries` when checking if an object contains at least one of the provided entries. @@ -717,17 +793,21 @@ test('passes when object contains at least one of the given entries', () => { }); ``` +[Open toContainAnyEntries in REPL](https://codesandbox.io/s/github/jest-community/jest-extended/tree/master/docs/sandbox?module=%2Fmatchers%2Fobject%2FtoContainAnyEntries.test.js) + #### .toBeExtensible() Use `.toBeExtensible` when checking if an object is extensible. ```js test('passes when value is extensible', () => { - expect({a: 1}).toBeExtensible(); + expect({ a: 1 }).toBeExtensible(); expect(1).not.toBeExtensible(); }); ``` +[Open toBeExtensible in REPL](https://codesandbox.io/s/github/jest-community/jest-extended/tree/master/docs/sandbox?module=%2Fmatchers%2Fobject%2FtoBeExtensible.test.js) + #### .toBeFrozen() Use `.toBeFrozen` when checking if an object is frozen. @@ -771,6 +851,8 @@ test('passes when value is a string', () => { }); ``` +[Open toBeString in REPL](https://codesandbox.io/s/github/jest-community/jest-extended/tree/master/docs/sandbox?module=%2Fmatchers%2Fstring%2FtoBeString.test.js) + #### .toBeHexadecimal(string) Use `.toBeHexadecimal` when checking if a value is a valid HTML hexadecimal color. @@ -784,6 +866,8 @@ test('passes when value is a valid hexadecimal', () => { }); ``` +[Open toBeHexadecimal in REPL](https://codesandbox.io/s/github/jest-community/jest-extended/tree/master/docs/sandbox?module=%2Fmatchers%2Fstring%2FtoBeHexadecimal.test.js) + #### .toEqualCaseInsensitive(string) Use `.toEqualCaseInsensitive` when checking if a string is equal (===) to another ignoring the casing of both strings. @@ -798,6 +882,8 @@ test('passes when strings are equal ignoring case', () => { }); ``` +[Open toEqualCaseInsensitive in REPL](https://codesandbox.io/s/github/jest-community/jest-extended/tree/master/docs/sandbox?module=%2Fmatchers%2Fstring%2FtoEqualCaseInsensitive.test.js) + #### .toStartWith(prefix) Use `.toStartWith` when checking if a `String` starts with a given `String` prefix. @@ -809,6 +895,8 @@ test('passes when value is starts with given string', () => { }); ``` +[Open toStartWith in REPL](https://codesandbox.io/s/github/jest-community/jest-extended/tree/master/docs/sandbox?module=%2Fmatchers%2Fstring%2FtoStartWith.test.js) + #### .toEndWith(suffix) Use `.toEndWith` when checking if a `String` ends with a given `String` suffix. @@ -820,6 +908,8 @@ test('passes when value is ends with given string', () => { }); ``` +[Open toEndWith in REPL](https://codesandbox.io/s/github/jest-community/jest-extended/tree/master/docs/sandbox?module=%2Fmatchers%2Fstring%2FtoEndWith.test.js) + #### .toInclude(substring) Use `.toInclude` when checking if a `String` includes the given `String` substring. @@ -831,6 +921,8 @@ test('passes when value includes substring', () => { }); ``` +[Open toInclude in REPL](https://codesandbox.io/s/github/jest-community/jest-extended/tree/master/docs/sandbox?module=%2Fmatchers%2Fstring%2FtoInclude.test.js) + #### .toIncludeRepeated(substring, times) Use `.toIncludeRepeated` when checking if a `String` includes the given `String` substring the correct number of times. @@ -842,6 +934,8 @@ test('passes when value includes substring n times', () => { }); ``` +[Open toIncludeRepeated in REPL](https://codesandbox.io/s/github/jest-community/jest-extended/tree/master/docs/sandbox?module=%2Fmatchers%2Fstring%2FtoIncludeRepeated.test.js) + #### .toIncludeMultiple([substring]) Use `.toIncludeMultiple` when checking if a `String` includes all of the given substrings. @@ -853,6 +947,8 @@ test('passes when value includes all substrings', () => { }); ``` +[Open toIncludeMultiple in REPL](https://codesandbox.io/s/github/jest-community/jest-extended/tree/master/docs/sandbox?module=%2Fmatchers%2Fstring%2FtoIncludeMultiple.test.js) + ## Contributors diff --git a/docs/EXAMPLES.md b/docs/EXAMPLES.md deleted file mode 100644 index e8f6743f..00000000 --- a/docs/EXAMPLES.md +++ /dev/null @@ -1,3 +0,0 @@ -# Examples - -☹️ None created yet, fancy adding some? See [CONTRIBUTING](/CONTRIBUTING.md) diff --git a/docs/README.template.md b/docs/README.template.md new file mode 100644 index 00000000..849e8ff5 --- /dev/null +++ b/docs/README.template.md @@ -0,0 +1,365 @@ +
+

jest-extended

+ + πŸƒπŸ’ͺ + + Additional Jest matchers +
+ +
+ +[![Build Status](https://img.shields.io/travis/jest-community/jest-extended.svg?style=flat-square)](https://travis-ci.org/jest-community/jest-extended) +[![Code Coverage](https://img.shields.io/codecov/c/github/jest-community/jest-extended.svg?style=flat-square)](https://codecov.io/github/jest-community/jest-extended) +[![version](https://img.shields.io/npm/v/jest-extended.svg?style=flat-square)](https://www.npmjs.com/package/jest-extended) +[![downloads](https://img.shields.io/npm/dm/jest-extended.svg?style=flat-square)](http://npm-stat.com/charts.html?package=jest-extended&from=2017-09-14) +[![MIT License](https://img.shields.io/npm/l/jest-extended.svg?style=flat-square)](https://github.com/jest-community/jest-extended/blob/master/LICENSE) +[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com) +[![Roadmap](https://img.shields.io/badge/%F0%9F%93%94-roadmap-CD9523.svg?style=flat-square)](https://github.com/jest-community/jest-extended/blob/master/docs/ROADMAP.md) +[![Examples](https://img.shields.io/badge/%F0%9F%92%A1-examples-ff615b.svg?style=flat-square)](https://github.com/jest-community/jest-extended/blob/master/docs/EXAMPLES.md) + +## 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](/CONTRIBUTING.md) docs as a way of getting started. + +--- + +- [Problem](#problem) +- [Solution](#solution) +- [Contributing](#contributing) +- [Installation](#installation) +- [Setup](#setup) +- [jest-extended REPL](https://codesandbox.io/s/github/jest-community/jest-extended/tree/master/docs/sandbox?module=%2Fmatchers%2FtoBeEmpty.test.js) +- [API](#api) + - [.pass(message)](#passmessage) + - [.fail(message)](#failmessage) + - [.toBeEmpty()](#tobeempty) + - [.toBeOneOf([members])](#tobeoneofmembers) + - [.toBeNil()](#tobenil) + - [.toSatisfy(predicate)](#tosatisfypredicate) + - [Array](#array) + - [.toBeArray()](#tobearray) + - [.toBeArrayOfSize()](#tobearrayofsize) + - [.toIncludeAllMembers([members])](#toincludeallmembersmembers) + - [.toIncludeAnyMembers([members])](#toincludeanymembersmembers) + - [.toIncludeSameMembers([members])](#toincludesamemembersmembers) + - [.toSatisfyAll(predicate)](#tosatisfyallpredicate) + - [Boolean](#boolean) + - [.toBeBoolean()](#tobeboolean) + - [.toBeTrue()](#tobetrue) + - [.toBeFalse()](#tobefalse) + - [Date](#date) + - [.toBeDate()](#tobedate) + - [.toBeValidDate()](#tobevaliddate) + - [.toBeAfter(date)](#tobeafterdate) + - [.toBeBefore(date)](#tobebeforedate) + - Further proposals in [#117](https://github.com/jest-community/jest-extended/issues/117) PRs welcome + - [Function](#function) + - [.toBeFunction()](#tobefunction) + - [Mock](#mock) + - [.toHaveBeenCalledBefore()](#tohavebeencalledbefore) + - [.toHaveBeenCalledAfter()](#tohavebeencalledafter) + - [Number](#number) + - [.toBeNumber()](#tobenumber) + - [.toBeNaN()](#tobenan) + - [.toBeFinite()](#tobefinite) + - [.toBePositive()](#tobepositive) + - [.toBeNegative()](#tobenegative) + - [.toBeEven()](#tobeeven) + - [.toBeOdd()](#tobeodd) + - [.toBeWithin(start, end)](#tobewithinstart--end) + - [Object](#object) + - [.toBeObject()](#tobeobject) + - [.toContainKey(key)](#tocontainkeykey) + - [.toContainKeys([keys])](#tocontainkeyskeys) + - [.toContainAllKeys([keys])](#tocontainallkeyskeys) + - [.toContainAnyKeys([keys])](#tocontainanykeyskeys) + - [.toContainValue(value)](#tocontainvaluevalue) + - [.toContainValues([values])](#tocontainvaluesvalues) + - [.toContainAllValues([values])](#tocontainallvaluesvalues) + - [.toContainAnyValues([values])](#tocontainanyvaluesvalues) + - [.toContainEntry([key, value])](#tocontainentrykey--value) + - [.toContainEntries([[key, value]])](#tocontainentrieskey--value) + - [.toContainAllEntries([[key, value]])](#tocontainallentrieskey--value) + - [.toContainAnyEntries([[key, value]])](#tocontainanyentrieskey--value) + - [.toBeExtensible()](#tobeextensible) + - [.toBeFrozen()](#tobefrozen) + - [.toBeSealed()](#tobesealed) + - [~~Promise~~](#promise) + - [String](#string) + - [.toBeString()](#tobestring) + - [.toBeHexadecimal(string)](#tobehexadecimal) + - [.toEqualCaseInsensitive(string)](#toequalcaseinsensitivestring) + - [.toStartWith(prefix)](#tostartwithprefix) + - [.toEndWith(suffix)](#toendwithsuffix) + - [.toInclude(substring)](#toincludesubstring) + - [.toIncludeRepeated(substring, times)](#toincluderepeatedsubstring--times) + - [.toIncludeMultiple([substring])](#toincludemultiplesubstring) +- [Contributors](#contributors) +- [LICENSE](#license) + +## Installation + +With npm: +```sh +npm install --save-dev jest-extended +``` + +With yarn: +```sh +yarn add -D jest-extended +``` + +## Setup + +Add jest-extended to your Jest setupTestFrameworkScriptFile configuration. [See for help](http://facebook.github.io/jest/docs/en/configuration.html#setuptestframeworkscriptfile-string) + +``` json +"jest": { + "setupTestFrameworkScriptFile": "jest-extended" +} +``` + +If you are already using another test framework, like [jest-chain](https://github.com/mattphillips/jest-chain), then you should create a test setup file and `require` each of the frameworks you are using. + +For example: + +```js +// ./testSetup.js +require('jest-extended'); +require('any other test framework libraries you are using'); +``` + +Then in your Jest config: + +```json +"jest": { + "setupTestFrameworkScriptFile": "./testSetup.js" +} +``` + +## API + +#### .pass(message) + +_Note: Currently unimplemented_ + +Passing assertion. + +```js +expect().pass('should pass'); +``` + +#### .fail(message) + +_Note: Currently unimplemented_ + +Failing assertion. + +```js +expect().fail('test should fail'); +``` + +{{sandbox/matchers/toBeEmpty.test.js}} + +{{sandbox/matchers/toBeOneOf.test.js}} + +{{sandbox/matchers/toBeNil.test.js}} + +{{sandbox/matchers/toSatisfy.test.js}} + +### Array + +{{sandbox/matchers/array/toBeArray.test.js}} + +{{sandbox/matchers/array/toBeArrayOfSize.test.js}} + +{{sandbox/matchers/array/toIncludeAllMembers.test.js}} + +{{sandbox/matchers/array/toIncludeAnyMembers.test.js}} + +{{sandbox/matchers/array/toIncludeSameMembers.test.js}} + +{{sandbox/matchers/array/toSatisfyAll.test.js}} + +### Boolean + +{{sandbox/matchers/boolean/toBeBoolean.test.js}} + +{{sandbox/matchers/boolean/toBeTrue.test.js}} + +{{sandbox/matchers/boolean/toBeFalse.test.js}} + +### ~~Date~~ + +Proposal in #117 (*under development*) + +{{sandbox/matchers/date/toBeDate.test.js}} + +{{sandbox/matchers/date/toBeValidDate.test.js}} + +{{sandbox/matchers/date/toBeAfter.test.js}} + +{{sandbox/matchers/date/toBeBefore.test.js}} + +### Function + +{{sandbox/matchers/function/toBeFunction.test.js}} + +### Mock + +#### .toHaveBeenCalledBefore() + +Use `.toHaveBeenCalledBefore` when checking if a `Mock` was called before another `Mock`. + +_Note: Required Jest version >=23_ + +```js +it('calls mock1 before mock2', () => { + const mock1 = jest.fn(); + const mock2 = jest.fn(); + + mock1(); + mock2(); + mock1(); + + expect(mock1).toHaveBeenCalledBefore(mock2); +}); +``` + +#### .toHaveBeenCalledAfter() + +Use `.toHaveBeenCalledAfter` when checking if a `Mock` was called after another `Mock`. + +_Note: Required Jest version >=23_ + +```js +it('calls mock1 after mock2', () => { + const mock1 = jest.fn(); + const mock2 = jest.fn(); + + mock2(); + mock1(); + mock2(); + + expect(mock1).toHaveBeenCalledAfter(mock2); +}); +``` + +### Number + +{{sandbox/matchers/number/toBeNumber.test.js}} + +{{sandbox/matchers/number/toBeNaN.test.js}} + +{{sandbox/matchers/number/toBeFinite.test.js}} + +{{sandbox/matchers/number/toBePositive.test.js}} + +{{sandbox/matchers/number/toBeNegative.test.js}} + +{{sandbox/matchers/number/toBeEven.test.js}} + +{{sandbox/matchers/number/toBeOdd.test.js}} + +{{sandbox/matchers/number/toBeWithin.test.js}} + +### Object + +{{sandbox/matchers/object/toBeObject.test.js}} + +{{sandbox/matchers/object/toContainKey.test.js}} + +{{sandbox/matchers/object/toContainKeys.test.js}} + +{{sandbox/matchers/object/toContainAllKeys.test.js}} + +{{sandbox/matchers/object/toContainAnyKeys.test.js}} + +{{sandbox/matchers/object/toContainValue.test.js}} + +{{sandbox/matchers/object/toContainValues.test.js}} + +{{sandbox/matchers/object/toContainAllValues.test.js}} + +{{sandbox/matchers/object/toContainAnyValues.test.js}} + +{{sandbox/matchers/object/toContainEntry.test.js}} + +{{sandbox/matchers/object/toContainEntries.test.js}} + +{{sandbox/matchers/object/toContainAllEntries.test.js}} + +{{sandbox/matchers/object/toContainAnyEntries.test.js}} + +{{sandbox/matchers/object/toBeExtensible.test.js}} + +#### .toBeFrozen() + +Use `.toBeFrozen` when checking if an object is frozen. + +```js +test('passes when value is frozen', () => { + expect(Object.frozen({})).toBeFrozen(); + expect({}).not.toBeFrozen(); + expect(1).not.toBeFrozen(); +}); +``` + +#### .toBeSealed() + +Use `.toBeSealed` when checking if an object is sealed. + +```js +test('passes when value is sealed', () => { + expect(Object.seal({})).toBeSealed(); + expect({}).not.toBeSealed(); + expect(1).not.toBeSealed(); +}); +``` + +### ~~Promise~~ + +_No APIs proposed yet_ + +### String + +{{sandbox/matchers/string/toBeString.test.js}} + +{{sandbox/matchers/string/toBeHexadecimal.test.js}} + +{{sandbox/matchers/string/toEqualCaseInsensitive.test.js}} + +{{sandbox/matchers/string/toStartWith.test.js}} + +{{sandbox/matchers/string/toEndWith.test.js}} + +{{sandbox/matchers/string/toInclude.test.js}} + +{{sandbox/matchers/string/toIncludeRepeated.test.js}} + +{{sandbox/matchers/string/toIncludeMultiple.test.js}} + +## Contributors + + +| [
Matt Phillips](http://mattphillips.io)
[πŸ“](#blog-mattphillips "Blogposts") [πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=mattphillips "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=mattphillips "Documentation") [πŸ’‘](#example-mattphillips "Examples") [πŸš‡](#infra-mattphillips "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=mattphillips "Tests") | [
Stephen Bluck](https://github.com/stevebluck)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=stevebluck "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=stevebluck "Tests") | [
Christoffer Hasselberg](https://github.com/stofolus)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=stofolus "Code") | [
Brandon Newton](https://btnwtn.com)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=btnwtn "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=btnwtn "Documentation") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=btnwtn "Tests") | [
Devan Patel](http://www.devanpatel.me)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=devanp92 "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=devanp92 "Documentation") | [
Gary Leutheuser](https://GaryLeutheuser.github.io)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=GaryLeutheuser "Code") | [
Johan Lindgren](https://github.com/lindgr3n)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=lindgr3n "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=lindgr3n "Documentation") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=lindgr3n "Tests") | +| :---: | :---: | :---: | :---: | :---: | :---: | :---: | +| [
Andrew Hayward](http://andrewhayward.net)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=andrewhayward "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=andrewhayward "Tests") | [
Oliver Schneider](https://ols.io)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=olsio "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=olsio "Tests") | [
Tyle Whalen](https://github.com/tjwhalen16)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=tjwhalen16 "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=tjwhalen16 "Documentation") | [
Martius](https://github.com/martiuslim)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=martiuslim "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=martiuslim "Tests") | [
Eli Collis](https://github.com/ecollis6)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=ecollis6 "Code") | [
Marcin LichwaΕ‚a](https://github.com/marcinlichwala)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=marcinlichwala "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=marcinlichwala "Tests") | [
Massimo Prencipe](https://github.com/mprencipe)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=mprencipe "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=mprencipe "Tests") | +| [
mjmiles](https://github.com/mjmiles)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=mjmiles "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=mjmiles "Documentation") | [
Gary Meehan](https://github.com/garmeeh)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=garmeeh "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=garmeeh "Documentation") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=garmeeh "Tests") | [
Fredrik MΓ€kilΓ€](https://github.com/GitHug)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=GitHug "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=GitHug "Tests") | [
Daniel Reinoso](http://kloc.io/)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=danielr18 "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=danielr18 "Tests") | [
Chris Hut](https://github.com/tophernuts)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=tophernuts "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=tophernuts "Tests") | [
Kelvin Ly](https://github.com/cactorium)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=cactorium "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=cactorium "Tests") | [
Francis Ngo](https://github.com/francisngo)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=francisngo "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=francisngo "Tests") | +| [
Amish Shah](https://hydrabolt.me/)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=hydrabolt "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=hydrabolt "Tests") | [
Dave Cooper](http://davecooper.org)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=grug "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=grug "Tests") | [
Swann Polydor](https://github.com/soueuls)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=soueuls "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=soueuls "Tests") | [
vikneshwar](https://github.com/vikneshwar)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=vikneshwar "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=vikneshwar "Tests") | [
Budi Irawan](http://budiirawan.com)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=deerawan "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=deerawan "Tests") | [
Tejas Bubane](http://foss-geek.blogspot.com/)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=tejasbubane "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=tejasbubane "Tests") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=tejasbubane "Documentation") | [
Subinoy Ghosh](https://github.com/subinoy7)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=subinoy7 "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=subinoy7 "Tests") | +| [
Simen Bekkhus](https://github.com/SimenB)
[πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=SimenB "Documentation") | [
Orta](http://orta.io)
[πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=orta "Documentation") | [
Tom](https://jsdevtom.com)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=jsdevtom "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=jsdevtom "Documentation") [πŸ’‘](#example-jsdevtom "Examples") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=jsdevtom "Tests") | [
Lucian Buzzo](https://github.com/LucianBuzzo)
| [
Thiago Delgado Pinto](https://github.com/thiagodp)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=thiagodp "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=thiagodp "Documentation") [πŸ’‘](#example-thiagodp "Examples") [πŸ€”](#ideas-thiagodp "Ideas, Planning, & Feedback") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=thiagodp "Tests") | [
Ragnar Laud](https://github.com/xprn)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=xprn "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=xprn "Documentation") | [
Luiz AmΓ©rico](https://github.com/blikblum)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=blikblum "Code") | +| [
Frederick Fogerty](https://github.com/frederickfogerty)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=frederickfogerty "Code") [πŸ€”](#ideas-frederickfogerty "Ideas, Planning, & Feedback") | [
Benjamin Kay](https://github.com/benjaminkay93)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=benjaminkay93 "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=benjaminkay93 "Documentation") | [
Gilles De Mey](https://demey.io)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=gillesdemey "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=gillesdemey "Documentation") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=gillesdemey "Tests") | [
Deniz Dogan](https://github.com/denizdogan)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=denizdogan "Code") | [
Mikey Powers](https://github.com/mvpowers)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=mvpowers "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=mvpowers "Documentation") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=mvpowers "Tests") | + + +## LICENSE + +[MIT](/LICENSE) diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md deleted file mode 100644 index 7765c510..00000000 --- a/docs/ROADMAP.md +++ /dev/null @@ -1,3 +0,0 @@ -# Project Roadmap - -## Want something added? Feel free to open an issue :) \ No newline at end of file diff --git a/docs/sandbox/.prettierrc b/docs/sandbox/.prettierrc new file mode 100644 index 00000000..d271427d --- /dev/null +++ b/docs/sandbox/.prettierrc @@ -0,0 +1,11 @@ +{ + "printWidth": 80, + "tabWidth": 2, + "useTabs": false, + "semi": true, + "singleQuote": true, + "trailingComma": "none", + "bracketSpacing": true, + "jsxBracketSameLine": false, + "fluid": false +} \ No newline at end of file diff --git a/docs/sandbox/matchers/array/toBeArray.test.js b/docs/sandbox/matchers/array/toBeArray.test.js new file mode 100644 index 00000000..da3b41ca --- /dev/null +++ b/docs/sandbox/matchers/array/toBeArray.test.js @@ -0,0 +1,11 @@ +/* +#### .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(); +}); diff --git a/docs/sandbox/matchers/array/toBeArrayOfSize.test.js b/docs/sandbox/matchers/array/toBeArrayOfSize.test.js new file mode 100644 index 00000000..aa60f62a --- /dev/null +++ b/docs/sandbox/matchers/array/toBeArrayOfSize.test.js @@ -0,0 +1,11 @@ +/* +#### .toBeArrayOfSize() + +Use `.toBeArrayOfSize` when checking if a value is an `Array` of size x. +*/ + +test('passes when value is an array', () => { + expect([]).toBeArrayOfSize(0); + expect([1]).toBeArrayOfSize(1); + expect(true).not.toBeArrayOfSize(1); +}); diff --git a/docs/sandbox/matchers/array/toIncludeAllMembers.test.js b/docs/sandbox/matchers/array/toIncludeAllMembers.test.js new file mode 100644 index 00000000..e5a074ca --- /dev/null +++ b/docs/sandbox/matchers/array/toIncludeAllMembers.test.js @@ -0,0 +1,10 @@ +/* +#### .toIncludeAllMembers([members]) + +Use `.toIncludeAllMembers` 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]).toIncludeAllMembers([2, 1, 3]); + expect([1, 2, 2]).toIncludeAllMembers([2, 1]); +}); diff --git a/docs/sandbox/matchers/array/toIncludeAnyMembers.test.js b/docs/sandbox/matchers/array/toIncludeAnyMembers.test.js new file mode 100644 index 00000000..cd3a2c90 --- /dev/null +++ b/docs/sandbox/matchers/array/toIncludeAnyMembers.test.js @@ -0,0 +1,11 @@ +/* +#### .toIncludeAnyMembers([members]) + +Use `.toIncludeAnyMembers` when checking if an `Array` contains any of the members of a given set. +*/ + +test('passes when given array values match any of the members in the set', () => { + expect([1, 2, 3]).toIncludeAnyMembers([2, 1, 3]); + expect([1, 2, 2]).toIncludeAnyMembers([2]); + expect([1, 2, 2]).not.toIncludeAnyMembers([3]); +}); diff --git a/docs/sandbox/matchers/array/toIncludeSameMembers.test.js b/docs/sandbox/matchers/array/toIncludeSameMembers.test.js new file mode 100644 index 00000000..d730d46d --- /dev/null +++ b/docs/sandbox/matchers/array/toIncludeSameMembers.test.js @@ -0,0 +1,10 @@ +/* +#### .toIncludeSameMembers([members]) + +Use `.toIncludeSameMembers` when checking if two arrays contain equal values, in any order. +*/ + +test('passes when arrays match in a different order', () => { + expect([1, 2, 3]).toIncludeSameMembers([3, 1, 2]); + expect([{ foo: 'bar' }, { baz: 'qux' }]).toIncludeSameMembers([{ baz: 'qux' }, { foo: 'bar' }]); +}); diff --git a/docs/sandbox/matchers/array/toSatisfyAll.test.js b/docs/sandbox/matchers/array/toSatisfyAll.test.js new file mode 100644 index 00000000..2298cbbd --- /dev/null +++ b/docs/sandbox/matchers/array/toSatisfyAll.test.js @@ -0,0 +1,11 @@ +/* +#### .toSatisfyAll(predicate) + +Use `.toSatisfyAll` when you want to use a custom matcher by supplying a predicate function that returns a `Boolean` for all values in an array. +*/ + +test('passes when all values in array pass given predicate', () => { + const isOdd = el => el % 2 === 1; + expect([1, 3, 5, 7]).toSatisfyAll(isOdd); + expect([1, 3, 4, 5, 7]).not.toSatisfyAll(isOdd); +}); diff --git a/docs/sandbox/matchers/boolean/toBeBoolean.test.js b/docs/sandbox/matchers/boolean/toBeBoolean.test.js new file mode 100644 index 00000000..3543ca61 --- /dev/null +++ b/docs/sandbox/matchers/boolean/toBeBoolean.test.js @@ -0,0 +1,12 @@ +/* +#### .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(); +}); diff --git a/docs/sandbox/matchers/boolean/toBeFalse.test.js b/docs/sandbox/matchers/boolean/toBeFalse.test.js new file mode 100644 index 00000000..5b752476 --- /dev/null +++ b/docs/sandbox/matchers/boolean/toBeFalse.test.js @@ -0,0 +1,10 @@ +/* +#### .toBeFalse() + +Use `.toBeFalse` when checking a value is equal (===) to `false`. +*/ + +test('returns false', () => { + expect(false).toBeFalse(); + expect(true).not.toBeFalse(); +}); diff --git a/docs/sandbox/matchers/boolean/toBeTrue.test.js b/docs/sandbox/matchers/boolean/toBeTrue.test.js new file mode 100644 index 00000000..69ee3746 --- /dev/null +++ b/docs/sandbox/matchers/boolean/toBeTrue.test.js @@ -0,0 +1,11 @@ +/* +#### .toBeTrue() + +Use `.toBeTrue` when checking a value is equal (===) to `true`. +*/ + +test('is jest cool', () => { + const isJestCool = () => true; + expect(isJestCool()).toBeTrue(); + expect(false).not.toBeTrue(); +}); diff --git a/docs/sandbox/matchers/date/toBeAfter.test.js b/docs/sandbox/matchers/date/toBeAfter.test.js new file mode 100644 index 00000000..0b9ccd4f --- /dev/null +++ b/docs/sandbox/matchers/date/toBeAfter.test.js @@ -0,0 +1,10 @@ +/* +#### .toBeAfter(date) + +Use `.toBeAfter` when checking if a date occurs after `date`. +*/ + +test('passes when input is after date', () => { + expect(new Date('01/01/2019')).toBeAfter(new Date('01/01/2018')); + expect('01/01/2018').not.toBeAfter(new Date('01/01/2019')); +}); diff --git a/docs/sandbox/matchers/date/toBeBefore.test.js b/docs/sandbox/matchers/date/toBeBefore.test.js new file mode 100644 index 00000000..b43dce65 --- /dev/null +++ b/docs/sandbox/matchers/date/toBeBefore.test.js @@ -0,0 +1,10 @@ +/* +#### .toBeBefore(date) + +Use `.toBeBefore` when checking if a date occurs before `date`. +*/ + +test('passes when input is before date', () => { + expect(new Date('01/01/2018')).toBeBefore(new Date('01/01/2019')); + expect('01/01/2019').not.toBeBefore(new Date('01/01/2018')); +}); diff --git a/docs/sandbox/matchers/date/toBeDate.test.js b/docs/sandbox/matchers/date/toBeDate.test.js new file mode 100644 index 00000000..9f432340 --- /dev/null +++ b/docs/sandbox/matchers/date/toBeDate.test.js @@ -0,0 +1,12 @@ +/* +#### .toBeDate() + +Use `.toBeDate` when checking if a value is a `Date`. +*/ + +test('passes when value is a date', () => { + expect(new Date()).toBeDate(); + expect('01/01/2018').not.toBeDate(); + expect(new Date('01/01/2018')).toBeDate(); + expect(undefined).not.toBeDate(); +}); diff --git a/docs/sandbox/matchers/date/toBeValidDate.test.js b/docs/sandbox/matchers/date/toBeValidDate.test.js new file mode 100644 index 00000000..0aa4b35f --- /dev/null +++ b/docs/sandbox/matchers/date/toBeValidDate.test.js @@ -0,0 +1,13 @@ +/* +#### .toBeValidDate() + +Use `.toBeValidDate` when checking if a given `Date` object is valid. +*/ + +test('passes when Date is valid', () => { + expect(new Date()).toBeValidDate(); + expect('01/01/2018').not.toBeValidDate(); + expect(new Date('01/01/2018')).toBeValidDate(); + expect(new Date('01/90/2018')).not.toBeValidDate(); + expect(undefined).not.toBeValidDate(); +}); diff --git a/docs/sandbox/matchers/function/toBeFunction.test.js b/docs/sandbox/matchers/function/toBeFunction.test.js new file mode 100644 index 00000000..0167a002 --- /dev/null +++ b/docs/sandbox/matchers/function/toBeFunction.test.js @@ -0,0 +1,12 @@ +/* +#### .toBeFunction() + +Use `.toBeFunction` when checking if a value is a `Function`. +*/ + +test('passes when value is a function', () => { + function noop() {} + expect(() => {}).toBeFunction(); + expect(noop).toBeFunction(); + expect(true).not.toBeFunction(); +}); diff --git a/docs/sandbox/matchers/number/toBeEven.test.js b/docs/sandbox/matchers/number/toBeEven.test.js new file mode 100644 index 00000000..5a75d73b --- /dev/null +++ b/docs/sandbox/matchers/number/toBeEven.test.js @@ -0,0 +1,11 @@ +/* +#### .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(); +}); diff --git a/docs/sandbox/matchers/number/toBeFinite.test.js b/docs/sandbox/matchers/number/toBeFinite.test.js new file mode 100644 index 00000000..2fb8b373 --- /dev/null +++ b/docs/sandbox/matchers/number/toBeFinite.test.js @@ -0,0 +1,11 @@ +/* +#### .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(); +}); diff --git a/docs/sandbox/matchers/number/toBeNaN.test.js b/docs/sandbox/matchers/number/toBeNaN.test.js new file mode 100644 index 00000000..7ffb218d --- /dev/null +++ b/docs/sandbox/matchers/number/toBeNaN.test.js @@ -0,0 +1,10 @@ +/* +#### .toBeNaN() + +Use `.toBeNaN` when checking a value is `NaN`. +*/ + +test('passes when value is NaN', () => { + expect(NaN).toBeNaN(); + expect(1).not.toBeNaN(); +}); diff --git a/docs/sandbox/matchers/number/toBeNegative.test.js b/docs/sandbox/matchers/number/toBeNegative.test.js new file mode 100644 index 00000000..0acba5bb --- /dev/null +++ b/docs/sandbox/matchers/number/toBeNegative.test.js @@ -0,0 +1,12 @@ +/* +#### .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(); +}); diff --git a/docs/sandbox/matchers/number/toBeNumber.test.js b/docs/sandbox/matchers/number/toBeNumber.test.js new file mode 100644 index 00000000..1efac018 --- /dev/null +++ b/docs/sandbox/matchers/number/toBeNumber.test.js @@ -0,0 +1,12 @@ +/* +#### .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(); +}); diff --git a/docs/sandbox/matchers/number/toBeOdd.test.js b/docs/sandbox/matchers/number/toBeOdd.test.js new file mode 100644 index 00000000..e7c2ed64 --- /dev/null +++ b/docs/sandbox/matchers/number/toBeOdd.test.js @@ -0,0 +1,11 @@ +/* +#### .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(); +}); diff --git a/docs/sandbox/matchers/number/toBePositive.test.js b/docs/sandbox/matchers/number/toBePositive.test.js new file mode 100644 index 00000000..2fc6bdb1 --- /dev/null +++ b/docs/sandbox/matchers/number/toBePositive.test.js @@ -0,0 +1,12 @@ +/* +#### .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(); +}); diff --git a/docs/sandbox/matchers/number/toBeWithin.test.js b/docs/sandbox/matchers/number/toBeWithin.test.js new file mode 100644 index 00000000..4859fb33 --- /dev/null +++ b/docs/sandbox/matchers/number/toBeWithin.test.js @@ -0,0 +1,11 @@ +/* +#### .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); +}); diff --git a/docs/sandbox/matchers/object/toBeExtensible.test.js b/docs/sandbox/matchers/object/toBeExtensible.test.js new file mode 100644 index 00000000..e75ddd98 --- /dev/null +++ b/docs/sandbox/matchers/object/toBeExtensible.test.js @@ -0,0 +1,10 @@ +/* +#### .toBeExtensible() + +Use `.toBeExtensible` when checking if an object is extensible. +*/ + +test('passes when value is extensible', () => { + expect({ a: 1 }).toBeExtensible(); + expect(1).not.toBeExtensible(); +}); diff --git a/docs/sandbox/matchers/object/toBeObject.test.js b/docs/sandbox/matchers/object/toBeObject.test.js new file mode 100644 index 00000000..e772de9d --- /dev/null +++ b/docs/sandbox/matchers/object/toBeObject.test.js @@ -0,0 +1,11 @@ +/* +#### .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(); +}); diff --git a/docs/sandbox/matchers/object/toContainAllEntries.test.js b/docs/sandbox/matchers/object/toContainAllEntries.test.js new file mode 100644 index 00000000..e6ae094a --- /dev/null +++ b/docs/sandbox/matchers/object/toContainAllEntries.test.js @@ -0,0 +1,11 @@ +/* +#### .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']]); +}); diff --git a/docs/sandbox/matchers/object/toContainAllKeys.test.js b/docs/sandbox/matchers/object/toContainAllKeys.test.js new file mode 100644 index 00000000..ef897a32 --- /dev/null +++ b/docs/sandbox/matchers/object/toContainAllKeys.test.js @@ -0,0 +1,12 @@ +/* +#### .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']); +}); diff --git a/docs/sandbox/matchers/object/toContainAllValues.test.js b/docs/sandbox/matchers/object/toContainAllValues.test.js new file mode 100644 index 00000000..2807becd --- /dev/null +++ b/docs/sandbox/matchers/object/toContainAllValues.test.js @@ -0,0 +1,12 @@ +/* +#### .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']); +}); diff --git a/docs/sandbox/matchers/object/toContainAnyEntries.test.js b/docs/sandbox/matchers/object/toContainAnyEntries.test.js new file mode 100644 index 00000000..04e51b1b --- /dev/null +++ b/docs/sandbox/matchers/object/toContainAnyEntries.test.js @@ -0,0 +1,13 @@ +/* +#### .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']]); +}); diff --git a/docs/sandbox/matchers/object/toContainAnyKeys.test.js b/docs/sandbox/matchers/object/toContainAnyKeys.test.js new file mode 100644 index 00000000..25e64b73 --- /dev/null +++ b/docs/sandbox/matchers/object/toContainAnyKeys.test.js @@ -0,0 +1,13 @@ +/* +#### .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']); +}); diff --git a/docs/sandbox/matchers/object/toContainAnyValues.test.js b/docs/sandbox/matchers/object/toContainAnyValues.test.js new file mode 100644 index 00000000..d8d04bd9 --- /dev/null +++ b/docs/sandbox/matchers/object/toContainAnyValues.test.js @@ -0,0 +1,13 @@ +/* +#### .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']); +}); diff --git a/docs/sandbox/matchers/object/toContainEntries.test.js b/docs/sandbox/matchers/object/toContainEntries.test.js new file mode 100644 index 00000000..8ee7c728 --- /dev/null +++ b/docs/sandbox/matchers/object/toContainEntries.test.js @@ -0,0 +1,12 @@ +/* +#### .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']]); +}); diff --git a/docs/sandbox/matchers/object/toContainEntry.test.js b/docs/sandbox/matchers/object/toContainEntry.test.js new file mode 100644 index 00000000..e75df796 --- /dev/null +++ b/docs/sandbox/matchers/object/toContainEntry.test.js @@ -0,0 +1,13 @@ +/* +#### .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']); +}); diff --git a/docs/sandbox/matchers/object/toContainKey.test.js b/docs/sandbox/matchers/object/toContainKey.test.js new file mode 100644 index 00000000..b8bb5e7b --- /dev/null +++ b/docs/sandbox/matchers/object/toContainKey.test.js @@ -0,0 +1,13 @@ +/* +#### .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'); +}); diff --git a/docs/sandbox/matchers/object/toContainKeys.test.js b/docs/sandbox/matchers/object/toContainKeys.test.js new file mode 100644 index 00000000..3b2ddb01 --- /dev/null +++ b/docs/sandbox/matchers/object/toContainKeys.test.js @@ -0,0 +1,12 @@ +/* +#### .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']); +}); diff --git a/docs/sandbox/matchers/object/toContainValue.test.js b/docs/sandbox/matchers/object/toContainValue.test.js new file mode 100644 index 00000000..400ad039 --- /dev/null +++ b/docs/sandbox/matchers/object/toContainValue.test.js @@ -0,0 +1,12 @@ +/* +#### .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'); +}); diff --git a/docs/sandbox/matchers/object/toContainValues.test.js b/docs/sandbox/matchers/object/toContainValues.test.js new file mode 100644 index 00000000..5e92fdbc --- /dev/null +++ b/docs/sandbox/matchers/object/toContainValues.test.js @@ -0,0 +1,12 @@ +/* +#### .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']); +}); diff --git a/docs/sandbox/matchers/string/toBeHexadecimal.test.js b/docs/sandbox/matchers/string/toBeHexadecimal.test.js new file mode 100644 index 00000000..79633951 --- /dev/null +++ b/docs/sandbox/matchers/string/toBeHexadecimal.test.js @@ -0,0 +1,12 @@ +/* +#### .toBeHexadecimal(string) + +Use `.toBeHexadecimal` when checking if a value is a valid HTML hexadecimal color. +*/ + +test('passes when value is a valid hexadecimal', () => { + expect('#abc123').toBeHexadecimal(); + expect('#FFF').toBeHexadecimal(); + expect('#000000').toBeHexadecimal(); + expect('#123ffg').not.toBeHexadecimal(); +}); diff --git a/docs/sandbox/matchers/string/toBeString.test.js b/docs/sandbox/matchers/string/toBeString.test.js new file mode 100644 index 00000000..f01549cc --- /dev/null +++ b/docs/sandbox/matchers/string/toBeString.test.js @@ -0,0 +1,12 @@ +/* +#### .toBeString() + +Use `.toBeString` when checking if a value is a `String`. +*/ + +test('passes when value is a string', () => { + expect('').toBeString(); + expect('hello').toBeString(); + expect(new String('hello')).toBeString(); + expect(true).not.toBeString(); +}); diff --git a/docs/sandbox/matchers/string/toEndWith.test.js b/docs/sandbox/matchers/string/toEndWith.test.js new file mode 100644 index 00000000..1da5df7f --- /dev/null +++ b/docs/sandbox/matchers/string/toEndWith.test.js @@ -0,0 +1,10 @@ +/* +#### .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'); +}); diff --git a/docs/sandbox/matchers/string/toEqualCaseInsensitive.test.js b/docs/sandbox/matchers/string/toEqualCaseInsensitive.test.js new file mode 100644 index 00000000..194fd6ee --- /dev/null +++ b/docs/sandbox/matchers/string/toEqualCaseInsensitive.test.js @@ -0,0 +1,13 @@ +/* +#### .toEqualCaseInsensitive(string) + +Use `.toEqualCaseInsensitive` 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').toEqualCaseInsensitive('hello world'); + expect('hello WORLD').toEqualCaseInsensitive('HELLO world'); + expect('HELLO WORLD').toEqualCaseInsensitive('hello world'); + expect('hello world').toEqualCaseInsensitive('HELLO WORLD'); + expect('hello world').not.toEqualCaseInsensitive('hello'); +}); diff --git a/docs/sandbox/matchers/string/toInclude.test.js b/docs/sandbox/matchers/string/toInclude.test.js new file mode 100644 index 00000000..bf484e7a --- /dev/null +++ b/docs/sandbox/matchers/string/toInclude.test.js @@ -0,0 +1,10 @@ +/* +#### .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'); +}); diff --git a/docs/sandbox/matchers/string/toIncludeMultiple.test.js b/docs/sandbox/matchers/string/toIncludeMultiple.test.js new file mode 100644 index 00000000..7980e7e2 --- /dev/null +++ b/docs/sandbox/matchers/string/toIncludeMultiple.test.js @@ -0,0 +1,10 @@ +/* +#### .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').toIncludeMultiple(['world', 'hello']); + expect('hello world').not.toIncludeMultiple(['world', 'hello', 'bob']); +}); diff --git a/docs/sandbox/matchers/string/toIncludeRepeated.test.js b/docs/sandbox/matchers/string/toIncludeRepeated.test.js new file mode 100644 index 00000000..6e3327d1 --- /dev/null +++ b/docs/sandbox/matchers/string/toIncludeRepeated.test.js @@ -0,0 +1,10 @@ +/* +#### .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); +}); diff --git a/docs/sandbox/matchers/string/toStartWith.test.js b/docs/sandbox/matchers/string/toStartWith.test.js new file mode 100644 index 00000000..cb4a7542 --- /dev/null +++ b/docs/sandbox/matchers/string/toStartWith.test.js @@ -0,0 +1,10 @@ +/* +#### .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'); +}); diff --git a/docs/sandbox/matchers/toBeEmpty.test.js b/docs/sandbox/matchers/toBeEmpty.test.js new file mode 100644 index 00000000..5fb6d13b --- /dev/null +++ b/docs/sandbox/matchers/toBeEmpty.test.js @@ -0,0 +1,20 @@ +/* +#### .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(); +}); diff --git a/docs/sandbox/matchers/toBeNil.test.js b/docs/sandbox/matchers/toBeNil.test.js new file mode 100644 index 00000000..829a040d --- /dev/null +++ b/docs/sandbox/matchers/toBeNil.test.js @@ -0,0 +1,11 @@ +/* +#### .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(); +}); diff --git a/docs/sandbox/matchers/toBeOneOf.test.js b/docs/sandbox/matchers/toBeOneOf.test.js new file mode 100644 index 00000000..1f9a718a --- /dev/null +++ b/docs/sandbox/matchers/toBeOneOf.test.js @@ -0,0 +1,10 @@ +/* +#### .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]); +}); diff --git a/docs/sandbox/matchers/toSatisfy.test.js b/docs/sandbox/matchers/toSatisfy.test.js new file mode 100644 index 00000000..81228f2e --- /dev/null +++ b/docs/sandbox/matchers/toSatisfy.test.js @@ -0,0 +1,12 @@ +/* +#### .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); +}); diff --git a/docs/sandbox/package.json b/docs/sandbox/package.json new file mode 100644 index 00000000..d4428602 --- /dev/null +++ b/docs/sandbox/package.json @@ -0,0 +1,19 @@ +{ + "name": "jest-extended-sandbox", + "version": "1.0.0", + "homepage": "https://github.com/jest-community/jest-extended", + "main": "setup.js", + "dependencies": { + "babel-jest": "20.0.3", + "jest": "20.0.4", + "jest-extended": "0.9.0" + }, + "keywords": [ + "jest", + "jest-extended", + "repl", + "sandbox", + "codesandbox" + ], + "description": "A sandbox repl of available jest-extended matchers." +} \ No newline at end of file diff --git a/docs/sandbox/sandbox.config.json b/docs/sandbox/sandbox.config.json new file mode 100644 index 00000000..6f1a1aeb --- /dev/null +++ b/docs/sandbox/sandbox.config.json @@ -0,0 +1,5 @@ +{ + "infiniteLoopProtection": true, + "hardReloadOnChange": false, + "view": "tests" +} \ No newline at end of file diff --git a/docs/sandbox/setup.js b/docs/sandbox/setup.js new file mode 100644 index 00000000..3b7aff06 --- /dev/null +++ b/docs/sandbox/setup.js @@ -0,0 +1,2 @@ +import je from 'jest-extended/dist/matchers'; +expect.extend(je); diff --git a/package.json b/package.json index fa92ea6e..3b13d035 100644 --- a/package.json +++ b/package.json @@ -14,12 +14,13 @@ "build": "babel src -d dist --ignore *.test.js", "contributor": "all-contributors add", "contributor:gen": "all-contributors generate", + "generate-readme": "node scripts/readme.js", "lint": "eslint src", "lint:fix": "yarn lint -- --fix", "prepublishOnly": "yarn build", "precommit": "lint-staged", "prettier": "prettier 'src/**/*.js' --write --single-quote=true --print-width=120", - "test": "jest", + "test": "jest src", "test:coverage": "yarn test -- --coverage", "test:report": "codecov", "test:watch": "yarn test -- --watch" diff --git a/scripts/readme.js b/scripts/readme.js new file mode 100644 index 00000000..8602b829 --- /dev/null +++ b/scripts/readme.js @@ -0,0 +1,45 @@ +const readline = require('readline'); +const fs = require('fs'); +const path = require('path'); + +const ROOT = path.resolve(__dirname, '..'); +const DOCS = path.resolve(ROOT, 'docs'); + +const TEMPLATE_PATH = `${DOCS}/README.template.md`; +const README_PATH = `${ROOT}/README.md`; + +const IMPORT_PATTERN = new RegExp(/{{(\w|\/)+\.test.js}}/g); +const IMPORT_START = /{/g; +const IMPORT_END = /}/g; +const COMMENT_START = '/*'; +const COMMENT_END = '*/'; + +const SANDBOX_URL = 'https://codesandbox.io/s/github/jest-community/jest-extended/tree/master/docs/sandbox'; +const SANDBOX_DIR = 'sandbox'; +const ESCAPED_SLASH = '%2F'; + +const rl = readline.createInterface({ input: fs.createReadStream(TEMPLATE_PATH) }); + +let readme = ''; + +rl + .on('line', line => { + if (IMPORT_PATTERN.test(line)) { + const file = line.replace(IMPORT_START, '').replace(IMPORT_END, ''); + const slugs = file.split('/'); + const matcher = slugs[slugs.length - 1].replace('.test.js', ''); + const module = file.replace(SANDBOX_DIR, '').replace(/\//g, ESCAPED_SLASH); + const contents = fs.readFileSync(`${DOCS}/${file}`, 'utf-8'); + const [comments, code] = contents.split(COMMENT_END); + + readme += comments.split(COMMENT_START)[1].trim() + '\n'; + readme += '\n```js\n' + `${code.trim()}\n` + '```\n'; + readme += `\n[Open ${matcher} in REPL](${SANDBOX_URL}?module=${module})\n`; + return; + } + + readme += `${line}\n`; + }) + .on('close', () => { + fs.writeFileSync(README_PATH, readme); + });