diff --git a/.eslintrc b/.eslintrc index 6d351930..2a042dae 100644 --- a/.eslintrc +++ b/.eslintrc @@ -15,6 +15,13 @@ "expect": true }, "plugins": ["eslint-plugin-import", "import", "jest"], + "settings": { + "import/resolver": { + "node": { + "moduleDirectory": ["./", "node_modules"] + } + } + }, "extends": ["eslint:recommended", "plugin:prettier/recommended"], "rules": { "comma-spacing": [1, { "before": false, "after": true }], diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index a4db86a3..addce6ac 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -12,10 +12,6 @@ Thanks for being willing to contribute! 3. `$ yarn test` to validate you've got it working 4. Create a branch for your PR -## Here as part of Hacktoberfest? - -Head over to [here](https://hacktoberfest.digitalocean.com/sign_up/register) to signup if you haven't already - ## Making changes - All changes should have unit tests @@ -24,12 +20,10 @@ Head over to [here](https://hacktoberfest.digitalocean.com/sign_up/register) to ### New Matchers -- Each matcher should be placed in it's own directory inside of the `matchers` directory. -- A matcher directory should contain the following: - - `index.js` - An export of the matcher in the format expected by Jest. See the docs for an [example](http://facebook.github.io/jest/docs/en/expect.html#expectextendmatchers). Note: the test outcome messages must be a function that returns a string (this caught me out 😉). - - `index.test.js` - Test suite that uses the new matcher and make sure it passes. - - `predicate.js` - The function that tests the actual value meets the expected value / behavior. - - `predicate.test.js` - Tests for the predicate both true/false cases must be covered. +- Each matcher should be placed in it's own file inside of the `src/matchers/[matcher].js`. +- A matcher should: + - Export the matcher in the format expected by Jest. See the docs for an [example](http://facebook.github.io/jest/docs/en/expect.html#expectextendmatchers). Note: the test outcome messages must be a function that returns a string (this caught me out 😉). +- Tests for the matcher should go in `test/matchers/[matcher].test.js` - Test suite that uses the new matcher and make sure it passes. - Docs for the new matcher should be updated in the API section of the `README.md` to no longer say `Unimplemented`. - Type definitions for the new matchers should be added to `types/index.d.ts`. diff --git a/package.json b/package.json index 12131707..9bd0a59d 100644 --- a/package.json +++ b/package.json @@ -78,6 +78,9 @@ "snapshotSerializers": [ "pretty-format/build/plugins/ConvertAnsi.js" ], + "moduleNameMapper": { + "src/(.*)": "/src/$1" + }, "coverageThreshold": { "global": { "branches": 100, diff --git a/src/matchers/fail.js b/src/matchers/fail.js new file mode 100644 index 00000000..410f4146 --- /dev/null +++ b/src/matchers/fail.js @@ -0,0 +1,6 @@ +export function fail(_, message) { + return { + pass: false, + message: () => (message ? message : 'fails by .fail() assertion'), + }; +} diff --git a/src/matchers/fail/index.js b/src/matchers/fail/index.js deleted file mode 100644 index 64dc56ba..00000000 --- a/src/matchers/fail/index.js +++ /dev/null @@ -1,10 +0,0 @@ -import predicate from './predicate'; - -const failMessage = message => { - if (message) return () => message; - else return () => 'fails by .fail() assertion'; -}; - -export function fail(expected, message) { - return { pass: predicate(), message: failMessage(message) }; -} diff --git a/src/matchers/fail/predicate.js b/src/matchers/fail/predicate.js deleted file mode 100644 index eae5767c..00000000 --- a/src/matchers/fail/predicate.js +++ /dev/null @@ -1 +0,0 @@ -export default () => false; diff --git a/src/matchers/fail/predicate.test.js b/src/matchers/fail/predicate.test.js deleted file mode 100644 index c1b6bba6..00000000 --- a/src/matchers/fail/predicate.test.js +++ /dev/null @@ -1,7 +0,0 @@ -import predicate from './predicate'; - -describe('fail Predicate', () => { - test('returns false', () => { - expect(predicate()).toBe(false); - }); -}); diff --git a/src/matchers/pass.js b/src/matchers/pass.js new file mode 100644 index 00000000..b4521d2a --- /dev/null +++ b/src/matchers/pass.js @@ -0,0 +1,6 @@ +export function pass(_, message) { + return { + pass: true, + message: () => (message ? message : 'passes by .pass() assertion'), + }; +} diff --git a/src/matchers/pass/index.js b/src/matchers/pass/index.js deleted file mode 100644 index 1d48865e..00000000 --- a/src/matchers/pass/index.js +++ /dev/null @@ -1,10 +0,0 @@ -import predicate from './predicate'; - -const passMessage = message => { - if (message) return () => message; - else return () => 'passes by .pass() assertion'; -}; - -export function pass(expected, message) { - return { pass: predicate(), message: passMessage(message) }; -} diff --git a/src/matchers/pass/predicate.js b/src/matchers/pass/predicate.js deleted file mode 100644 index 3c72b2d3..00000000 --- a/src/matchers/pass/predicate.js +++ /dev/null @@ -1 +0,0 @@ -export default () => true; diff --git a/src/matchers/pass/predicate.test.js b/src/matchers/pass/predicate.test.js deleted file mode 100644 index 388b3cae..00000000 --- a/src/matchers/pass/predicate.test.js +++ /dev/null @@ -1,7 +0,0 @@ -import predicate from './predicate'; - -describe('true Predicate', () => { - test('returns true', () => { - expect(predicate()).toBe(true); - }); -}); diff --git a/src/matchers/toBeAfter.js b/src/matchers/toBeAfter.js new file mode 100644 index 00000000..27de3bdc --- /dev/null +++ b/src/matchers/toBeAfter.js @@ -0,0 +1,18 @@ +export function toBeAfter(date, after) { + const { printReceived, matcherHint } = this.utils; + const passMessage = + matcherHint('.not.toBeAfter', 'received', '') + + '\n\n' + + `Expected date to be after ${printReceived(after)} but received:\n` + + ` ${printReceived(date)}`; + + const failMessage = + matcherHint('.toBeAfter', 'received', '') + + '\n\n' + + `Expected date to be after ${printReceived(after)} but received:\n` + + ` ${printReceived(date)}`; + + const pass = date > after; + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toBeAfter/index.js b/src/matchers/toBeAfter/index.js deleted file mode 100644 index 3973df53..00000000 --- a/src/matchers/toBeAfter/index.js +++ /dev/null @@ -1,22 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, received, after) => () => - utils.matcherHint('.not.toBeAfter', 'received', '') + - '\n\n' + - `Expected date to be after ${utils.printReceived(after)} but received:\n` + - ` ${utils.printReceived(received)}`; - -const failMessage = (utils, received, after) => () => - utils.matcherHint('.toBeAfter', 'received', '') + - '\n\n' + - `Expected date to be after ${utils.printReceived(after)} but received:\n` + - ` ${utils.printReceived(received)}`; - -export function toBeAfter(date, after) { - const pass = predicate(date, after); - if (pass) { - return { pass: true, message: passMessage(this.utils, date, after) }; - } - - return { pass: false, message: failMessage(this.utils, date, after) }; -} diff --git a/src/matchers/toBeAfter/predicate.js b/src/matchers/toBeAfter/predicate.js deleted file mode 100644 index 66d536f4..00000000 --- a/src/matchers/toBeAfter/predicate.js +++ /dev/null @@ -1,5 +0,0 @@ -function toBeAfter(date, after) { - return date > after; -} - -export default (date, after) => toBeAfter(date, after); diff --git a/src/matchers/toBeAfter/predicate.test.js b/src/matchers/toBeAfter/predicate.test.js deleted file mode 100644 index 6b2693f6..00000000 --- a/src/matchers/toBeAfter/predicate.test.js +++ /dev/null @@ -1,14 +0,0 @@ -import predicate from './predicate'; - -const EARLIER = new Date('2018-06-01T22:00:00.000Z'); -const LATER = new Date('2018-06-02T22:00:00.000Z'); - -describe('toBeAfter Predicate', () => { - test('returns true when given an earlier date', () => { - expect(predicate(LATER, EARLIER)).toBe(true); - }); - - test('returns false when given a later date', () => { - expect(predicate(EARLIER, LATER)).toBe(false); - }); -}); diff --git a/src/matchers/toBeAfterOrEqualTo.js b/src/matchers/toBeAfterOrEqualTo.js new file mode 100644 index 00000000..8f0e302a --- /dev/null +++ b/src/matchers/toBeAfterOrEqualTo.js @@ -0,0 +1,19 @@ +export function toBeAfterOrEqualTo(actual, expected) { + const { printReceived, matcherHint } = this.utils; + + const passMessage = + matcherHint('.not.toBeAfterOrEqualTo', 'received', '') + + '\n\n' + + `Expected date to be after or equal to ${printReceived(expected)} but received:\n` + + ` ${printReceived(actual)}`; + + const failMessage = + matcherHint('.toBeAfterOrEqualTo', 'received', '') + + '\n\n' + + `Expected date to be after or equal to ${printReceived(expected)} but received:\n` + + ` ${printReceived(actual)}`; + + const pass = actual >= expected; + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toBeAfterOrEqualTo/index.js b/src/matchers/toBeAfterOrEqualTo/index.js deleted file mode 100644 index 6e7351c4..00000000 --- a/src/matchers/toBeAfterOrEqualTo/index.js +++ /dev/null @@ -1,22 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, received, before) => () => - utils.matcherHint('.not.toBeAfterOrEqualTo', 'received', '') + - '\n\n' + - `Expected date to be after or equal to ${utils.printReceived(before)} but received:\n` + - ` ${utils.printReceived(received)}`; - -const failMessage = (utils, received, before) => () => - utils.matcherHint('.toBeAfterOrEqualTo', 'received', '') + - '\n\n' + - `Expected date to be after or equal to ${utils.printReceived(before)} but received:\n` + - ` ${utils.printReceived(received)}`; - -export function toBeAfterOrEqualTo(date, after) { - const pass = predicate(date, after); - if (pass) { - return { pass: true, message: passMessage(this.utils, date, after) }; - } - - return { pass: false, message: failMessage(this.utils, date, after) }; -} diff --git a/src/matchers/toBeAfterOrEqualTo/predicate.js b/src/matchers/toBeAfterOrEqualTo/predicate.js deleted file mode 100644 index c1cf2729..00000000 --- a/src/matchers/toBeAfterOrEqualTo/predicate.js +++ /dev/null @@ -1,5 +0,0 @@ -function toBeAfterOrEqualTo(date, after) { - return date >= after; -} - -export default (date, after) => toBeAfterOrEqualTo(date, after); diff --git a/src/matchers/toBeAfterOrEqualTo/predicate.test.js b/src/matchers/toBeAfterOrEqualTo/predicate.test.js deleted file mode 100644 index 340d9b27..00000000 --- a/src/matchers/toBeAfterOrEqualTo/predicate.test.js +++ /dev/null @@ -1,18 +0,0 @@ -import predicate from './predicate'; - -const EARLIER = new Date('01/09/2019'); -const LATER = new Date('07/09/2019'); - -describe('toBeAfterOrEqualTo Predicate', () => { - test('returns true when given an earlier date', () => { - expect(predicate(LATER, EARLIER)).toBe(true); - }); - - test('returns true when given an equal date', () => { - expect(predicate(EARLIER, EARLIER)).toBe(true); - }); - - test('returns false when given a later date', () => { - expect(predicate(EARLIER, LATER)).toBe(false); - }); -}); diff --git a/src/matchers/toBeArray.js b/src/matchers/toBeArray.js new file mode 100644 index 00000000..f03a8edf --- /dev/null +++ b/src/matchers/toBeArray.js @@ -0,0 +1,19 @@ +export function toBeArray(expected) { + const { matcherHint, printReceived } = this.utils; + + const passMessage = + matcherHint('.not.toBeArray', 'received', '') + + '\n\n' + + 'Expected value to not be an array received:\n' + + ` ${printReceived(expected)}`; + + const failMessage = + matcherHint('.toBeArray', 'received', '') + + '\n\n' + + 'Expected value to be an array received:\n' + + ` ${printReceived(expected)}`; + + const pass = Array.isArray(expected); + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toBeArray/index.js b/src/matchers/toBeArray/index.js deleted file mode 100644 index 6c4fe00c..00000000 --- a/src/matchers/toBeArray/index.js +++ /dev/null @@ -1,22 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, received) => () => - utils.matcherHint('.not.toBeArray', 'received', '') + - '\n\n' + - 'Expected value to not be an array received:\n' + - ` ${utils.printReceived(received)}`; - -const failMessage = (utils, received) => () => - utils.matcherHint('.toBeArray', 'received', '') + - '\n\n' + - 'Expected value to be an array received:\n' + - ` ${utils.printReceived(received)}`; - -export function toBeArray(expected) { - const pass = predicate(expected); - if (pass) { - return { pass: true, message: passMessage(this.utils, expected) }; - } - - return { pass: false, message: failMessage(this.utils, expected) }; -} diff --git a/src/matchers/toBeArray/predicate.js b/src/matchers/toBeArray/predicate.js deleted file mode 100644 index 8a3f5710..00000000 --- a/src/matchers/toBeArray/predicate.js +++ /dev/null @@ -1 +0,0 @@ -export default expected => Array.isArray(expected); diff --git a/src/matchers/toBeArray/predicate.test.js b/src/matchers/toBeArray/predicate.test.js deleted file mode 100644 index 7855dda6..00000000 --- a/src/matchers/toBeArray/predicate.test.js +++ /dev/null @@ -1,14 +0,0 @@ -import predicate from './predicate'; - -describe('toBeArray Predicate', () => { - test('returns true when given an array', () => { - expect(predicate(['an array'])).toBe(true); - }); - - test.each([[false], [''], [0], [{}], [() => {}], [undefined], [null], [NaN]])( - 'returns false when given: %s', - given => { - expect(predicate(given)).toBe(false); - }, - ); -}); diff --git a/src/matchers/toBeArrayOfSize.js b/src/matchers/toBeArrayOfSize.js new file mode 100644 index 00000000..71fd2c76 --- /dev/null +++ b/src/matchers/toBeArrayOfSize.js @@ -0,0 +1,25 @@ +import { determinePropertyMessage } from '../utils'; + +export function toBeArrayOfSize(actual, expected) { + const { printExpected, printReceived, matcherHint } = this.utils; + + const passMessage = `${matcherHint('.not.toBeArrayOfSize')} + +Expected value to not be an array of size: + ${printExpected(expected)} +Received: + value: ${printReceived(actual)} + length: ${printReceived(determinePropertyMessage(actual, 'length'))}`; + + const failMessage = `${matcherHint('.toBeArrayOfSize')} + +Expected value to be an array of size: + ${printExpected(expected)} +Received: + value: ${printReceived(actual)} + length: ${printReceived(determinePropertyMessage(actual, 'length'))}`; + + const pass = Array.isArray(actual) && actual.length === expected; + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toBeArrayOfSize/index.js b/src/matchers/toBeArrayOfSize/index.js deleted file mode 100644 index 1f991d3a..00000000 --- a/src/matchers/toBeArrayOfSize/index.js +++ /dev/null @@ -1,30 +0,0 @@ -import { determinePropertyMessage } from '../../utils'; - -import predicate from './predicate'; - -const passMessage = (utils, actual, expected) => () => - `${utils.matcherHint('.not.toBeArrayOfSize')} - -Expected value to not be an array of size: - ${utils.printExpected(expected)} -Received: - value: ${utils.printReceived(actual)} - length: ${utils.printReceived(determinePropertyMessage(actual, 'length'))}`; - -const failMessage = (utils, actual, expected) => () => - `${utils.matcherHint('.toBeArrayOfSize')} - -Expected value to be an array of size: - ${utils.printExpected(expected)} -Received: - value: ${utils.printReceived(actual)} - length: ${utils.printReceived(determinePropertyMessage(actual, 'length'))}`; - -export function toBeArrayOfSize(actual, expected) { - const pass = predicate(actual, expected); - if (pass) { - return { pass: true, message: passMessage(this.utils, actual, expected) }; - } - - return { pass: false, message: failMessage(this.utils, actual, expected) }; -} diff --git a/src/matchers/toBeArrayOfSize/predicate.js b/src/matchers/toBeArrayOfSize/predicate.js deleted file mode 100644 index 93d60b0b..00000000 --- a/src/matchers/toBeArrayOfSize/predicate.js +++ /dev/null @@ -1,8 +0,0 @@ -import toBeArray from '../toBeArray/predicate'; - -/** - * @param actual - * @param size - * @returns {boolean} - */ -export default (actual, size) => toBeArray(actual) && actual.length === size; diff --git a/src/matchers/toBeArrayOfSize/predicate.test.js b/src/matchers/toBeArrayOfSize/predicate.test.js deleted file mode 100644 index e392bae3..00000000 --- a/src/matchers/toBeArrayOfSize/predicate.test.js +++ /dev/null @@ -1,21 +0,0 @@ -import predicate from './predicate'; - -describe('toBeArrayOfSize Predicate', () => { - const size = 5; - test(`returns true when given an array of size ${size}`, () => { - expect(predicate([1, 2, 3, 4, 5], size)).toBe(true); - }); - - test.each([ - [[false], 0], - [[''], -1], - [[0], 6], - [[{}], 6], - [[() => {}], 6], - [[undefined], 6], - [[null], 6], - [[NaN], 6], - ])('returns false when given: %s', (array, size) => { - expect(predicate(array, size)).toBe(false); - }); -}); diff --git a/src/matchers/toBeBefore.js b/src/matchers/toBeBefore.js new file mode 100644 index 00000000..3d59b9a6 --- /dev/null +++ b/src/matchers/toBeBefore.js @@ -0,0 +1,18 @@ +export function toBeBefore(actual, expected) { + const { matcherHint, printReceived } = this.utils; + const passMessage = + matcherHint('.not.toBeBefore', 'received', '') + + '\n\n' + + `Expected date to be before ${printReceived(expected)} but received:\n` + + ` ${printReceived(actual)}`; + + const failMessage = + matcherHint('.toBeBefore', 'received', '') + + '\n\n' + + `Expected date to be before ${printReceived(expected)} but received:\n` + + ` ${printReceived(actual)}`; + + const pass = actual < expected; + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toBeBefore/index.js b/src/matchers/toBeBefore/index.js deleted file mode 100644 index abfcfbed..00000000 --- a/src/matchers/toBeBefore/index.js +++ /dev/null @@ -1,22 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, received, before) => () => - utils.matcherHint('.not.toBeBefore', 'received', '') + - '\n\n' + - `Expected date to be before ${utils.printReceived(before)} but received:\n` + - ` ${utils.printReceived(received)}`; - -const failMessage = (utils, received, before) => () => - utils.matcherHint('.toBeBefore', 'received', '') + - '\n\n' + - `Expected date to be before ${utils.printReceived(before)} but received:\n` + - ` ${utils.printReceived(received)}`; - -export function toBeBefore(date, before) { - const pass = predicate(date, before); - if (pass) { - return { pass: true, message: passMessage(this.utils, date, before) }; - } - - return { pass: false, message: failMessage(this.utils, date, before) }; -} diff --git a/src/matchers/toBeBefore/predicate.js b/src/matchers/toBeBefore/predicate.js deleted file mode 100644 index dbe8a86c..00000000 --- a/src/matchers/toBeBefore/predicate.js +++ /dev/null @@ -1,5 +0,0 @@ -function toBeBefore(date, before) { - return date < before; -} - -export default (date, before) => toBeBefore(date, before); diff --git a/src/matchers/toBeBefore/predicate.test.js b/src/matchers/toBeBefore/predicate.test.js deleted file mode 100644 index 6b89bd7a..00000000 --- a/src/matchers/toBeBefore/predicate.test.js +++ /dev/null @@ -1,14 +0,0 @@ -import predicate from './predicate'; - -const EARLIER = new Date('06/02/2018'); -const LATER = new Date('07/02/2018'); - -describe('toBeAfter Predicate', () => { - test('returns true when given a later date', () => { - expect(predicate(EARLIER, LATER)).toBe(true); - }); - - test('returns false when given an earlier date', () => { - expect(predicate(LATER, EARLIER)).toBe(false); - }); -}); diff --git a/src/matchers/toBeBeforeOrEqualTo.js b/src/matchers/toBeBeforeOrEqualTo.js new file mode 100644 index 00000000..0f93e13e --- /dev/null +++ b/src/matchers/toBeBeforeOrEqualTo.js @@ -0,0 +1,19 @@ +export function toBeBeforeOrEqualTo(actual, expected) { + const { matcherHint, printReceived } = this.utils; + + const passMessage = + matcherHint('.not.toBeBeforeOrEqualTo', 'received', '') + + '\n\n' + + `Expected date to be before or equal to ${printReceived(expected)} but received:\n` + + ` ${printReceived(actual)}`; + + const failMessage = + matcherHint('.toBeBeforeOrEqualTo', 'received', '') + + '\n\n' + + `Expected date to be before or equal to ${printReceived(expected)} but received:\n` + + ` ${printReceived(actual)}`; + + const pass = actual <= expected; + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toBeBeforeOrEqualTo/index.js b/src/matchers/toBeBeforeOrEqualTo/index.js deleted file mode 100644 index aa7bd945..00000000 --- a/src/matchers/toBeBeforeOrEqualTo/index.js +++ /dev/null @@ -1,22 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, received, before) => () => - utils.matcherHint('.not.toBeBeforeOrEqualTo', 'received', '') + - '\n\n' + - `Expected date to be before or equal to ${utils.printReceived(before)} but received:\n` + - ` ${utils.printReceived(received)}`; - -const failMessage = (utils, received, before) => () => - utils.matcherHint('.toBeBeforeOrEqualTo', 'received', '') + - '\n\n' + - `Expected date to be before or equal to ${utils.printReceived(before)} but received:\n` + - ` ${utils.printReceived(received)}`; - -export function toBeBeforeOrEqualTo(date, before) { - const pass = predicate(date, before); - if (pass) { - return { pass: true, message: passMessage(this.utils, date, before) }; - } - - return { pass: false, message: failMessage(this.utils, date, before) }; -} diff --git a/src/matchers/toBeBeforeOrEqualTo/predicate.js b/src/matchers/toBeBeforeOrEqualTo/predicate.js deleted file mode 100644 index a522eedb..00000000 --- a/src/matchers/toBeBeforeOrEqualTo/predicate.js +++ /dev/null @@ -1,5 +0,0 @@ -function toBeBeforeOrEqualTo(date, before) { - return date <= before; -} - -export default (date, before) => toBeBeforeOrEqualTo(date, before); diff --git a/src/matchers/toBeBeforeOrEqualTo/predicate.test.js b/src/matchers/toBeBeforeOrEqualTo/predicate.test.js deleted file mode 100644 index 3f3e8cde..00000000 --- a/src/matchers/toBeBeforeOrEqualTo/predicate.test.js +++ /dev/null @@ -1,18 +0,0 @@ -import predicate from './predicate'; - -const EARLIER = new Date('01/09/2019'); -const LATER = new Date('07/09/2019'); - -describe('toBeBeforeOrEqualTo Predicate', () => { - test('returns true when given a later date', () => { - expect(predicate(EARLIER, LATER)).toBe(true); - }); - - test('returns true when given an equal date', () => { - expect(predicate(EARLIER, EARLIER)).toBe(true); - }); - - test('returns false when given an earlier date', () => { - expect(predicate(LATER, EARLIER)).toBe(false); - }); -}); diff --git a/src/matchers/toBeBetween.js b/src/matchers/toBeBetween.js new file mode 100644 index 00000000..a99b9194 --- /dev/null +++ b/src/matchers/toBeBetween.js @@ -0,0 +1,19 @@ +export function toBeBetween(actual, startDate, endDate) { + const { matcherHint, printReceived } = this.utils; + + const passMessage = + matcherHint('.not.toBeBetween', 'received', '') + + '\n\n' + + `Expected date to be between ${printReceived(startDate)} and ${printReceived(endDate)} but received:\n` + + ` ${printReceived(actual)}`; + + const failMessage = + matcherHint('.toBeBetween', 'received', '') + + '\n\n' + + `Expected date to be between ${printReceived(startDate)} and ${printReceived(endDate)} but received:\n` + + ` ${printReceived(actual)}`; + + const pass = actual >= startDate && actual <= endDate; + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toBeBetween/index.js b/src/matchers/toBeBetween/index.js deleted file mode 100644 index c71deb5c..00000000 --- a/src/matchers/toBeBetween/index.js +++ /dev/null @@ -1,22 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, received, startDate, endDate) => () => - utils.matcherHint('.not.toBeBetween', 'received', '') + - '\n\n' + - `Expected date to be between ${utils.printReceived(startDate)} and ${utils.printReceived(endDate)} but received:\n` + - ` ${utils.printReceived(received)}`; - -const failMessage = (utils, received, startDate, endDate) => () => - utils.matcherHint('.toBeBetween', 'received', '') + - '\n\n' + - `Expected date to be between ${utils.printReceived(startDate)} and ${utils.printReceived(endDate)} but received:\n` + - ` ${utils.printReceived(received)}`; - -export function toBeBetween(date, startDate, endDate) { - const pass = predicate(date, startDate, endDate); - if (pass) { - return { pass: true, message: passMessage(this.utils, date, startDate, endDate) }; - } - - return { pass: false, message: failMessage(this.utils, date, startDate, endDate) }; -} diff --git a/src/matchers/toBeBetween/predicate.js b/src/matchers/toBeBetween/predicate.js deleted file mode 100644 index b28b2b32..00000000 --- a/src/matchers/toBeBetween/predicate.js +++ /dev/null @@ -1,5 +0,0 @@ -function toBeBetween(date, startDate, endDate) { - return date >= startDate && date <= endDate; -} - -export default (date, startDate, endDate) => toBeBetween(date, startDate, endDate); diff --git a/src/matchers/toBeBetween/predicate.test.js b/src/matchers/toBeBetween/predicate.test.js deleted file mode 100644 index bc143f83..00000000 --- a/src/matchers/toBeBetween/predicate.test.js +++ /dev/null @@ -1,15 +0,0 @@ -import predicate from './predicate'; - -const TESTDATE1 = new Date('01/09/2019'); -const TESTDATE2 = new Date('10/09/2019'); -const TESTDATE3 = new Date('03/09/2019'); - -describe('toBeBetween Predicate', () => { - test('returns true when date is in given range', () => { - expect(predicate(TESTDATE3, TESTDATE1, TESTDATE2)).toBe(true); - }); - - test('returns false when date is not in given range', () => { - expect(predicate(TESTDATE1, TESTDATE3, TESTDATE2)).toBe(false); - }); -}); diff --git a/src/matchers/toBeBoolean.js b/src/matchers/toBeBoolean.js new file mode 100644 index 00000000..3c9871d9 --- /dev/null +++ b/src/matchers/toBeBoolean.js @@ -0,0 +1,19 @@ +export function toBeBoolean(actual) { + const { matcherHint, printReceived } = this.utils; + + const passMessage = + matcherHint('.not.toBeBoolean', 'received', '') + + '\n\n' + + 'Expected value to not be of type boolean, received:\n' + + ` ${printReceived(actual)}`; + + const failMessage = + matcherHint('.toBeBoolean', 'received', '') + + '\n\n' + + 'Expected value to be of type boolean, received:\n' + + ` ${printReceived(actual)}`; + + const pass = typeof actual === 'boolean' || actual instanceof Boolean; + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toBeBoolean/index.js b/src/matchers/toBeBoolean/index.js deleted file mode 100644 index 67b4862a..00000000 --- a/src/matchers/toBeBoolean/index.js +++ /dev/null @@ -1,22 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, received) => () => - utils.matcherHint('.not.toBeBoolean', 'received', '') + - '\n\n' + - 'Expected value to not be of type boolean, received:\n' + - ` ${utils.printReceived(received)}`; - -const failMessage = (utils, received) => () => - utils.matcherHint('.toBeBoolean', 'received', '') + - '\n\n' + - 'Expected value to be of type boolean, received:\n' + - ` ${utils.printReceived(received)}`; - -export function toBeBoolean(received) { - const pass = predicate(received); - if (pass) { - return { pass: true, message: passMessage(this.utils, received) }; - } - - return { pass: false, message: failMessage(this.utils, received) }; -} diff --git a/src/matchers/toBeBoolean/predicate.js b/src/matchers/toBeBoolean/predicate.js deleted file mode 100644 index 22e4f515..00000000 --- a/src/matchers/toBeBoolean/predicate.js +++ /dev/null @@ -1,3 +0,0 @@ -export default received => { - return typeof received === 'boolean' || received instanceof Boolean; -}; diff --git a/src/matchers/toBeBoolean/predicate.test.js b/src/matchers/toBeBoolean/predicate.test.js deleted file mode 100644 index 20dce73e..00000000 --- a/src/matchers/toBeBoolean/predicate.test.js +++ /dev/null @@ -1,18 +0,0 @@ -import predicate from './predicate'; - -describe('toBeBoolean', () => { - test('returns true when given a boolean', () => { - expect(predicate(false)).toBe(true); - }); - - test('returns true when given an object of type Boolean', () => { - expect(predicate(new Boolean(false))).toBe(true); - }); - - test.each([['false'], [0], [{}], [[]], [() => {}], [undefined], [null], [NaN]])( - 'returns false when given: %s', - given => { - expect(predicate(given)).toBe(false); - }, - ); -}); diff --git a/src/matchers/toBeDate.js b/src/matchers/toBeDate.js new file mode 100644 index 00000000..8d883174 --- /dev/null +++ b/src/matchers/toBeDate.js @@ -0,0 +1,21 @@ +import { getType } from 'jest-get-type'; + +export function toBeDate(actual) { + const { matcherHint, printReceived } = this.utils; + + const passMessage = + matcherHint('.not.toBeDate', 'received', '') + + '\n\n' + + 'Expected value to not be a date received:\n' + + ` ${printReceived(actual)}`; + + const failMessage = + matcherHint('.toBeDate', 'received', '') + + '\n\n' + + 'Expected value to be a date received:\n' + + ` ${printReceived(actual)}`; + + const pass = getType(actual) === 'date' && !isNaN(actual); + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toBeDate/index.js b/src/matchers/toBeDate/index.js deleted file mode 100644 index 82f5ac81..00000000 --- a/src/matchers/toBeDate/index.js +++ /dev/null @@ -1,22 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, received) => () => - utils.matcherHint('.not.toBeDate', 'received', '') + - '\n\n' + - 'Expected value to not be a date received:\n' + - ` ${utils.printReceived(received)}`; - -const failMessage = (utils, received) => () => - utils.matcherHint('.toBeDate', 'received', '') + - '\n\n' + - 'Expected value to be a date received:\n' + - ` ${utils.printReceived(received)}`; - -export function toBeDate(expected) { - const pass = predicate(expected); - if (pass) { - return { pass: true, message: passMessage(this.utils, expected) }; - } - - return { pass: false, message: failMessage(this.utils, expected) }; -} diff --git a/src/matchers/toBeDate/predicate.js b/src/matchers/toBeDate/predicate.js deleted file mode 100644 index 36743cbb..00000000 --- a/src/matchers/toBeDate/predicate.js +++ /dev/null @@ -1,5 +0,0 @@ -import { getType } from 'jest-get-type'; - -const isDate = value => getType(value) === 'date' && !isNaN(value); - -export default isDate; diff --git a/src/matchers/toBeDate/predicate.test.js b/src/matchers/toBeDate/predicate.test.js deleted file mode 100644 index 8bbfb3e8..00000000 --- a/src/matchers/toBeDate/predicate.test.js +++ /dev/null @@ -1,14 +0,0 @@ -import predicate from './predicate'; - -describe('toBeDate Predicate', () => { - test('returns true when given a date', () => { - expect(predicate(new Date('12/25/2017'))).toBe(true); - }); - - test.each([[true], [false], [''], [0], [{}], [() => {}], [undefined], [null], [NaN]])( - 'returns false when given: %s', - given => { - expect(predicate(given)).toBe(false); - }, - ); -}); diff --git a/src/matchers/toBeDateString.js b/src/matchers/toBeDateString.js new file mode 100644 index 00000000..f6ee1a63 --- /dev/null +++ b/src/matchers/toBeDateString.js @@ -0,0 +1,19 @@ +export function toBeDateString(actual) { + const { matcherHint, printReceived } = this.utils; + + const passMessage = + matcherHint('.not.toBeDateString', 'received', '') + + '\n\n' + + 'Expected value to not be a date string received:\n' + + ` ${printReceived(actual)}`; + + const failMessage = + matcherHint('.toBeDateString', 'received', '') + + '\n\n' + + 'Expected value to be a date string received:\n' + + ` ${printReceived(actual)}`; + + const pass = !isNaN(Date.parse(actual)); + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toBeDateString/index.js b/src/matchers/toBeDateString/index.js deleted file mode 100644 index a86f9096..00000000 --- a/src/matchers/toBeDateString/index.js +++ /dev/null @@ -1,22 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, received) => () => - utils.matcherHint('.not.toBeDateString', 'received', '') + - '\n\n' + - 'Expected value to not be a date string received:\n' + - ` ${utils.printReceived(received)}`; - -const failMessage = (utils, received) => () => - utils.matcherHint('.toBeDateString', 'received', '') + - '\n\n' + - 'Expected value to be a date string received:\n' + - ` ${utils.printReceived(received)}`; - -export function toBeDateString(expected) { - const pass = predicate(expected); - if (pass) { - return { pass: true, message: passMessage(this.utils, expected) }; - } - - return { pass: false, message: failMessage(this.utils, expected) }; -} diff --git a/src/matchers/toBeDateString/predicate.js b/src/matchers/toBeDateString/predicate.js deleted file mode 100644 index 5874ba28..00000000 --- a/src/matchers/toBeDateString/predicate.js +++ /dev/null @@ -1,3 +0,0 @@ -const isDateString = value => !isNaN(Date.parse(value)); - -export default isDateString; diff --git a/src/matchers/toBeDateString/predicate.test.js b/src/matchers/toBeDateString/predicate.test.js deleted file mode 100644 index ca7aac0d..00000000 --- a/src/matchers/toBeDateString/predicate.test.js +++ /dev/null @@ -1,11 +0,0 @@ -import predicate from './predicate'; - -describe('toBeDateString Predicate', () => { - test('returns true when given a date string', () => { - expect(predicate(new Date().toISOString())).toBe(true); - }); - - test('returns false when given a non date string', () => { - expect(predicate('not a date')).toBe(false); - }); -}); diff --git a/src/matchers/toBeEmpty.js b/src/matchers/toBeEmpty.js new file mode 100644 index 00000000..076531d2 --- /dev/null +++ b/src/matchers/toBeEmpty.js @@ -0,0 +1,27 @@ +export function toBeEmpty(actual) { + const { printReceived, matcherHint } = this.utils; + + const passMessage = + matcherHint('.not.toBeEmpty', 'received', '') + + '\n\n' + + 'Expected value to not be empty received:\n' + + ` ${printReceived(actual)}`; + + const failMessage = + matcherHint('.toBeEmpty', 'received', '') + + '\n\n' + + 'Expected value to be empty received:\n' + + ` ${printReceived(actual)}`; + + const pass = this.equals({}, actual) || isEmptyIterable(actual); + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} + +const isEmptyIterable = value => { + if (typeof value[Symbol.iterator] !== 'function') { + return false; + } + const firstIteration = value[Symbol.iterator]().next(); + return firstIteration.done; +}; diff --git a/src/matchers/toBeEmpty/index.js b/src/matchers/toBeEmpty/index.js deleted file mode 100644 index 716ccd91..00000000 --- a/src/matchers/toBeEmpty/index.js +++ /dev/null @@ -1,22 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, received) => () => - utils.matcherHint('.not.toBeEmpty', 'received', '') + - '\n\n' + - 'Expected value to not be empty received:\n' + - ` ${utils.printReceived(received)}`; - -const failMessage = (utils, received) => () => - utils.matcherHint('.toBeEmpty', 'received', '') + - '\n\n' + - 'Expected value to be empty received:\n' + - ` ${utils.printReceived(received)}`; - -export function toBeEmpty(expected) { - const pass = predicate(this.equals, expected); - if (pass) { - return { pass: true, message: passMessage(this.utils, expected) }; - } - - return { pass: false, message: failMessage(this.utils, expected) }; -} diff --git a/src/matchers/toBeEmpty/predicate.js b/src/matchers/toBeEmpty/predicate.js deleted file mode 100644 index 6b233f64..00000000 --- a/src/matchers/toBeEmpty/predicate.js +++ /dev/null @@ -1,9 +0,0 @@ -const isEmptyIterable = value => { - if (typeof value[Symbol.iterator] !== 'function') { - return false; - } - const firstIteration = value[Symbol.iterator]().next(); - return firstIteration.done; -}; - -export default (equals, value) => equals({}, value) || isEmptyIterable(value); diff --git a/src/matchers/toBeEmpty/predicate.test.js b/src/matchers/toBeEmpty/predicate.test.js deleted file mode 100644 index 0a952c61..00000000 --- a/src/matchers/toBeEmpty/predicate.test.js +++ /dev/null @@ -1,74 +0,0 @@ -import { equals } from 'expect/build/jasmineUtils'; -import predicate from './predicate'; - -describe('toBeEmpty Predicate', () => { - describe('returns true', () => { - test('When empty string is passed', () => { - expect(predicate(equals, '')).toBe(true); - }); - - test('When empty string object is passed', () => { - expect(predicate(equals, new String(''))).toBe(true); - }); - - test('When empty array is passed', () => { - expect(predicate(equals, [])).toBe(true); - }); - - test('When empty object is passed', () => { - expect(predicate(equals, {})).toBe(true); - }); - - test('When empty Set is passed', () => { - expect(predicate(equals, new Set())).toBe(true); - }); - - test('When empty Map is passed', () => { - expect(predicate(equals, new Map([]))).toBe(true); - }); - - test('When empty generator is passed', () => { - function* yieldsNothing() {} - - expect(predicate(equals, yieldsNothing())).toBe(true); - }); - }); - - describe('return false', () => { - test('When array with members is passed', () => { - expect(predicate(equals, ['1'])).toBe(false); - }); - - test('When non-empty string is passed', () => { - expect(predicate(equals, 'string')).toBe(false); - }); - - test('When blank string is passed', () => { - expect(predicate(equals, ' ')).toBe(false); - }); - - test('When non-empty string object is passed', () => { - expect(predicate(equals, new String('string'))).toBe(false); - }); - - test('When object with members is passed', () => { - expect(predicate(equals, { foo: 'bar' })).toBe(false); - }); - - test('When non-empty Set is passed', () => { - expect(predicate(equals, new Set(['']))).toBe(false); - }); - - test('When non-empty Map is passed', () => { - expect(predicate(equals, new Map([['k', 'v']]))).toBe(false); - }); - - test('When non-empty generator is passed', () => { - function* yieldsSomething() { - yield 'a thing'; - } - - expect(predicate(equals, yieldsSomething())).toBe(false); - }); - }); -}); diff --git a/src/matchers/toBeEmptyObject.js b/src/matchers/toBeEmptyObject.js new file mode 100644 index 00000000..8f3df001 --- /dev/null +++ b/src/matchers/toBeEmptyObject.js @@ -0,0 +1,21 @@ +import { getType } from 'jest-get-type'; + +export function toBeEmptyObject(actual) { + const { printReceived, matcherHint } = this.utils; + + const passMessage = + matcherHint('.not.toBeEmptyObject', 'received', '') + + '\n\n' + + 'Expected value to not be an empty object, received:\n' + + ` ${printReceived(actual)}`; + + const failMessage = + matcherHint('.toBeEmptyObject', 'received', '') + + '\n\n' + + 'Expected value to be an empty object, received:\n' + + ` ${printReceived(actual)}`; + + const pass = getType(actual) === 'object' && Object.keys(actual).length === 0; + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toBeEmptyObject/index.js b/src/matchers/toBeEmptyObject/index.js deleted file mode 100644 index ce1d9963..00000000 --- a/src/matchers/toBeEmptyObject/index.js +++ /dev/null @@ -1,22 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, received) => () => - utils.matcherHint('.not.toBeEmptyObject', 'received', '') + - '\n\n' + - 'Expected value to not be an empty object, received:\n' + - ` ${utils.printReceived(received)}`; - -const failMessage = (utils, received) => () => - utils.matcherHint('.toBeEmptyObject', 'received', '') + - '\n\n' + - 'Expected value to be an empty object, received:\n' + - ` ${utils.printReceived(received)}`; - -export function toBeEmptyObject(expected) { - const pass = predicate(expected); - if (pass) { - return { pass: true, message: passMessage(this.utils, expected) }; - } - - return { pass: false, message: failMessage(this.utils, expected) }; -} diff --git a/src/matchers/toBeEmptyObject/predicate.js b/src/matchers/toBeEmptyObject/predicate.js deleted file mode 100644 index cb1e0b3a..00000000 --- a/src/matchers/toBeEmptyObject/predicate.js +++ /dev/null @@ -1,3 +0,0 @@ -import { getType } from 'jest-get-type'; - -export default expected => getType(expected) === 'object' && Object.keys(expected).length === 0; diff --git a/src/matchers/toBeEmptyObject/predicate.test.js b/src/matchers/toBeEmptyObject/predicate.test.js deleted file mode 100644 index 20c68c03..00000000 --- a/src/matchers/toBeEmptyObject/predicate.test.js +++ /dev/null @@ -1,11 +0,0 @@ -import predicate from './predicate'; - -describe('toBeEmptyObject Predicate', () => { - test('returns true when given an empty object', () => { - expect(predicate({})).toBe(true); - }); - - test('returns false when given a non-empty object', () => { - expect(predicate({ property1: 'something' })).toBe(false); - }); -}); diff --git a/src/matchers/toBeEven.js b/src/matchers/toBeEven.js new file mode 100644 index 00000000..cb86d513 --- /dev/null +++ b/src/matchers/toBeEven.js @@ -0,0 +1,22 @@ +export function toBeEven(actual) { + const { printReceived, matcherHint } = this.utils; + + const passMessage = + matcherHint('.not.toBeEven', 'received', '') + + '\n\n' + + 'Expected value to not be an even number received:\n' + + ` ${printReceived(actual)}`; + + const failMessage = + matcherHint('.toBeEven', 'received', '') + + '\n\n' + + 'Expected value to be an even number received:\n' + + ` ${printReceived(actual)}`; + + const pass = isNumber(actual) && isEven(actual); + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} + +const isNumber = expected => !isNaN(parseInt(expected)); +const isEven = expected => expected % 2 === 0; diff --git a/src/matchers/toBeEven/index.js b/src/matchers/toBeEven/index.js deleted file mode 100644 index 99539fbf..00000000 --- a/src/matchers/toBeEven/index.js +++ /dev/null @@ -1,22 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, received) => () => - utils.matcherHint('.not.toBeEven', 'received', '') + - '\n\n' + - 'Expected value to not be an even number received:\n' + - ` ${utils.printReceived(received)}`; - -const failMessage = (utils, received) => () => - utils.matcherHint('.toBeEven', 'received', '') + - '\n\n' + - 'Expected value to be an even number received:\n' + - ` ${utils.printReceived(received)}`; - -export function toBeEven(expected) { - const pass = predicate(expected); - if (pass) { - return { pass: true, message: passMessage(this.utils, expected) }; - } - - return { pass: false, message: failMessage(this.utils, expected) }; -} diff --git a/src/matchers/toBeEven/predicate.js b/src/matchers/toBeEven/predicate.js deleted file mode 100644 index 40df7b41..00000000 --- a/src/matchers/toBeEven/predicate.js +++ /dev/null @@ -1,3 +0,0 @@ -const isNumber = expected => !isNaN(parseInt(expected)); -const isEven = expected => expected % 2 === 0; -export default expected => isNumber(expected) && isEven(expected); diff --git a/src/matchers/toBeEven/predicate.test.js b/src/matchers/toBeEven/predicate.test.js deleted file mode 100644 index 3e14e2d6..00000000 --- a/src/matchers/toBeEven/predicate.test.js +++ /dev/null @@ -1,18 +0,0 @@ -import predicate from './predicate'; - -describe('toBeEven Predicate', () => { - test('returns true when given an even number', () => { - expect(predicate(2)).toBe(true); - }); - - test('returns false when given an odd number', () => { - expect(predicate(1)).toBe(false); - }); - - test.each([[false], [''], [[]], [{}], [() => {}], [undefined], [null], [NaN]])( - 'returns false when given: %s', - given => { - expect(predicate(given)).toBe(false); - }, - ); -}); diff --git a/src/matchers/toBeExtensible.js b/src/matchers/toBeExtensible.js new file mode 100644 index 00000000..eaa9e4da --- /dev/null +++ b/src/matchers/toBeExtensible.js @@ -0,0 +1,19 @@ +export function toBeExtensible(actual) { + const { matcherHint, printExpected, printReceived } = this.utils; + + const passMessage = + matcherHint('.not.toBeExtensible', 'received', '') + + '\n\n' + + 'Expected value to not be extensible received:\n' + + ` ${printExpected(actual)}\n`; + + const failMessage = + matcherHint('.toBeExtensible', 'received', '') + + '\n\n' + + 'Expected value to be extensible received:\n' + + ` ${printReceived(actual)}`; + + const pass = Object.isExtensible(actual); + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toBeExtensible/index.js b/src/matchers/toBeExtensible/index.js deleted file mode 100644 index 65a4fe57..00000000 --- a/src/matchers/toBeExtensible/index.js +++ /dev/null @@ -1,27 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, received) => () => { - return ( - utils.matcherHint('.not.toBeExtensible', 'received', '') + - '\n\n' + - 'Expected value to not be extensible received:\n' + - ` ${utils.printExpected(received)}\n` - ); -}; - -const failMessage = (utils, received) => () => { - return ( - utils.matcherHint('.toBeExtensible', 'received', '') + - '\n\n' + - 'Expected value to be extensible received:\n' + - ` ${utils.printReceived(received)}` - ); -}; - -export function toBeExtensible(expected) { - const pass = predicate(expected); - return { - pass, - message: pass ? passMessage(this.utils, expected) : failMessage(this.utils, expected), - }; -} diff --git a/src/matchers/toBeExtensible/predicate.js b/src/matchers/toBeExtensible/predicate.js deleted file mode 100644 index c5400bd4..00000000 --- a/src/matchers/toBeExtensible/predicate.js +++ /dev/null @@ -1 +0,0 @@ -export default expected => Object.isExtensible(expected); diff --git a/src/matchers/toBeExtensible/predicate.test.js b/src/matchers/toBeExtensible/predicate.test.js deleted file mode 100644 index 2959a065..00000000 --- a/src/matchers/toBeExtensible/predicate.test.js +++ /dev/null @@ -1,14 +0,0 @@ -import predicate from './predicate'; - -describe('toBeExpected Predicate', () => { - test.each([[{}], [[]], [() => {}]])('returns true when given extensible object: %s', given => { - expect(predicate(given)).toBe(true); - }); - - test.each([[false], [''], [0], [undefined], [null], [NaN], [Object.seal({})], [Object.freeze({})]])( - 'returns false when given non-extensible object: %s', - given => { - expect(predicate(given)).toBe(false); - }, - ); -}); diff --git a/src/matchers/toBeFalse.js b/src/matchers/toBeFalse.js new file mode 100644 index 00000000..c5500b93 --- /dev/null +++ b/src/matchers/toBeFalse.js @@ -0,0 +1,21 @@ +export function toBeFalse(actual) { + const { printReceived, matcherHint, printExpected } = this.utils; + + const passMessage = + matcherHint('.not.toBeFalse', 'received', '') + + '\n\n' + + 'Expected value to not be false received:\n' + + ` ${printReceived(actual)}`; + + const failMessage = + matcherHint('.toBeFalse', 'received', '') + + '\n\n' + + 'Expected value to be false:\n' + + ` ${printExpected(false)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const pass = actual === false; + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toBeFalse/index.js b/src/matchers/toBeFalse/index.js deleted file mode 100644 index f671e474..00000000 --- a/src/matchers/toBeFalse/index.js +++ /dev/null @@ -1,24 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, received) => () => - utils.matcherHint('.not.toBeFalse', 'received', '') + - '\n\n' + - 'Expected value to not be false received:\n' + - ` ${utils.printReceived(received)}`; - -const failMessage = (utils, received) => () => - utils.matcherHint('.toBeFalse', 'received', '') + - '\n\n' + - 'Expected value to be false:\n' + - ` ${utils.printExpected(false)}\n` + - 'Received:\n' + - ` ${utils.printReceived(received)}`; - -export function toBeFalse(expected) { - const pass = predicate(expected); - if (pass) { - return { pass: true, message: passMessage(this.utils, expected) }; - } - - return { pass: false, message: failMessage(this.utils, expected) }; -} diff --git a/src/matchers/toBeFalse/predicate.js b/src/matchers/toBeFalse/predicate.js deleted file mode 100644 index ebfb2411..00000000 --- a/src/matchers/toBeFalse/predicate.js +++ /dev/null @@ -1 +0,0 @@ -export default expected => expected === false; diff --git a/src/matchers/toBeFalse/predicate.test.js b/src/matchers/toBeFalse/predicate.test.js deleted file mode 100644 index e1e17fdd..00000000 --- a/src/matchers/toBeFalse/predicate.test.js +++ /dev/null @@ -1,14 +0,0 @@ -import predicate from './predicate'; - -describe('toBeFalse Predicate', () => { - test('returns true when given false', () => { - expect(predicate(false)).toBe(true); - }); - - test.each([[true], [''], [0], [{}], [[]], [() => {}], [undefined], [null], [NaN]])( - 'returns false when given: %s', - given => { - expect(predicate(given)).toBe(false); - }, - ); -}); diff --git a/src/matchers/toBeFinite.js b/src/matchers/toBeFinite.js new file mode 100644 index 00000000..9b8f0bb4 --- /dev/null +++ b/src/matchers/toBeFinite.js @@ -0,0 +1,19 @@ +export function toBeFinite(actual) { + const { printReceived, matcherHint } = this.utils; + + const passMessage = + matcherHint('.not.toBeFinite', 'received', '') + + '\n\n' + + 'Expected value to not be finite received:\n' + + ` ${printReceived(actual)}`; + + const failMessage = + matcherHint('.toBeFinite', 'received', '') + + '\n\n' + + 'Expected value to be finite received:\n' + + ` ${printReceived(actual)}`; + + const pass = Number.isFinite(actual); + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toBeFinite/index.js b/src/matchers/toBeFinite/index.js deleted file mode 100644 index 220c36f2..00000000 --- a/src/matchers/toBeFinite/index.js +++ /dev/null @@ -1,22 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, received) => () => - utils.matcherHint('.not.toBeFinite', 'received', '') + - '\n\n' + - 'Expected value to not be finite received:\n' + - ` ${utils.printReceived(received)}`; - -const failMessage = (utils, received) => () => - utils.matcherHint('.toBeFinite', 'received', '') + - '\n\n' + - 'Expected value to be finite received:\n' + - ` ${utils.printReceived(received)}`; - -export function toBeFinite(expected) { - const pass = predicate(expected); - if (pass) { - return { pass: true, message: passMessage(this.utils, expected) }; - } - - return { pass: false, message: failMessage(this.utils, expected) }; -} diff --git a/src/matchers/toBeFinite/predicate.js b/src/matchers/toBeFinite/predicate.js deleted file mode 100644 index 004dbf01..00000000 --- a/src/matchers/toBeFinite/predicate.js +++ /dev/null @@ -1 +0,0 @@ -export default expected => isFinite(expected); diff --git a/src/matchers/toBeFinite/predicate.test.js b/src/matchers/toBeFinite/predicate.test.js deleted file mode 100644 index 048626f7..00000000 --- a/src/matchers/toBeFinite/predicate.test.js +++ /dev/null @@ -1,15 +0,0 @@ -import predicate from './predicate'; - -describe('toBeFinite Predicate', () => { - test('returns true when given a finite number', () => { - expect(predicate(1)).toBe(true); - }); - - test('return false when given infinity', () => { - expect(predicate(Infinity)).toBe(false); - }); - - test('return false when given NaN', () => { - expect(predicate(NaN)).toBe(false); - }); -}); diff --git a/src/matchers/toBeFrozen.js b/src/matchers/toBeFrozen.js new file mode 100644 index 00000000..31893783 --- /dev/null +++ b/src/matchers/toBeFrozen.js @@ -0,0 +1,11 @@ +export function toBeFrozen(actual) { + const { matcherHint } = this.utils; + + const passMessage = matcherHint('.not.toBeFrozen', 'received', '') + '\n\n' + 'Expected object to not be frozen'; + + const failMessage = matcherHint('.toBeFrozen', 'received', '') + '\n\n' + 'Expected object to be frozen'; + + const pass = Object.isFrozen(actual); + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toBeFrozen/index.js b/src/matchers/toBeFrozen/index.js deleted file mode 100644 index 3f9d4fd4..00000000 --- a/src/matchers/toBeFrozen/index.js +++ /dev/null @@ -1,16 +0,0 @@ -import predicate from './predicate'; - -const passMessage = utils => () => - utils.matcherHint('.not.toBeFrozen', 'received', '') + '\n\n' + 'Expected object to not be frozen'; - -const failMessage = utils => () => - utils.matcherHint('.toBeFrozen', 'received', '') + '\n\n' + 'Expected object to be frozen'; - -export function toBeFrozen(expected) { - const pass = predicate(expected); - if (pass) { - return { pass: true, message: passMessage(this.utils) }; - } - - return { pass: false, message: failMessage(this.utils) }; -} diff --git a/src/matchers/toBeFrozen/predicate.js b/src/matchers/toBeFrozen/predicate.js deleted file mode 100644 index 3e29f31c..00000000 --- a/src/matchers/toBeFrozen/predicate.js +++ /dev/null @@ -1 +0,0 @@ -export default Object.isFrozen; diff --git a/src/matchers/toBeFrozen/predicate.test.js b/src/matchers/toBeFrozen/predicate.test.js deleted file mode 100644 index 3fe53f6a..00000000 --- a/src/matchers/toBeFrozen/predicate.test.js +++ /dev/null @@ -1,11 +0,0 @@ -import predicate from './predicate'; - -describe('toBeFrozen Predicate', () => { - test('returns true when given a frozen object', () => { - expect(predicate(Object.freeze({}))).toBe(true); - }); - - test('returns false when given a non-frozen object', () => { - expect(predicate({})).toBe(false); - }); -}); diff --git a/src/matchers/toBeFunction.js b/src/matchers/toBeFunction.js new file mode 100644 index 00000000..3bd1147b --- /dev/null +++ b/src/matchers/toBeFunction.js @@ -0,0 +1,19 @@ +export function toBeFunction(actual) { + const { printReceived, matcherHint } = this.utils; + + const passMessage = + matcherHint('.not.toBeFunction', 'received', '') + + '\n\n' + + 'Expected value to not be a function, received:\n' + + ` ${printReceived(actual)}`; + + const failMessage = + matcherHint('.toBeFunction', 'received', '') + + '\n\n' + + 'Expected to receive a function, received:\n' + + ` ${printReceived(actual)}`; + + const pass = typeof actual === 'function'; + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toBeFunction/index.js b/src/matchers/toBeFunction/index.js deleted file mode 100644 index 85bb7a6d..00000000 --- a/src/matchers/toBeFunction/index.js +++ /dev/null @@ -1,22 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, received) => () => - utils.matcherHint('.not.toBeFunction', 'received', '') + - '\n\n' + - 'Expected value to not be a function, received:\n' + - ` ${utils.printReceived(received)}`; - -const failMessage = (utils, received) => () => - utils.matcherHint('.toBeFunction', 'received', '') + - '\n\n' + - 'Expected to receive a function, received:\n' + - ` ${utils.printReceived(received)}`; - -export function toBeFunction(expected) { - const pass = predicate(expected); - if (pass) { - return { pass: true, message: passMessage(this.utils, expected) }; - } - - return { pass: false, message: failMessage(this.utils, expected) }; -} diff --git a/src/matchers/toBeFunction/predicate.js b/src/matchers/toBeFunction/predicate.js deleted file mode 100644 index 7278bd11..00000000 --- a/src/matchers/toBeFunction/predicate.js +++ /dev/null @@ -1 +0,0 @@ -export default expected => typeof expected === 'function'; diff --git a/src/matchers/toBeFunction/predicate.test.js b/src/matchers/toBeFunction/predicate.test.js deleted file mode 100644 index 7988ffd1..00000000 --- a/src/matchers/toBeFunction/predicate.test.js +++ /dev/null @@ -1,11 +0,0 @@ -import predicate from './predicate'; - -describe('toBeFunction Predicate', () => { - test('returns true when given a function', () => { - expect(predicate(() => {})).toBe(true); - }); - - test.each([[false], [''], [0], [{}], [[]], [undefined], [null], [NaN]])('returns false when given: %s', given => { - expect(predicate(given)).toBe(false); - }); -}); diff --git a/src/matchers/toBeHexadecimal.js b/src/matchers/toBeHexadecimal.js new file mode 100644 index 00000000..8d68858c --- /dev/null +++ b/src/matchers/toBeHexadecimal.js @@ -0,0 +1,22 @@ +export function toBeHexadecimal(actual) { + const { printReceived, matcherHint } = this.utils; + + const passMessage = + matcherHint('.not.toBeHexadecimal', 'received', '') + + '\n\n' + + 'Expected value to not be a hexadecimal, received:\n' + + ` ${printReceived(actual)}`; + + const failMessage = + matcherHint('.toBeHexadecimal', 'received', '') + + '\n\n' + + 'Expected value to be a hexadecimal, received:\n' + + ` ${printReceived(actual)}`; + + const pass = longRegex.test(actual) || shortRegex.test(actual); + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} + +const longRegex = RegExp(/^#\b[a-f0-9]{6}\b/gi); +const shortRegex = RegExp(/^#\b[a-f0-9]{3}\b/gi); diff --git a/src/matchers/toBeHexadecimal/index.js b/src/matchers/toBeHexadecimal/index.js deleted file mode 100644 index 95671ae2..00000000 --- a/src/matchers/toBeHexadecimal/index.js +++ /dev/null @@ -1,22 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, received) => () => - utils.matcherHint('.not.toBeHexadecimal', 'received', '') + - '\n\n' + - 'Expected value to not be a hexadecimal, received:\n' + - ` ${utils.printReceived(received)}`; - -const failMessage = (utils, received) => () => - utils.matcherHint('.toBeHexadecimal', 'received', '') + - '\n\n' + - 'Expected value to be a hexadecimal, received:\n' + - ` ${utils.printReceived(received)}`; - -export function toBeHexadecimal(expected) { - const pass = predicate(expected); - if (pass) { - return { pass: true, message: passMessage(this.utils, expected) }; - } - - return { pass: false, message: failMessage(this.utils, expected) }; -} diff --git a/src/matchers/toBeHexadecimal/predicate.js b/src/matchers/toBeHexadecimal/predicate.js deleted file mode 100644 index 8ef70624..00000000 --- a/src/matchers/toBeHexadecimal/predicate.js +++ /dev/null @@ -1,5 +0,0 @@ -export default expected => { - const longRegex = RegExp(/^#\b[a-f0-9]{6}\b/gi); - const shortRegex = RegExp(/^#\b[a-f0-9]{3}\b/gi); - return longRegex.test(expected) || shortRegex.test(expected); -}; diff --git a/src/matchers/toBeHexadecimal/predicate.test.js b/src/matchers/toBeHexadecimal/predicate.test.js deleted file mode 100644 index 2c1b7818..00000000 --- a/src/matchers/toBeHexadecimal/predicate.test.js +++ /dev/null @@ -1,30 +0,0 @@ -import predicate from './predicate'; - -describe('toBeHexadecimal Predicate', () => { - describe('returns true', () => { - test('When valid hex is passed', () => { - expect(predicate('#123abc')).toBe(true); - expect(predicate('#ABC123')).toBe(true); - expect(predicate('#FFF')).toBe(true); - expect(predicate('#ecECec')).toBe(true); - expect(predicate('#00f')).toBe(true); - }); - }); - - describe('return false', () => { - test('When incomplete hex is passed', () => { - expect(predicate('#ecec')).toBe(false); - expect(predicate('#00')).toBe(false); - }); - - test('When octothorp is not passed', () => { - expect(predicate('000')).toBe(false); - expect(predicate('ececec')).toBe(false); - }); - - test('When invalid character is passed', () => { - expect(predicate('#123ffg')).toBe(false); - expect(predicate('#GGG')).toBe(false); - }); - }); -}); diff --git a/src/matchers/toBeInteger.js b/src/matchers/toBeInteger.js new file mode 100644 index 00000000..363febec --- /dev/null +++ b/src/matchers/toBeInteger.js @@ -0,0 +1,22 @@ +export function toBeInteger(actual) { + const { printReceived, matcherHint } = this.utils; + + const passMessage = + matcherHint('.not.toBeInteger', 'received', '') + + '\n\n' + + 'Expected value to not be an integer received:\n' + + ` ${printReceived(actual)}`; + + const failMessage = + matcherHint('.toBeInteger', 'received', '') + + '\n\n' + + 'Expected value to be an integer received:\n' + + ` ${printReceived(actual)}`; + + const pass = isNumber(actual) && isInteger(actual); + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} + +const isNumber = value => !isNaN(parseInt(value)); +const isInteger = value => Number.isInteger(+value); diff --git a/src/matchers/toBeInteger/index.js b/src/matchers/toBeInteger/index.js deleted file mode 100644 index 72637479..00000000 --- a/src/matchers/toBeInteger/index.js +++ /dev/null @@ -1,22 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, received) => () => - utils.matcherHint('.not.toBeInteger', 'received', '') + - '\n\n' + - 'Expected value to not be an integer received:\n' + - ` ${utils.printReceived(received)}`; - -const failMessage = (utils, received) => () => - utils.matcherHint('.toBeInteger', 'received', '') + - '\n\n' + - 'Expected value to be an integer received:\n' + - ` ${utils.printReceived(received)}`; - -export function toBeInteger(expected) { - const pass = predicate(expected); - if (pass) { - return { pass: true, message: passMessage(this.utils, expected) }; - } - - return { pass: false, message: failMessage(this.utils, expected) }; -} diff --git a/src/matchers/toBeInteger/predicate.js b/src/matchers/toBeInteger/predicate.js deleted file mode 100644 index 6dc0fb90..00000000 --- a/src/matchers/toBeInteger/predicate.js +++ /dev/null @@ -1,4 +0,0 @@ -const isNumber = value => !isNaN(parseInt(value)); -const isInteger = value => Number.isInteger(+value); - -export default value => isNumber(value) && isInteger(value); diff --git a/src/matchers/toBeInteger/predicate.test.js b/src/matchers/toBeInteger/predicate.test.js deleted file mode 100644 index 21204d9c..00000000 --- a/src/matchers/toBeInteger/predicate.test.js +++ /dev/null @@ -1,27 +0,0 @@ -import predicate from './predicate'; - -describe('toBeInteger Predicate', () => { - describe('returns true', () => { - test('When integer is passed', () => { - expect(predicate(1)).toBe(true); - expect(predicate(0)).toBe(true); - expect(predicate(-1)).toBe(true); - expect(predicate('1')).toBe(true); - }); - }); - - describe('return false', () => { - test('When fraction is passed', () => { - expect(predicate(1.1)).toBe(false); - expect(predicate(0.1)).toBe(false); - expect(predicate(-1.1)).toBe(false); - expect(predicate('1.1')).toBe(false); - }); - - test('When NaN is passed', () => { - expect(predicate(NaN)).toBe(false); - expect(predicate('Not a number!')).toBe(false); - expect(predicate({})).toBe(false); - }); - }); -}); diff --git a/src/matchers/toBeNaN.js b/src/matchers/toBeNaN.js new file mode 100644 index 00000000..7f1dfeda --- /dev/null +++ b/src/matchers/toBeNaN.js @@ -0,0 +1,19 @@ +export function toBeNaN(actual) { + const { printReceived, matcherHint } = this.utils; + + const passMessage = + matcherHint('.not.toBeNaN', 'received', '') + + '\n\n' + + 'Expected value to be a number received:\n' + + ` ${printReceived(actual)}`; + + const failMessage = + matcherHint('.toBeNaN', 'received', '') + + '\n\n' + + 'Expected value to not be a number received:\n' + + ` ${printReceived(actual)}`; + + const pass = isNaN(actual); + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toBeNaN/index.js b/src/matchers/toBeNaN/index.js deleted file mode 100644 index 942e88ac..00000000 --- a/src/matchers/toBeNaN/index.js +++ /dev/null @@ -1,22 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, received) => () => - utils.matcherHint('.not.toBeNaN', 'received', '') + - '\n\n' + - 'Expected value to be a number received:\n' + - ` ${utils.printReceived(received)}`; - -const failMessage = (utils, received) => () => - utils.matcherHint('.toBeNaN', 'received', '') + - '\n\n' + - 'Expected value to not be a number received:\n' + - ` ${utils.printReceived(received)}`; - -export function toBeNaN(expected) { - const pass = predicate(expected); - if (pass) { - return { pass: true, message: passMessage(this.utils, expected) }; - } - - return { pass: false, message: failMessage(this.utils, expected) }; -} diff --git a/src/matchers/toBeNaN/predicate.js b/src/matchers/toBeNaN/predicate.js deleted file mode 100644 index 8d71fa2f..00000000 --- a/src/matchers/toBeNaN/predicate.js +++ /dev/null @@ -1 +0,0 @@ -export default expected => isNaN(expected); diff --git a/src/matchers/toBeNaN/predicate.test.js b/src/matchers/toBeNaN/predicate.test.js deleted file mode 100644 index 5a8cfb7d..00000000 --- a/src/matchers/toBeNaN/predicate.test.js +++ /dev/null @@ -1,11 +0,0 @@ -import predicate from './predicate'; - -describe('toBeNaN Predicate', () => { - test('returns true when given a non-number', () => { - expect(predicate({})).toBe(true); - }); - - test.each([[0], [1], [300], [10.5], [-50]])('returns false when given: %s', given => { - expect(predicate(given)).toBe(false); - }); -}); diff --git a/src/matchers/toBeNegative.js b/src/matchers/toBeNegative.js new file mode 100644 index 00000000..b03354b5 --- /dev/null +++ b/src/matchers/toBeNegative.js @@ -0,0 +1,22 @@ +export function toBeNegative(actual) { + const { printReceived, matcherHint } = this.utils; + + const passMessage = + matcherHint('.not.toBeNegative', 'received', '') + + '\n\n' + + 'Expected value to not be a negative number received:\n' + + ` ${printReceived(actual)}`; + + const failMessage = + matcherHint('.toBeNegative', 'received', '') + + '\n\n' + + 'Expected value to be a negative number received:\n' + + ` ${printReceived(actual)}`; + + const pass = isNumber(actual) && isNegative(actual); + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} + +const isNumber = value => !isNaN(parseInt(value)); +const isNegative = value => value < 0; diff --git a/src/matchers/toBeNegative/index.js b/src/matchers/toBeNegative/index.js deleted file mode 100644 index 511c853b..00000000 --- a/src/matchers/toBeNegative/index.js +++ /dev/null @@ -1,22 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, received) => () => - utils.matcherHint('.not.toBeNegative', 'received', '') + - '\n\n' + - 'Expected value to not be a negative number received:\n' + - ` ${utils.printReceived(received)}`; - -const failMessage = (utils, received) => () => - utils.matcherHint('.toBeNegative', 'received', '') + - '\n\n' + - 'Expected value to be a negative number received:\n' + - ` ${utils.printReceived(received)}`; - -export function toBeNegative(expected) { - const pass = predicate(expected); - if (pass) { - return { pass: true, message: passMessage(this.utils, expected) }; - } - - return { pass: false, message: failMessage(this.utils, expected) }; -} diff --git a/src/matchers/toBeNegative/predicate.js b/src/matchers/toBeNegative/predicate.js deleted file mode 100644 index 972343d4..00000000 --- a/src/matchers/toBeNegative/predicate.js +++ /dev/null @@ -1,4 +0,0 @@ -const isNumber = value => !isNaN(parseInt(value)); -const isNegative = value => value < 0; - -export default value => isNumber(value) && isNegative(value); diff --git a/src/matchers/toBeNegative/predicate.test.js b/src/matchers/toBeNegative/predicate.test.js deleted file mode 100644 index 3e891f71..00000000 --- a/src/matchers/toBeNegative/predicate.test.js +++ /dev/null @@ -1,28 +0,0 @@ -import predicate from './predicate'; - -describe('toBeNegative Predicate', () => { - describe('returns true', () => { - test('When negative number is passed', () => { - expect(predicate(-1)).toBe(true); - expect(predicate(-100.1)).toBe(true); - }); - }); - - describe('return false', () => { - test('When positive number is passed', () => { - expect(predicate(1)).toBe(false); - expect(predicate(100.1)).toBe(false); - expect(predicate(Infinity)).toBe(false); - }); - - test('When Infinity is passed', () => { - expect(predicate(Infinity)).toBe(false); - }); - - test('When NaN is passed', () => { - expect(predicate(NaN)).toBe(false); - expect(predicate('Not a number!')).toBe(false); - expect(predicate({})).toBe(false); - }); - }); -}); diff --git a/src/matchers/toBeNil.js b/src/matchers/toBeNil.js new file mode 100644 index 00000000..e061e4c2 --- /dev/null +++ b/src/matchers/toBeNil.js @@ -0,0 +1,19 @@ +export function toBeNil(actual) { + const { printReceived, matcherHint } = this.utils; + + const passMessage = + matcherHint('.not.toBeNil', 'received', '') + + '\n\n' + + 'Expected value not to be null or undefined, received:\n' + + ` ${printReceived(actual)}`; + + const failMessage = + matcherHint('.toBeNil', 'received', '') + + '\n\n' + + 'Expected value to be null or undefined, received:\n' + + ` ${printReceived(actual)}`; + + const pass = actual === undefined || actual === null; + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toBeNil/index.js b/src/matchers/toBeNil/index.js deleted file mode 100644 index 770b79ff..00000000 --- a/src/matchers/toBeNil/index.js +++ /dev/null @@ -1,22 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, received) => () => - utils.matcherHint('.not.toBeNil', 'received', '') + - '\n\n' + - 'Expected value not to be null or undefined, received:\n' + - ` ${utils.printReceived(received)}`; - -const failMessage = (utils, received) => () => - utils.matcherHint('.toBeNil', 'received', '') + - '\n\n' + - 'Expected value to be null or undefined, received:\n' + - ` ${utils.printReceived(received)}`; - -export function toBeNil(received) { - const pass = predicate(received); - if (pass) { - return { pass: true, message: passMessage(this.utils, received) }; - } - - return { pass: false, message: failMessage(this.utils, received) }; -} diff --git a/src/matchers/toBeNil/predicate.js b/src/matchers/toBeNil/predicate.js deleted file mode 100644 index cf6b11b3..00000000 --- a/src/matchers/toBeNil/predicate.js +++ /dev/null @@ -1 +0,0 @@ -export default value => value === undefined || value === null; diff --git a/src/matchers/toBeNil/predicate.test.js b/src/matchers/toBeNil/predicate.test.js deleted file mode 100644 index 8f8ad77a..00000000 --- a/src/matchers/toBeNil/predicate.test.js +++ /dev/null @@ -1,15 +0,0 @@ -import predicate from './predicate'; - -describe('toBeNil', () => { - test('returns true when value is undefined', () => { - expect(predicate(undefined)).toBe(true); - }); - - test('returns true when value is null', () => { - expect(predicate(null)).toBe(true); - }); - - test.each([['false'], [['hello', 'world']], [{ hello: 'world' }]])('returns false when given: %s', given => { - expect(predicate(given)).toBe(false); - }); -}); diff --git a/src/matchers/toBeNumber.js b/src/matchers/toBeNumber.js new file mode 100644 index 00000000..abe32231 --- /dev/null +++ b/src/matchers/toBeNumber.js @@ -0,0 +1,19 @@ +export function toBeNumber(actual) { + const { printReceived, matcherHint } = this.utils; + + const passMessage = + matcherHint('.not.toBeNumber', 'received', '') + + '\n\n' + + 'Expected value to not be a number received:\n' + + ` ${printReceived(actual)}`; + + const failMessage = + matcherHint('.toBeNumber', 'received', '') + + '\n\n' + + 'Expected value to be a number received:\n' + + ` ${printReceived(actual)}`; + + const pass = typeof actual === 'number'; + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toBeNumber/index.js b/src/matchers/toBeNumber/index.js deleted file mode 100644 index d5703a84..00000000 --- a/src/matchers/toBeNumber/index.js +++ /dev/null @@ -1,22 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, received) => () => - utils.matcherHint('.not.toBeNumber', 'received', '') + - '\n\n' + - 'Expected value to not be a number received:\n' + - ` ${utils.printReceived(received)}`; - -const failMessage = (utils, received) => () => - utils.matcherHint('.toBeNumber', 'received', '') + - '\n\n' + - 'Expected value to be a number received:\n' + - ` ${utils.printReceived(received)}`; - -export function toBeNumber(expected) { - const pass = predicate(expected); - if (pass) { - return { pass: true, message: passMessage(this.utils, expected) }; - } - - return { pass: false, message: failMessage(this.utils, expected) }; -} diff --git a/src/matchers/toBeNumber/predicate.js b/src/matchers/toBeNumber/predicate.js deleted file mode 100644 index 48c194b0..00000000 --- a/src/matchers/toBeNumber/predicate.js +++ /dev/null @@ -1 +0,0 @@ -export default expected => typeof expected === 'number'; diff --git a/src/matchers/toBeNumber/predicate.test.js b/src/matchers/toBeNumber/predicate.test.js deleted file mode 100644 index f1b453cd..00000000 --- a/src/matchers/toBeNumber/predicate.test.js +++ /dev/null @@ -1,20 +0,0 @@ -import predicate from './predicate'; - -describe('toBeNumber Predicate', () => { - test.each` - number - ${10} - ${NaN} - ${Infinity} - ${-Infinity} - `('returns true when given: $number', ({ number }) => { - expect(predicate(number)).toBe(true); - }); - - test.each([[false], [''], [[]], [{}], [() => {}], [undefined], [null], ['10']])( - 'returns false when given: %s', - given => { - expect(predicate(given)).toBe(false); - }, - ); -}); diff --git a/src/matchers/toBeObject.js b/src/matchers/toBeObject.js new file mode 100644 index 00000000..4df9c75c --- /dev/null +++ b/src/matchers/toBeObject.js @@ -0,0 +1,21 @@ +import { getType } from 'jest-get-type'; + +export function toBeObject(actual) { + const { printReceived, matcherHint } = this.utils; + + const passMessage = + matcherHint('.not.toBeObject', 'received', '') + + '\n\n' + + 'Expected value to not be an object, received:\n' + + ` ${printReceived(actual)}`; + + const failMessage = + matcherHint('.toBeObject', 'received', '') + + '\n\n' + + 'Expected value to be an object, received:\n' + + ` ${printReceived(actual)}`; + + const pass = getType(actual) === 'object'; + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toBeObject/index.js b/src/matchers/toBeObject/index.js deleted file mode 100644 index 7e8fa73b..00000000 --- a/src/matchers/toBeObject/index.js +++ /dev/null @@ -1,22 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, received) => () => - utils.matcherHint('.not.toBeObject', 'received', '') + - '\n\n' + - 'Expected value to not be an object, received:\n' + - ` ${utils.printReceived(received)}`; - -const failMessage = (utils, received) => () => - utils.matcherHint('.toBeObject', 'received', '') + - '\n\n' + - 'Expected value to be an object, received:\n' + - ` ${utils.printReceived(received)}`; - -export function toBeObject(expected) { - const pass = predicate(expected); - if (pass) { - return { pass: true, message: passMessage(this.utils, expected) }; - } - - return { pass: false, message: failMessage(this.utils, expected) }; -} diff --git a/src/matchers/toBeObject/predicate.js b/src/matchers/toBeObject/predicate.js deleted file mode 100644 index ae845849..00000000 --- a/src/matchers/toBeObject/predicate.js +++ /dev/null @@ -1,3 +0,0 @@ -import { getType } from 'jest-get-type'; - -export default expected => getType(expected) === 'object'; diff --git a/src/matchers/toBeObject/predicate.test.js b/src/matchers/toBeObject/predicate.test.js deleted file mode 100644 index c7ae1165..00000000 --- a/src/matchers/toBeObject/predicate.test.js +++ /dev/null @@ -1,11 +0,0 @@ -import predicate from './predicate'; - -describe('toBeObject Predicate', () => { - test('returns true when given an object', () => { - expect(predicate({})).toBe(true); - }); - - test.each([[false], [''], [0], [() => {}], [undefined], [NaN]])('returns false when given: %s', given => { - expect(predicate(given)).toBe(false); - }); -}); diff --git a/src/matchers/toBeOdd.js b/src/matchers/toBeOdd.js new file mode 100644 index 00000000..92e70a32 --- /dev/null +++ b/src/matchers/toBeOdd.js @@ -0,0 +1,19 @@ +export function toBeOdd(received) { + const { printReceived, matcherHint } = this.utils; + + const passMessage = + matcherHint('.not.toBeOdd', 'received', '') + + '\n\n' + + 'Expected value to not be odd received:\n' + + ` ${printReceived(received)}`; + + const failMessage = + matcherHint('.toBeOdd', 'received', '') + + '\n\n' + + 'Expected value to be odd received:\n' + + ` ${printReceived(received)}`; + + const pass = !isNaN(parseInt(received)) && received % 2 === 1; + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toBeOdd/index.js b/src/matchers/toBeOdd/index.js deleted file mode 100644 index a95cb68f..00000000 --- a/src/matchers/toBeOdd/index.js +++ /dev/null @@ -1,22 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, received) => () => - utils.matcherHint('.not.toBeOdd', 'received', '') + - '\n\n' + - 'Expected value to not be odd received:\n' + - ` ${utils.printReceived(received)}`; - -const failMessage = (utils, received) => () => - utils.matcherHint('.toBeOdd', 'received', '') + - '\n\n' + - 'Expected value to be odd received:\n' + - ` ${utils.printReceived(received)}`; - -export function toBeOdd(expected) { - const pass = predicate(expected); - if (pass) { - return { pass: true, message: passMessage(this.utils, expected) }; - } - - return { pass: false, message: failMessage(this.utils, expected) }; -} diff --git a/src/matchers/toBeOdd/predicate.js b/src/matchers/toBeOdd/predicate.js deleted file mode 100644 index c6812611..00000000 --- a/src/matchers/toBeOdd/predicate.js +++ /dev/null @@ -1 +0,0 @@ -export default expected => !isNaN(parseInt(expected)) && expected % 2 === 1; diff --git a/src/matchers/toBeOdd/predicate.test.js b/src/matchers/toBeOdd/predicate.test.js deleted file mode 100644 index db97f611..00000000 --- a/src/matchers/toBeOdd/predicate.test.js +++ /dev/null @@ -1,18 +0,0 @@ -import predicate from './predicate'; - -describe('toBeOdd Predicate', () => { - test('returns true when given an odd number', () => { - expect(predicate(1)).toBe(true); - }); - - test('returns false when given an even number', () => { - expect(predicate(2)).toBe(false); - }); - - test.each([[false], [''], [[]], [{}], [() => {}], [undefined], [null], [NaN]])( - 'returns false when given: %s', - given => { - expect(predicate(given)).toBe(false); - }, - ); -}); diff --git a/src/matchers/toBeOneOf.js b/src/matchers/toBeOneOf.js new file mode 100644 index 00000000..834321da --- /dev/null +++ b/src/matchers/toBeOneOf.js @@ -0,0 +1,25 @@ +import { contains } from '../utils'; + +export function toBeOneOf(actual, expected) { + const { printReceived, printExpected, matcherHint } = this.utils; + + const passMessage = + matcherHint('.not.toBeOneOf', 'item', 'list') + + '\n\n' + + 'Expected value to not be in list:\n' + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const failMessage = + matcherHint('.toBeOneOf', 'item', 'list') + + '\n\n' + + 'Expected value to be in list:\n' + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const pass = contains(this.equals, expected, actual); + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toBeOneOf/index.js b/src/matchers/toBeOneOf/index.js deleted file mode 100644 index 10a7e770..00000000 --- a/src/matchers/toBeOneOf/index.js +++ /dev/null @@ -1,26 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, item, list) => () => - utils.matcherHint('.not.toBeOneOf', 'item', 'list') + - '\n\n' + - 'Expected value to not be in list:\n' + - ` ${utils.printExpected(list)}\n` + - 'Received:\n' + - ` ${utils.printReceived(item)}`; - -const failMessage = (utils, item, list) => () => - utils.matcherHint('.toBeOneOf', 'item', 'list') + - '\n\n' + - 'Expected value to be in list:\n' + - ` ${utils.printExpected(list)}\n` + - 'Received:\n' + - ` ${utils.printReceived(item)}`; - -export function toBeOneOf(item, list) { - const pass = predicate(this.equals, item, list); - if (pass) { - return { pass: true, message: passMessage(this.utils, item, list) }; - } - - return { pass: false, message: failMessage(this.utils, item, list) }; -} diff --git a/src/matchers/toBeOneOf/predicate.js b/src/matchers/toBeOneOf/predicate.js deleted file mode 100644 index 00bbd510..00000000 --- a/src/matchers/toBeOneOf/predicate.js +++ /dev/null @@ -1,3 +0,0 @@ -import { contains } from '../../utils'; - -export default (equals, value, list) => contains(equals, list, value); diff --git a/src/matchers/toBeOneOf/predicate.test.js b/src/matchers/toBeOneOf/predicate.test.js deleted file mode 100644 index 8cb32b87..00000000 --- a/src/matchers/toBeOneOf/predicate.test.js +++ /dev/null @@ -1,22 +0,0 @@ -import { equals } from 'expect/build/jasmineUtils'; -import predicate from './predicate'; - -describe('.toBeOneOf', () => { - test.each([[1], [null], [undefined], [false], ['']])( - 'returns true when primitive value: %s is in given array', - value => { - expect(predicate(equals, value, [1, 2, 3, null, undefined, false, ''])).toBe(true); - }, - ); - - test.each([[{ hello: 'world' }], [['foo']]])('returns true when nested value: %s is in given array', value => { - expect(predicate(equals, value, [1, 2, { hello: 'world' }, ['foo']])).toBe(true); - }); - - test.each([[0], [null], [undefined], [false], [''], [{ hello: 'world' }], [['foo']]])( - 'returns false when value: %s is not in given array', - value => { - expect(predicate(equals, value, [1, 2, 3])).toBe(false); - }, - ); -}); diff --git a/src/matchers/toBePositive.js b/src/matchers/toBePositive.js new file mode 100644 index 00000000..9ae7d0b6 --- /dev/null +++ b/src/matchers/toBePositive.js @@ -0,0 +1,19 @@ +export function toBePositive(actual) { + const { printReceived, matcherHint } = this.utils; + + const passMessage = + matcherHint('.not.toBePositive', 'received', '') + + '\n\n' + + 'Expected value to not be positive received:\n' + + ` ${printReceived(actual)}`; + + const failMessage = + matcherHint('.toBePositive', 'received', '') + + '\n\n' + + 'Expected value to be positive received:\n' + + ` ${printReceived(actual)}`; + + const pass = actual !== true && !isNaN(actual) && actual !== Infinity && actual > 0; + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toBePositive/index.js b/src/matchers/toBePositive/index.js deleted file mode 100644 index 6568ff80..00000000 --- a/src/matchers/toBePositive/index.js +++ /dev/null @@ -1,22 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, received) => () => - utils.matcherHint('.not.toBePositive', 'received', '') + - '\n\n' + - 'Expected value to not be positive received:\n' + - ` ${utils.printReceived(received)}`; - -const failMessage = (utils, received) => () => - utils.matcherHint('.toBePositive', 'received', '') + - '\n\n' + - 'Expected value to be positive received:\n' + - ` ${utils.printReceived(received)}`; - -export function toBePositive(expected) { - const pass = predicate(expected); - if (pass) { - return { pass: true, message: passMessage(this.utils, expected) }; - } - - return { pass: false, message: failMessage(this.utils, expected) }; -} diff --git a/src/matchers/toBePositive/predicate.js b/src/matchers/toBePositive/predicate.js deleted file mode 100644 index 14a691b7..00000000 --- a/src/matchers/toBePositive/predicate.js +++ /dev/null @@ -1 +0,0 @@ -export default expected => expected !== true && !isNaN(expected) && expected !== Infinity && expected > 0; diff --git a/src/matchers/toBePositive/predicate.test.js b/src/matchers/toBePositive/predicate.test.js deleted file mode 100644 index 5ca09828..00000000 --- a/src/matchers/toBePositive/predicate.test.js +++ /dev/null @@ -1,14 +0,0 @@ -import predicate from './predicate'; - -describe('toBePositive Predicate', () => { - test('returns true when given a positive number', () => { - expect(predicate(1)).toBe(true); - }); - - test.each([[false], [''], [-1], [0], [{}], [[]], [() => {}], [undefined], [null], [NaN], [Infinity]])( - 'returns false when given: %s', - given => { - expect(predicate(given)).toBe(false); - }, - ); -}); diff --git a/src/matchers/toBeSealed.js b/src/matchers/toBeSealed.js new file mode 100644 index 00000000..313fc03a --- /dev/null +++ b/src/matchers/toBeSealed.js @@ -0,0 +1,11 @@ +export function toBeSealed(actual) { + const { matcherHint } = this.utils; + + const passMessage = matcherHint('.not.toBeSealed', 'received', '') + '\n\nExpected object to be not sealed'; + + const failMessage = matcherHint('.toBeSealed', 'received', '') + '\n\nExpected object to not sealed'; + + const pass = Object.isSealed(actual); + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toBeSealed/index.js b/src/matchers/toBeSealed/index.js deleted file mode 100644 index 8776ba04..00000000 --- a/src/matchers/toBeSealed/index.js +++ /dev/null @@ -1,16 +0,0 @@ -import predicate from './predicate'; - -const passMessage = utils => () => - utils.matcherHint('.not.toBeSealed', 'received', '') + '\n\nExpected object to be not sealed'; - -const failMessage = utils => () => - utils.matcherHint('.toBeSealed', 'received', '') + '\n\nExpected object to not sealed'; - -export function toBeSealed(expected) { - const pass = predicate(expected); - if (pass) { - return { pass: true, message: passMessage(this.utils) }; - } - - return { pass: false, message: failMessage(this.utils) }; -} diff --git a/src/matchers/toBeSealed/predicate.js b/src/matchers/toBeSealed/predicate.js deleted file mode 100644 index 769f6818..00000000 --- a/src/matchers/toBeSealed/predicate.js +++ /dev/null @@ -1 +0,0 @@ -export default Object.isSealed; diff --git a/src/matchers/toBeSealed/predicate.test.js b/src/matchers/toBeSealed/predicate.test.js deleted file mode 100644 index 85582ae7..00000000 --- a/src/matchers/toBeSealed/predicate.test.js +++ /dev/null @@ -1,11 +0,0 @@ -import predicate from './predicate'; - -describe('toBeSealed Predicate', () => { - test('returns true when given a sealed object', () => { - expect(predicate(Object.seal({}))).toBe(true); - }); - - test('returns false when given a non-sealed object', () => { - expect(predicate({})).toBe(false); - }); -}); diff --git a/src/matchers/toBeString.js b/src/matchers/toBeString.js new file mode 100644 index 00000000..88adf768 --- /dev/null +++ b/src/matchers/toBeString.js @@ -0,0 +1,21 @@ +export function toBeString(expected) { + const { printReceived, printExpected, matcherHint } = this.utils; + + const passMessage = + matcherHint('.not.toBeString', 'received', '') + + '\n\n' + + 'Expected value to not be of type string received:\n' + + ` ${printReceived(expected)}`; + + const failMessage = + matcherHint('.toBeString', 'received', '') + + '\n\n' + + 'Expected value to be of type string:\n' + + ` ${printExpected('type of string')}\n` + + 'Received:\n' + + ` ${printReceived(typeof expected)}`; + + const pass = typeof expected === 'string' || expected instanceof String; + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toBeString/index.js b/src/matchers/toBeString/index.js deleted file mode 100644 index cd6315d3..00000000 --- a/src/matchers/toBeString/index.js +++ /dev/null @@ -1,24 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, received) => () => - utils.matcherHint('.not.toBeString', 'received', '') + - '\n\n' + - 'Expected value to not be of type string received:\n' + - ` ${utils.printReceived(received)}`; - -const failMessage = (utils, received) => () => - utils.matcherHint('.toBeString', 'received', '') + - '\n\n' + - 'Expected value to be of type string:\n' + - ` ${utils.printExpected('type of string')}\n` + - 'Received:\n' + - ` ${utils.printReceived(typeof received)}`; - -export function toBeString(expected) { - const pass = predicate(expected); - if (pass) { - return { pass: true, message: passMessage(this.utils, expected) }; - } - - return { pass: false, message: failMessage(this.utils, expected) }; -} diff --git a/src/matchers/toBeString/predicate.js b/src/matchers/toBeString/predicate.js deleted file mode 100644 index add5d7e3..00000000 --- a/src/matchers/toBeString/predicate.js +++ /dev/null @@ -1,3 +0,0 @@ -export default expected => { - return typeof expected === 'string' || expected instanceof String; -}; diff --git a/src/matchers/toBeString/predicate.test.js b/src/matchers/toBeString/predicate.test.js deleted file mode 100644 index 7f364de1..00000000 --- a/src/matchers/toBeString/predicate.test.js +++ /dev/null @@ -1,18 +0,0 @@ -import predicate from './predicate'; - -describe('toBeString Predicate', () => { - test('returns true when given a string literal', () => { - expect(predicate('string example')).toBe(true); - }); - - test('returns true when given a string object', () => { - expect(predicate(new String('string example'))).toBe(true); - }); - - test.each([[false], [0], [{}], [[]], [() => {}], [undefined], [null], [NaN]])( - 'returns false when given: %s', - given => { - expect(predicate(given)).toBe(false); - }, - ); -}); diff --git a/src/matchers/toBeSymbol.js b/src/matchers/toBeSymbol.js new file mode 100644 index 00000000..a5042a50 --- /dev/null +++ b/src/matchers/toBeSymbol.js @@ -0,0 +1,19 @@ +export function toBeSymbol(actual) { + const { printReceived, matcherHint } = this.utils; + + const passMessage = + matcherHint('.not.toBeSymbol', 'received', '') + + '\n\n' + + 'Expected value to not be a symbol, received:\n' + + ` ${printReceived(actual)}`; + + const failMessage = + matcherHint('.toBeSymbol', 'received', '') + + '\n\n' + + 'Expected to receive a symbol, received:\n' + + ` ${printReceived(actual)}`; + + const pass = typeof actual === 'symbol'; + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toBeSymbol/index.js b/src/matchers/toBeSymbol/index.js deleted file mode 100644 index 782f8135..00000000 --- a/src/matchers/toBeSymbol/index.js +++ /dev/null @@ -1,22 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, received) => () => - utils.matcherHint('.not.toBeSymbol', 'received', '') + - '\n\n' + - 'Expected value to not be a symbol, received:\n' + - ` ${utils.printReceived(received)}`; - -const failMessage = (utils, received) => () => - utils.matcherHint('.toBeSymbol', 'received', '') + - '\n\n' + - 'Expected to receive a symbol, received:\n' + - ` ${utils.printReceived(received)}`; - -export function toBeSymbol(expected) { - const pass = predicate(expected); - if (pass) { - return { pass: true, message: passMessage(this.utils, expected) }; - } - - return { pass: false, message: failMessage(this.utils, expected) }; -} diff --git a/src/matchers/toBeSymbol/predicate.js b/src/matchers/toBeSymbol/predicate.js deleted file mode 100644 index fdea4f7f..00000000 --- a/src/matchers/toBeSymbol/predicate.js +++ /dev/null @@ -1 +0,0 @@ -export default expected => typeof expected === 'symbol'; diff --git a/src/matchers/toBeSymbol/predicate.test.js b/src/matchers/toBeSymbol/predicate.test.js deleted file mode 100644 index 777388ab..00000000 --- a/src/matchers/toBeSymbol/predicate.test.js +++ /dev/null @@ -1,14 +0,0 @@ -import predicate from './predicate'; - -describe('toBeSymbol Predicate', () => { - test('returns true when given a symbol', () => { - expect(predicate(Symbol())).toBe(true); - }); - - test.each([[false], [''], [0], [{}], [[]], [undefined], [null], [NaN], [() => {}]])( - 'returns false when given: %s', - given => { - expect(predicate(given)).toBe(false); - }, - ); -}); diff --git a/src/matchers/toBeTrue.js b/src/matchers/toBeTrue.js new file mode 100644 index 00000000..b9a85c27 --- /dev/null +++ b/src/matchers/toBeTrue.js @@ -0,0 +1,21 @@ +export function toBeTrue(actual) { + const { printReceived, printExpected, matcherHint } = this.utils; + + const passMessage = + matcherHint('.not.toBeTrue', 'received', '') + + '\n\n' + + 'Expected value to not be true received:\n' + + ` ${printReceived(actual)}`; + + const failMessage = + matcherHint('.toBeTrue', 'received', '') + + '\n\n' + + 'Expected value to be true:\n' + + ` ${printExpected(true)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const pass = actual === true; + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toBeTrue/index.js b/src/matchers/toBeTrue/index.js deleted file mode 100644 index ec656b34..00000000 --- a/src/matchers/toBeTrue/index.js +++ /dev/null @@ -1,24 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, received) => () => - utils.matcherHint('.not.toBeTrue', 'received', '') + - '\n\n' + - 'Expected value to not be true received:\n' + - ` ${utils.printReceived(received)}`; - -const failMessage = (utils, received) => () => - utils.matcherHint('.toBeTrue', 'received', '') + - '\n\n' + - 'Expected value to be true:\n' + - ` ${utils.printExpected(true)}\n` + - 'Received:\n' + - ` ${utils.printReceived(received)}`; - -export function toBeTrue(expected) { - const pass = predicate(expected); - if (pass) { - return { pass: true, message: passMessage(this.utils, expected) }; - } - - return { pass: false, message: failMessage(this.utils, expected) }; -} diff --git a/src/matchers/toBeTrue/predicate.js b/src/matchers/toBeTrue/predicate.js deleted file mode 100644 index c64cf191..00000000 --- a/src/matchers/toBeTrue/predicate.js +++ /dev/null @@ -1 +0,0 @@ -export default expected => expected === true; diff --git a/src/matchers/toBeTrue/predicate.test.js b/src/matchers/toBeTrue/predicate.test.js deleted file mode 100644 index 0b05c395..00000000 --- a/src/matchers/toBeTrue/predicate.test.js +++ /dev/null @@ -1,14 +0,0 @@ -import predicate from './predicate'; - -describe('toBeTrue Predicate', () => { - test('returns true when given an array', () => { - expect(predicate(true)).toBe(true); - }); - - test.each([[false], [''], [0], [{}], [[]], [() => {}], [undefined], [null], [NaN]])( - 'returns false when given: %s', - given => { - expect(predicate(given)).toBe(false); - }, - ); -}); diff --git a/src/matchers/toBeValidDate.js b/src/matchers/toBeValidDate.js new file mode 100644 index 00000000..8d576d59 --- /dev/null +++ b/src/matchers/toBeValidDate.js @@ -0,0 +1,21 @@ +import { getType } from 'jest-get-type'; + +export function toBeValidDate(actual) { + const { printReceived, matcherHint } = this.utils; + + const passMessage = + matcherHint('.not.toBeValidDate', 'received', '') + + '\n\n' + + 'Expected value to not be a valid date received:\n' + + ` ${printReceived(actual)}`; + + const failMessage = + matcherHint('.toBeValidDate', 'received', '') + + '\n\n' + + 'Expected value to be a valid date received:\n' + + ` ${printReceived(actual)}`; + + const pass = getType(actual) === 'date' && !isNaN(actual) && !isNaN(actual.getTime()); + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toBeValidDate/index.js b/src/matchers/toBeValidDate/index.js deleted file mode 100644 index b0fddd9a..00000000 --- a/src/matchers/toBeValidDate/index.js +++ /dev/null @@ -1,22 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, received) => () => - utils.matcherHint('.not.toBeValidDate', 'received', '') + - '\n\n' + - 'Expected value to not be a valid date received:\n' + - ` ${utils.printReceived(received)}`; - -const failMessage = (utils, received) => () => - utils.matcherHint('.toBeValidDate', 'received', '') + - '\n\n' + - 'Expected value to be a valid date received:\n' + - ` ${utils.printReceived(received)}`; - -export function toBeValidDate(expected) { - const pass = predicate(expected); - if (pass) { - return { pass: true, message: passMessage(this.utils, expected) }; - } - - return { pass: false, message: failMessage(this.utils, expected) }; -} diff --git a/src/matchers/toBeValidDate/predicate.js b/src/matchers/toBeValidDate/predicate.js deleted file mode 100644 index 1669ddaa..00000000 --- a/src/matchers/toBeValidDate/predicate.js +++ /dev/null @@ -1,5 +0,0 @@ -import { getType } from 'jest-get-type'; - -const isValidDate = value => getType(value) === 'date' && !isNaN(value) && !isNaN(value.getTime()); - -export default isValidDate; diff --git a/src/matchers/toBeValidDate/predicate.test.js b/src/matchers/toBeValidDate/predicate.test.js deleted file mode 100644 index aea62b07..00000000 --- a/src/matchers/toBeValidDate/predicate.test.js +++ /dev/null @@ -1,23 +0,0 @@ -import predicate from './predicate'; - -describe('toBeDate Predicate', () => { - test('returns true when given a valid date', () => { - expect(predicate(new Date('12/25/2017'))).toBe(true); - }); - - test.each([ - [new Date('01/90/2018')], - [new Date('32/01/2018')], - [true], - [false], - [''], - [0], - [{}], - [() => {}], - [undefined], - [null], - [NaN], - ])('returns false when given: %s', given => { - expect(predicate(given)).toBe(false); - }); -}); diff --git a/src/matchers/toBeWithin.js b/src/matchers/toBeWithin.js new file mode 100644 index 00000000..a86107af --- /dev/null +++ b/src/matchers/toBeWithin.js @@ -0,0 +1,23 @@ +export function toBeWithin(actual, start, end) { + const { printReceived, printExpected, matcherHint } = this.utils; + + const passMessage = + matcherHint('.not.toBeWithin') + + '\n\n' + + 'Expected number to not be within start (inclusive) and end (exclusive):\n' + + ` start: ${printExpected(start)} end: ${printExpected(end)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const failMessage = + matcherHint('.toBeWithin') + + '\n\n' + + 'Expected number to be within start (inclusive) and end (exclusive):\n' + + ` start: ${printExpected(start)} end: ${printExpected(end)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const pass = actual >= start && actual < end; + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toBeWithin/index.js b/src/matchers/toBeWithin/index.js deleted file mode 100644 index beafe590..00000000 --- a/src/matchers/toBeWithin/index.js +++ /dev/null @@ -1,26 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, number, start, end) => () => - utils.matcherHint('.not.toBeWithin') + - '\n\n' + - 'Expected number to not be within start (inclusive) and end (exclusive):\n' + - ` start: ${utils.printExpected(start)} end: ${utils.printExpected(end)}\n` + - 'Received:\n' + - ` ${utils.printReceived(number)}`; - -const failMessage = (utils, number, start, end) => () => - utils.matcherHint('.toBeWithin') + - '\n\n' + - 'Expected number to be within start (inclusive) and end (exclusive):\n' + - ` start: ${utils.printExpected(start)} end: ${utils.printExpected(end)}\n` + - 'Received:\n' + - ` ${utils.printReceived(number)}`; - -export function toBeWithin(number, start, end) { - const pass = predicate(number, start, end); - if (pass) { - return { pass: true, message: passMessage(this.utils, number, start, end) }; - } - - return { pass: false, message: failMessage(this.utils, number, start, end) }; -} diff --git a/src/matchers/toBeWithin/predicate.js b/src/matchers/toBeWithin/predicate.js deleted file mode 100644 index d836ae6a..00000000 --- a/src/matchers/toBeWithin/predicate.js +++ /dev/null @@ -1 +0,0 @@ -export default (number, start, end) => number >= start && number < end; diff --git a/src/matchers/toBeWithin/predicate.test.js b/src/matchers/toBeWithin/predicate.test.js deleted file mode 100644 index 2cc092e7..00000000 --- a/src/matchers/toBeWithin/predicate.test.js +++ /dev/null @@ -1,11 +0,0 @@ -import predicate from './predicate'; - -describe('toBeWithin Predicate', () => { - test('returns true when given number is within the given bounds of start (inclusive) and end (exclusive)', () => { - expect(predicate(1, 1, 3)).toBe(true); - }); - - test('returns false when given number is not within the given bounds of start (inclusive) and end (exclusive)', () => { - expect(predicate(3, 1, 3)).toBe(false); - }); -}); diff --git a/src/matchers/toContainAllEntries.js b/src/matchers/toContainAllEntries.js new file mode 100644 index 00000000..621a6412 --- /dev/null +++ b/src/matchers/toContainAllEntries.js @@ -0,0 +1,28 @@ +import { containsEntry } from '../utils'; + +export function toContainAllEntries(actual, expected) { + const { printReceived, printExpected, matcherHint } = this.utils; + + const passMessage = + matcherHint('.not.toContainAllEntries') + + '\n\n' + + 'Expected object to not only contain all of the given entries:\n' + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const failMessage = + matcherHint('.toContainAllEntries') + + '\n\n' + + 'Expected object to only contain all of the given entries:\n' + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const pass = + actual.hasOwnProperty && + expected.length == Object.keys(actual).length && + expected.every(entry => containsEntry(this.equals, actual, entry)); + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toContainAllEntries/index.js b/src/matchers/toContainAllEntries/index.js deleted file mode 100644 index cd27d41c..00000000 --- a/src/matchers/toContainAllEntries/index.js +++ /dev/null @@ -1,26 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, actual, expected) => () => - utils.matcherHint('.not.toContainAllEntries') + - '\n\n' + - 'Expected object to not only contain all of the given entries:\n' + - ` ${utils.printExpected(expected)}\n` + - 'Received:\n' + - ` ${utils.printReceived(actual)}`; - -const failMessage = (utils, actual, expected) => () => - utils.matcherHint('.toContainAllEntries') + - '\n\n' + - 'Expected object to only contain all of the given entries:\n' + - ` ${utils.printExpected(expected)}\n` + - 'Received:\n' + - ` ${utils.printReceived(actual)}`; - -export function toContainAllEntries(actual, expected) { - const pass = predicate(this.equals, actual, expected); - if (pass) { - return { pass: true, message: passMessage(this.utils, actual, expected) }; - } - - return { pass: false, message: failMessage(this.utils, actual, expected) }; -} diff --git a/src/matchers/toContainAllEntries/predicate.js b/src/matchers/toContainAllEntries/predicate.js deleted file mode 100644 index 841d08f6..00000000 --- a/src/matchers/toContainAllEntries/predicate.js +++ /dev/null @@ -1,9 +0,0 @@ -import toContainEntries from '../toContainEntries/predicate'; - -export default (equals, obj, entries) => { - if (!obj.hasOwnProperty || entries.length != Object.keys(obj).length) { - return false; - } - - return toContainEntries(equals, obj, entries); -}; diff --git a/src/matchers/toContainAllEntries/predicate.test.js b/src/matchers/toContainAllEntries/predicate.test.js deleted file mode 100644 index 0aea4cf1..00000000 --- a/src/matchers/toContainAllEntries/predicate.test.js +++ /dev/null @@ -1,39 +0,0 @@ -import { equals } from 'expect/build/jasmineUtils'; -import predicate from './predicate'; - -const data = { a: 'foo', b: 'bar', c: 'baz' }; - -describe('.toContainAllEntries', () => { - test('passes when given nested values', () => { - expect(predicate(equals, { hello: { message: 'world' } }, [['hello', { message: 'world' }]])).toBe(true); - }); - test('passes when object only contains all of the given entries', () => { - expect( - predicate(equals, data, [ - ['a', 'foo'], - ['b', 'bar'], - ['c', 'baz'], - ]), - ).toBe(true); - }); - - test('fails when object does not only contain all of the given entries', () => { - expect( - predicate(equals, data, [ - ['a', 'foo'], - ['b', 'bar'], - ]), - ).toBe(false); - }); - - test('fails when object does not contain all of the given entries', () => { - expect( - predicate(equals, data, [ - ['a', 'foo'], - ['b', 'bar'], - ['c', 'baz'], - ['d', 'qux'], - ]), - ).toBe(false); - }); -}); diff --git a/src/matchers/toContainAllKeys.js b/src/matchers/toContainAllKeys.js new file mode 100644 index 00000000..061c5b0f --- /dev/null +++ b/src/matchers/toContainAllKeys.js @@ -0,0 +1,26 @@ +import { contains } from '../utils'; + +export function toContainAllKeys(actual, expected) { + const { printExpected, printReceived, matcherHint } = this.utils; + + const passMessage = + matcherHint('.not.toContainAllKeys') + + '\n\n' + + 'Expected object to not contain all keys:\n' + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(Object.keys(actual))}`; + + const failMessage = + matcherHint('.toContainAllKeys') + + '\n\n' + + 'Expected object to contain all keys:\n' + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(Object.keys(actual))}`; + + const objectKeys = Object.keys(actual); + const pass = objectKeys.length === expected.length && expected.every(key => contains(this.equals, objectKeys, key)); + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toContainAllKeys/index.js b/src/matchers/toContainAllKeys/index.js deleted file mode 100644 index 9dedd613..00000000 --- a/src/matchers/toContainAllKeys/index.js +++ /dev/null @@ -1,26 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, actual, expected) => () => - utils.matcherHint('.not.toContainAllKeys') + - '\n\n' + - 'Expected object to not contain all keys:\n' + - ` ${utils.printExpected(expected)}\n` + - 'Received:\n' + - ` ${utils.printReceived(Object.keys(actual))}`; - -const failMessage = (utils, actual, expected) => () => - utils.matcherHint('.toContainAllKeys') + - '\n\n' + - 'Expected object to contain all keys:\n' + - ` ${utils.printExpected(expected)}\n` + - 'Received:\n' + - ` ${utils.printReceived(Object.keys(actual))}`; - -export function toContainAllKeys(actual, expected) { - const pass = predicate(this.equals, actual, expected); - if (pass) { - return { pass: true, message: passMessage(this.utils, actual, expected) }; - } - - return { pass: false, message: failMessage(this.utils, actual, expected) }; -} diff --git a/src/matchers/toContainAllKeys/predicate.js b/src/matchers/toContainAllKeys/predicate.js deleted file mode 100644 index 78a1f3f2..00000000 --- a/src/matchers/toContainAllKeys/predicate.js +++ /dev/null @@ -1,7 +0,0 @@ -import { contains } from '../../utils'; - -export default (equals, object, keys) => { - const objectKeys = Object.keys(object); - - return objectKeys.length === keys.length && keys.every(key => contains(equals, objectKeys, key)); -}; diff --git a/src/matchers/toContainAllKeys/predicate.test.js b/src/matchers/toContainAllKeys/predicate.test.js deleted file mode 100644 index cf4c3341..00000000 --- a/src/matchers/toContainAllKeys/predicate.test.js +++ /dev/null @@ -1,26 +0,0 @@ -import { equals } from 'expect/build/jasmineUtils'; -import predicate from './predicate'; - -const data = { a: 'hello', b: 'world' }; - -describe('.toContainAllKeys', () => { - test('passes when given object contains all keys', () => { - expect(predicate(equals, data, ['a', 'b'])).toBe(true); - }); - - test('passes when given object contains all keys, regardless of order', () => { - expect(predicate(equals, data, ['b', 'a'])).toBe(true); - }); - - test('fails when given object does not contain all keys', () => { - expect(predicate(equals, data, ['b'])).toBe(false); - }); - - test('fails when given an empty object', () => { - expect(predicate(equals, {}, ['b'])).toBe(false); - }); - - test('fails when all of the object keys are matched, but there are additional keys ', () => { - expect(predicate(equals, data, ['a', 'b', 'c'])).toBe(false); - }); -}); diff --git a/src/matchers/toContainAllValues.js b/src/matchers/toContainAllValues.js new file mode 100644 index 00000000..d3d68d5a --- /dev/null +++ b/src/matchers/toContainAllValues.js @@ -0,0 +1,27 @@ +import { contains } from '../utils'; + +export function toContainAllValues(actual, expected) { + const { printReceived, printExpected, matcherHint } = this.utils; + + const passMessage = + matcherHint('.not.toContainAllValues') + + '\n\n' + + 'Expected object to not contain all values:\n' + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const failMessage = + matcherHint('.toContainAllValues') + + '\n\n' + + 'Expected object to contain all values:\n' + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const values = Object.keys(actual).map(k => actual[k]); + const pass = + values.length === expected.length && values.every(objectValue => contains(this.equals, expected, objectValue)); + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toContainAllValues/index.js b/src/matchers/toContainAllValues/index.js deleted file mode 100644 index 2e6a5e13..00000000 --- a/src/matchers/toContainAllValues/index.js +++ /dev/null @@ -1,26 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, actual, expected) => () => - utils.matcherHint('.not.toContainAllValues') + - '\n\n' + - 'Expected object to not contain all values:\n' + - ` ${utils.printExpected(expected)}\n` + - 'Received:\n' + - ` ${utils.printReceived(actual)}`; - -const failMessage = (utils, actual, expected) => () => - utils.matcherHint('.toContainAllValues') + - '\n\n' + - 'Expected object to contain all values:\n' + - ` ${utils.printExpected(expected)}\n` + - 'Received:\n' + - ` ${utils.printReceived(actual)}`; - -export function toContainAllValues(actual, expected) { - const pass = predicate(this.equals, actual, expected); - if (pass) { - return { pass: true, message: passMessage(this.utils, actual, expected) }; - } - - return { pass: false, message: failMessage(this.utils, actual, expected) }; -} diff --git a/src/matchers/toContainAllValues/predicate.js b/src/matchers/toContainAllValues/predicate.js deleted file mode 100644 index 1dba7b5d..00000000 --- a/src/matchers/toContainAllValues/predicate.js +++ /dev/null @@ -1,8 +0,0 @@ -import { contains } from '../../utils'; - -export default (equals, object, values) => { - const objectValues = Object.keys(object).map(k => object[k]); - return ( - objectValues.length === values.length && objectValues.every(objectValue => contains(equals, values, objectValue)) - ); -}; diff --git a/src/matchers/toContainAllValues/predicate.test.js b/src/matchers/toContainAllValues/predicate.test.js deleted file mode 100644 index 8cab9739..00000000 --- a/src/matchers/toContainAllValues/predicate.test.js +++ /dev/null @@ -1,50 +0,0 @@ -import { equals } from 'expect/build/jasmineUtils'; -import predicate from './predicate'; - -describe('toContainAllValues Predicate', () => { - const shallow = { - hello: 'world', - foo: 0, - bar: false, - }; - const deep = { - message: shallow, - donald: 'duck', - }; - const deepArray = { - message: [shallow], - donald: 'duck', - }; - - describe('returns true', () => { - test('when given object contains all primitive values', () => { - expect(predicate(equals, shallow, ['world', false, 0])).toBe(true); - }); - - test('when given object contains all values including objects', () => { - expect(predicate(equals, deep, ['duck', { hello: 'world', foo: 0, bar: false }])).toBe(true); - }); - - test('when given object contains all values including arrays', () => { - expect(predicate(equals, deepArray, ['duck', [{ hello: 'world', foo: 0, bar: false }]])).toBe(true); - }); - }); - - describe('returns false', () => { - test('returns false when object does not contain all values', () => { - const o = { a: 'foo', b: 'bar', c: 'baz' }; - expect(predicate(equals, o, ['foo', 'bar', 'baz', 'qux'])).toBe(false); - }); - test('when given object does not contain all primitive value', () => { - expect(predicate(equals, shallow, ['world', false])).toBe(false); - }); - - test('when given object does not contain all values including objects', () => { - expect(predicate(equals, deep, ['made up value', 'duck', { hello: 'world', foo: 0 }])).toBe(false); - }); - - test('when given object does not contain all values including arrays', () => { - expect(predicate(equals, deepArray, ['duck', 'made up value', [{ hello: 'world', foo: 0 }]])).toBe(false); - }); - }); -}); diff --git a/src/matchers/toContainAnyEntries.js b/src/matchers/toContainAnyEntries.js new file mode 100644 index 00000000..290f96ec --- /dev/null +++ b/src/matchers/toContainAnyEntries.js @@ -0,0 +1,26 @@ +import { contains } from '../utils'; + +export function toContainAnyEntries(actual, expected) { + const { printReceived, printExpected, matcherHint } = this.utils; + + const passMessage = + matcherHint('.not.toContainAnyEntries') + + '\n\n' + + 'Expected object to not contain any of the provided entries:\n' + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const failMessage = + matcherHint('.toContainAnyEntries') + + '\n\n' + + 'Expected object to contain any of the provided entries:\n' + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const entries = Object.keys(actual).map(k => [k, actual[k]]); + const pass = expected.some(entry => contains(this.equals, entries, entry)); + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toContainAnyEntries/index.js b/src/matchers/toContainAnyEntries/index.js deleted file mode 100644 index f9ae1b48..00000000 --- a/src/matchers/toContainAnyEntries/index.js +++ /dev/null @@ -1,25 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, object, entries) => () => - utils.matcherHint('.not.toContainAnyEntries') + - '\n\n' + - 'Expected object to not contain any of the provided entries:\n' + - ` ${utils.printExpected(entries)}\n` + - 'Received:\n' + - ` ${utils.printReceived(object)}`; - -const failMessage = (utils, object, entries) => () => - utils.matcherHint('.toContainAnyEntries') + - '\n\n' + - 'Expected object to contain any of the provided entries:\n' + - ` ${utils.printExpected(entries)}\n` + - 'Received:\n' + - ` ${utils.printReceived(object)}`; - -export function toContainAnyEntries(object, entries) { - const pass = predicate(this.equals, object, entries); - if (pass) { - return { pass: true, message: passMessage(this.utils, object, entries) }; - } - return { pass: false, message: failMessage(this.utils, object, entries) }; -} diff --git a/src/matchers/toContainAnyEntries/predicate.js b/src/matchers/toContainAnyEntries/predicate.js deleted file mode 100644 index 36a90d62..00000000 --- a/src/matchers/toContainAnyEntries/predicate.js +++ /dev/null @@ -1,6 +0,0 @@ -import { contains } from '../../utils'; - -export default (equals, object, entries) => { - const objectEntries = Object.keys(object).map(k => [k, object[k]]); - return entries.some(entry => contains(equals, objectEntries, entry)); -}; diff --git a/src/matchers/toContainAnyEntries/predicate.test.js b/src/matchers/toContainAnyEntries/predicate.test.js deleted file mode 100644 index 320f8d46..00000000 --- a/src/matchers/toContainAnyEntries/predicate.test.js +++ /dev/null @@ -1,36 +0,0 @@ -import { equals } from 'expect/build/jasmineUtils'; -import predicate from './predicate'; - -const data = { a: 'foo', b: 'bar', c: 'baz' }; - -describe('.toContainAnyEntries', () => { - test('passes when given object contains entries', () => { - expect( - predicate(equals, data, [ - ['a', 'qux'], - ['a', 'foo'], - ['x', 'foo'], - ]), - ).toBe(true); - }); - - test('passes when given object contains entries with nested values', () => { - expect( - predicate(equals, { hello: { message: 'world' } }, [ - ['hello', { message: 'world' }], - ['a', 'foo'], - ['x', 'foo'], - ]), - ).toBe(true); - }); - - test('fails when given object does not contain entries', () => { - expect( - predicate(equals, data, [ - ['a', 'qux'], - ['b', 'foo'], - ['x', 'foo'], - ]), - ).toBe(false); - }); -}); diff --git a/src/matchers/toContainAnyKeys.js b/src/matchers/toContainAnyKeys.js new file mode 100644 index 00000000..50580296 --- /dev/null +++ b/src/matchers/toContainAnyKeys.js @@ -0,0 +1,23 @@ +export function toContainAnyKeys(actual, expected) { + const { printReceived, printExpected, matcherHint } = this.utils; + + const passMessage = + matcherHint('.not.toContainAnyKeys') + + '\n\n' + + 'Expected object not to contain any of the following keys:\n' + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const failMessage = + matcherHint('.toContainValue') + + '\n\n' + + 'Expected object to contain any of the following keys:\n' + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const pass = expected.some(key => Object.prototype.hasOwnProperty.call(actual, key)); + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toContainAnyKeys/index.js b/src/matchers/toContainAnyKeys/index.js deleted file mode 100644 index 30dfad2b..00000000 --- a/src/matchers/toContainAnyKeys/index.js +++ /dev/null @@ -1,26 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, actual, expected) => () => - utils.matcherHint('.not.toContainAnyKeys') + - '\n\n' + - 'Expected object not to contain any of the following keys:\n' + - ` ${utils.printExpected(expected)}\n` + - 'Received:\n' + - ` ${utils.printReceived(actual)}`; - -const failMessage = (utils, actual, expected) => () => - utils.matcherHint('.toContainValue') + - '\n\n' + - 'Expected object to contain any of the following keys:\n' + - ` ${utils.printExpected(expected)}\n` + - 'Received:\n' + - ` ${utils.printReceived(actual)}`; - -export function toContainAnyKeys(actual, expected) { - const pass = predicate(actual, expected); - - return { - pass: pass, - message: pass ? passMessage(this.utils, actual, expected) : failMessage(this.utils, actual, expected), - }; -} diff --git a/src/matchers/toContainAnyKeys/predicate.js b/src/matchers/toContainAnyKeys/predicate.js deleted file mode 100644 index c9c8362c..00000000 --- a/src/matchers/toContainAnyKeys/predicate.js +++ /dev/null @@ -1 +0,0 @@ -export default (actual, values) => values.some(value => Object.prototype.hasOwnProperty.call(actual, value)); diff --git a/src/matchers/toContainAnyKeys/predicate.test.js b/src/matchers/toContainAnyKeys/predicate.test.js deleted file mode 100644 index def5346b..00000000 --- a/src/matchers/toContainAnyKeys/predicate.test.js +++ /dev/null @@ -1,29 +0,0 @@ -import predicate from './predicate'; - -describe('toContainAnyKeys Predicate', () => { - describe('returns true', () => { - test('when one or more key is found in the object', () => { - var test = predicate( - { - name: 'Steve the Pirate', - }, - ['name'], - ); - - expect(test).toBe(true); - }); - }); - - describe('returns false', () => { - test('when no keys are found in the object', () => { - var test = predicate( - { - name: 'Steve the Pirate', - }, - ['age'], - ); - - expect(test).toBe(false); - }); - }); -}); diff --git a/src/matchers/toContainAnyValues.js b/src/matchers/toContainAnyValues.js new file mode 100644 index 00000000..3d397edf --- /dev/null +++ b/src/matchers/toContainAnyValues.js @@ -0,0 +1,26 @@ +import { contains } from '../utils'; + +export function toContainAnyValues(actual, expected) { + const { printReceived, printExpected, matcherHint } = this.utils; + + const passMessage = + matcherHint('.not.toContainAnyValues') + + '\n\n' + + 'Expected object to not contain any of the following values:\n' + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const failMessage = + matcherHint('.toContainAnyValues') + + '\n\n' + + 'Expected object to contain any of the following values:\n' + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const objectValues = Object.keys(actual).map(k => actual[k]); + const pass = expected.some(value => contains(this.equals, objectValues, value)); + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toContainAnyValues/index.js b/src/matchers/toContainAnyValues/index.js deleted file mode 100644 index 4461ddb2..00000000 --- a/src/matchers/toContainAnyValues/index.js +++ /dev/null @@ -1,26 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, actual, expected) => () => - utils.matcherHint('.not.toContainAnyValues') + - '\n\n' + - 'Expected object to not contain any of the following values:\n' + - ` ${utils.printExpected(expected)}\n` + - 'Received:\n' + - ` ${utils.printReceived(actual)}`; - -const failMessage = (utils, actual, expected) => () => - utils.matcherHint('.toContainAnyValues') + - '\n\n' + - 'Expected object to contain any of the following values:\n' + - ` ${utils.printExpected(expected)}\n` + - 'Received:\n' + - ` ${utils.printReceived(actual)}`; - -export function toContainAnyValues(actual, expected) { - const pass = predicate(this.equals, actual, expected); - if (pass) { - return { pass: true, message: passMessage(this.utils, actual, expected) }; - } - - return { pass: false, message: failMessage(this.utils, actual, expected) }; -} diff --git a/src/matchers/toContainAnyValues/predicate.js b/src/matchers/toContainAnyValues/predicate.js deleted file mode 100644 index 5d423162..00000000 --- a/src/matchers/toContainAnyValues/predicate.js +++ /dev/null @@ -1,10 +0,0 @@ -import { contains } from '../../utils'; - -/** - * @params {Object} object - * @params {Array} values - */ -export default (equals, object, values) => { - const objectValues = Object.keys(object).map(k => object[k]); - return values.some(value => contains(equals, objectValues, value)); -}; diff --git a/src/matchers/toContainAnyValues/predicate.test.js b/src/matchers/toContainAnyValues/predicate.test.js deleted file mode 100644 index f0a4b57c..00000000 --- a/src/matchers/toContainAnyValues/predicate.test.js +++ /dev/null @@ -1,18 +0,0 @@ -import { equals } from 'expect/build/jasmineUtils'; -import predicate from './predicate'; - -const data = { a: 'foo', b: 'bar', c: 'baz' }; - -describe('toContainAnyValues Predicate', () => { - test('passes when object contains at least one of the given values ', () => { - expect(predicate(equals, data, ['qux', 'foo'])).toBe(true); - expect(predicate(equals, data, ['qux', 'bar'])).toBe(true); - expect(predicate(equals, data, ['foo', 'bar'])).toBe(true); - expect(predicate(equals, data, ['baz'])).toBe(true); - }); - - test('fails when object does not contain any given values', () => { - expect(predicate(equals, data, ['qux'])).toBe(false); - expect(predicate(equals, data, ['qux', 'zoo'])).toBe(false); - }); -}); diff --git a/src/matchers/toContainEntries.js b/src/matchers/toContainEntries.js new file mode 100644 index 00000000..57be4209 --- /dev/null +++ b/src/matchers/toContainEntries.js @@ -0,0 +1,25 @@ +import { containsEntry } from '../utils'; + +export function toContainEntries(actual, expected) { + const { printReceived, printExpected, matcherHint } = this.utils; + + const passMessage = + matcherHint('.not.toContainEntries') + + '\n\n' + + 'Expected object to not contain all of the given entries:\n' + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const failMessage = + matcherHint('.toContainEntries') + + '\n\n' + + 'Expected object to contain all of the given entries:\n' + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const pass = expected.every(entry => containsEntry(this.equals, actual, entry)); + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toContainEntries/index.js b/src/matchers/toContainEntries/index.js deleted file mode 100644 index edb79f73..00000000 --- a/src/matchers/toContainEntries/index.js +++ /dev/null @@ -1,26 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, actual, expected) => () => - utils.matcherHint('.not.toContainEntries') + - '\n\n' + - 'Expected object to not contain all of the given entries:\n' + - ` ${utils.printExpected(expected)}\n` + - 'Received:\n' + - ` ${utils.printReceived(actual)}`; - -const failMessage = (utils, actual, expected) => () => - utils.matcherHint('.toContainEntries') + - '\n\n' + - 'Expected object to contain all of the given entries:\n' + - ` ${utils.printExpected(expected)}\n` + - 'Received:\n' + - ` ${utils.printReceived(actual)}`; - -export function toContainEntries(actual, expected) { - const pass = predicate(this.equals, actual, expected); - if (pass) { - return { pass: true, message: passMessage(this.utils, actual, expected) }; - } - - return { pass: false, message: failMessage(this.utils, actual, expected) }; -} diff --git a/src/matchers/toContainEntries/predicate.js b/src/matchers/toContainEntries/predicate.js deleted file mode 100644 index 652bfe15..00000000 --- a/src/matchers/toContainEntries/predicate.js +++ /dev/null @@ -1,3 +0,0 @@ -import toContainEntry from '../toContainEntry/predicate'; - -export default (equals, obj, entries) => entries.every(entry => toContainEntry(equals, obj, entry)); diff --git a/src/matchers/toContainEntries/predicate.test.js b/src/matchers/toContainEntries/predicate.test.js deleted file mode 100644 index db520bc5..00000000 --- a/src/matchers/toContainEntries/predicate.test.js +++ /dev/null @@ -1,28 +0,0 @@ -import { equals } from 'expect/build/jasmineUtils'; -import predicate from './predicate'; - -const data = { a: 'foo', b: 'bar', c: 'baz' }; - -describe('.toContainEntries', () => { - test('passes when object contains given entry', () => { - expect( - predicate(equals, data, [ - ['c', 'baz'], - ['a', 'foo'], - ]), - ).toBe(true); - }); - - test('passes when given nested values', () => { - expect(predicate(equals, { hello: { message: 'world' } }, [['hello', { message: 'world' }]])).toBe(true); - }); - - test('fails when object does not contain given entry', () => { - expect( - predicate(equals, data, [ - ['a', 'qux'], - ['b', 'bar'], - ]), - ).toBe(false); - }); -}); diff --git a/src/matchers/toContainEntry.js b/src/matchers/toContainEntry.js new file mode 100644 index 00000000..175cb430 --- /dev/null +++ b/src/matchers/toContainEntry.js @@ -0,0 +1,25 @@ +import { containsEntry } from '../utils'; + +export function toContainEntry(actual, expected) { + const { printReceived, printExpected, matcherHint } = this.utils; + + const passMessage = + matcherHint('.not.toContainEntry') + + '\n\n' + + 'Expected object to not contain entry:\n' + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const failMessage = + matcherHint('.toContainEntry') + + '\n\n' + + 'Expected object to contain entry:\n' + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const pass = containsEntry(this.equals, actual, expected); + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toContainEntry/index.js b/src/matchers/toContainEntry/index.js deleted file mode 100644 index 6d728e5b..00000000 --- a/src/matchers/toContainEntry/index.js +++ /dev/null @@ -1,26 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, actual, expected) => () => - utils.matcherHint('.not.toContainEntry') + - '\n\n' + - 'Expected object to not contain entry:\n' + - ` ${utils.printExpected(expected)}\n` + - 'Received:\n' + - ` ${utils.printReceived(actual)}`; - -const failMessage = (utils, actual, expected) => () => - utils.matcherHint('.toContainEntry') + - '\n\n' + - 'Expected object to contain entry:\n' + - ` ${utils.printExpected(expected)}\n` + - 'Received:\n' + - ` ${utils.printReceived(actual)}`; - -export function toContainEntry(actual, expected) { - const pass = predicate(this.equals, actual, expected); - if (pass) { - return { pass: true, message: passMessage(this.utils, actual, expected) }; - } - - return { pass: false, message: failMessage(this.utils, actual, expected) }; -} diff --git a/src/matchers/toContainEntry/predicate.js b/src/matchers/toContainEntry/predicate.js deleted file mode 100644 index f3324b77..00000000 --- a/src/matchers/toContainEntry/predicate.js +++ /dev/null @@ -1,2 +0,0 @@ -export default (equals, obj, [key, value]) => - obj.hasOwnProperty && Object.prototype.hasOwnProperty.call(obj, key) && equals(obj[key], value); diff --git a/src/matchers/toContainEntry/predicate.test.js b/src/matchers/toContainEntry/predicate.test.js deleted file mode 100644 index e79565a1..00000000 --- a/src/matchers/toContainEntry/predicate.test.js +++ /dev/null @@ -1,20 +0,0 @@ -import { equals } from 'expect/build/jasmineUtils'; -import predicate from './predicate'; - -const data = { a: 'foo', b: 'bar', c: 'baz' }; - -describe('.toContainEntry', () => { - test('passes when object contains given entry', () => { - expect(predicate(equals, data, ['a', 'foo'])).toBe(true); - expect(predicate(equals, data, ['b', 'bar'])).toBe(true); - expect(predicate(equals, data, ['c', 'baz'])).toBe(true); - }); - - test('passes when object contain given entry with nested value', () => { - expect(predicate(equals, { data }, ['data', data])).toBe(true); - }); - - test('fails when object does not contain given entry', () => { - expect(predicate(equals, data, ['a', 'qux'])).toBe(false); - }); -}); diff --git a/src/matchers/toContainKey.js b/src/matchers/toContainKey.js new file mode 100644 index 00000000..8fb841a6 --- /dev/null +++ b/src/matchers/toContainKey.js @@ -0,0 +1,23 @@ +export function toContainKey(actual, expected) { + const { printReceived, printExpected, matcherHint } = this.utils; + + const passMessage = + matcherHint('.not.toContainKey') + + '\n\n' + + 'Expected object to not contain key:\n' + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const failMessage = + matcherHint('.toContainKey') + + '\n\n' + + 'Expected object to contain key:\n' + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const pass = actual.hasOwnProperty && Object.prototype.hasOwnProperty.call(actual, expected); + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toContainKey/index.js b/src/matchers/toContainKey/index.js deleted file mode 100644 index 44cee9b3..00000000 --- a/src/matchers/toContainKey/index.js +++ /dev/null @@ -1,26 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, actual, expected) => () => - utils.matcherHint('.not.toContainKey') + - '\n\n' + - 'Expected object to not contain key:\n' + - ` ${utils.printExpected(expected)}\n` + - 'Received:\n' + - ` ${utils.printReceived(actual)}`; - -const failMessage = (utils, actual, expected) => () => - utils.matcherHint('.toContainKey') + - '\n\n' + - 'Expected object to contain key:\n' + - ` ${utils.printExpected(expected)}\n` + - 'Received:\n' + - ` ${utils.printReceived(actual)}`; - -export function toContainKey(actual, expected) { - const pass = predicate(actual, expected); - if (pass) { - return { pass: true, message: passMessage(this.utils, actual, expected) }; - } - - return { pass: false, message: failMessage(this.utils, actual, expected) }; -} diff --git a/src/matchers/toContainKey/predicate.js b/src/matchers/toContainKey/predicate.js deleted file mode 100644 index 89b9744b..00000000 --- a/src/matchers/toContainKey/predicate.js +++ /dev/null @@ -1 +0,0 @@ -export default (obj, key) => obj.hasOwnProperty && Object.prototype.hasOwnProperty.call(obj, key); diff --git a/src/matchers/toContainKey/predicate.test.js b/src/matchers/toContainKey/predicate.test.js deleted file mode 100644 index 1b426401..00000000 --- a/src/matchers/toContainKey/predicate.test.js +++ /dev/null @@ -1,13 +0,0 @@ -import predicate from './predicate'; - -const data = { hello: 'world' }; - -describe('.toContainKey', () => { - test('passes when given object contains key', () => { - expect(predicate(data, 'hello')).toBe(true); - }); - - test('fails when given object does not contain key', () => { - expect(predicate(data, 'missing')).toBe(false); - }); -}); diff --git a/src/matchers/toContainKeys.js b/src/matchers/toContainKeys.js new file mode 100644 index 00000000..bacd8871 --- /dev/null +++ b/src/matchers/toContainKeys.js @@ -0,0 +1,25 @@ +export function toContainKeys(actual, expected) { + const { printReceived, printExpected, matcherHint } = this.utils; + + const passMessage = + matcherHint('.not.toContainKeys') + + '\n\n' + + 'Expected object to not contain all keys:\n' + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const failMessage = + matcherHint('.toContainKeys') + + '\n\n' + + 'Expected object to contain all keys:\n' + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const pass = expected.every( + key => actual && actual.hasOwnProperty && Object.prototype.hasOwnProperty.call(actual, key), + ); + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toContainKeys/index.js b/src/matchers/toContainKeys/index.js deleted file mode 100644 index 47cca7a3..00000000 --- a/src/matchers/toContainKeys/index.js +++ /dev/null @@ -1,26 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, actual, expected) => () => - utils.matcherHint('.not.toContainKeys') + - '\n\n' + - 'Expected object to not contain all keys:\n' + - ` ${utils.printExpected(expected)}\n` + - 'Received:\n' + - ` ${utils.printReceived(actual)}`; - -const failMessage = (utils, actual, expected) => () => - utils.matcherHint('.toContainKeys') + - '\n\n' + - 'Expected object to contain all keys:\n' + - ` ${utils.printExpected(expected)}\n` + - 'Received:\n' + - ` ${utils.printReceived(actual)}`; - -export function toContainKeys(actual, expected) { - const pass = predicate(actual, expected); - if (pass) { - return { pass: true, message: passMessage(this.utils, actual, expected) }; - } - - return { pass: false, message: failMessage(this.utils, actual, expected) }; -} diff --git a/src/matchers/toContainKeys/predicate.js b/src/matchers/toContainKeys/predicate.js deleted file mode 100644 index b1f84385..00000000 --- a/src/matchers/toContainKeys/predicate.js +++ /dev/null @@ -1,2 +0,0 @@ -export default (obj, keys) => - keys.every(key => obj && obj.hasOwnProperty && Object.prototype.hasOwnProperty.call(obj, key)); diff --git a/src/matchers/toContainKeys/predicate.test.js b/src/matchers/toContainKeys/predicate.test.js deleted file mode 100644 index 85f5e17b..00000000 --- a/src/matchers/toContainKeys/predicate.test.js +++ /dev/null @@ -1,19 +0,0 @@ -import predicate from './predicate'; - -const data = { a: 'foo', b: 'bar', c: 'baz' }; - -describe('.toContainKeys', () => { - test('passes when object contains all keys', () => { - expect(predicate(data, ['a', 'b'])).toBe(true); - }); - - test('fails when object does not contain all keys', () => { - expect(predicate(data, ['d'])).toBe(false); - }); - - test('does not throw when object is undefined', () => { - expect(() => { - predicate(undefined, ['d']); - }).not.toThrow(); - }); -}); diff --git a/src/matchers/toContainValue.js b/src/matchers/toContainValue.js new file mode 100644 index 00000000..34bf78a5 --- /dev/null +++ b/src/matchers/toContainValue.js @@ -0,0 +1,26 @@ +import { contains } from '../utils'; + +export function toContainValue(actual, expected) { + const { printReceived, printExpected, matcherHint } = this.utils; + + const passMessage = + matcherHint('.not.toContainValue') + + '\n\n' + + 'Expected object to not contain value:\n' + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const failMessage = + matcherHint('.toContainValue') + + '\n\n' + + 'Expected object to contain value:\n' + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const values = Object.keys(actual).map(k => actual[k]); + const pass = contains(this.equals, values, expected); + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toContainValue/index.js b/src/matchers/toContainValue/index.js deleted file mode 100644 index b17b528a..00000000 --- a/src/matchers/toContainValue/index.js +++ /dev/null @@ -1,26 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, actual, expected) => () => - utils.matcherHint('.not.toContainValue') + - '\n\n' + - 'Expected object to not contain value:\n' + - ` ${utils.printExpected(expected)}\n` + - 'Received:\n' + - ` ${utils.printReceived(actual)}`; - -const failMessage = (utils, actual, expected) => () => - utils.matcherHint('.toContainValue') + - '\n\n' + - 'Expected object to contain value:\n' + - ` ${utils.printExpected(expected)}\n` + - 'Received:\n' + - ` ${utils.printReceived(actual)}`; - -export function toContainValue(actual, expected) { - const pass = predicate(this.equals, actual, expected); - if (pass) { - return { pass: true, message: passMessage(this.utils, actual, expected) }; - } - - return { pass: false, message: failMessage(this.utils, actual, expected) }; -} diff --git a/src/matchers/toContainValue/predicate.js b/src/matchers/toContainValue/predicate.js deleted file mode 100644 index b89af493..00000000 --- a/src/matchers/toContainValue/predicate.js +++ /dev/null @@ -1,6 +0,0 @@ -import { contains } from '../../utils'; - -export default (equals, actual, value) => { - const objectValues = Object.keys(actual).map(k => actual[k]); - return contains(equals, objectValues, value); -}; diff --git a/src/matchers/toContainValue/predicate.test.js b/src/matchers/toContainValue/predicate.test.js deleted file mode 100644 index 96a08e1c..00000000 --- a/src/matchers/toContainValue/predicate.test.js +++ /dev/null @@ -1,42 +0,0 @@ -import { equals } from 'expect/build/jasmineUtils'; -import predicate from './predicate'; - -describe('toContainValue Predicate', () => { - const shallow = { hello: 'world', bool: false, nothing: undefined, absent: null, empty: '', zero: 0 }; - const deep = { message: { hello: 'world' } }; - const deepArray = { message: [{ hello: 'world' }] }; - - describe('returns true', () => { - test.each([['world'], [false], [undefined], [null], [''], [0]])( - 'when given object contains primitive value: %s', - value => { - expect(predicate(equals, shallow, value)).toBe(true); - }, - ); - - test('when given object contains object value', () => { - expect(predicate(equals, deep, { hello: 'world' })).toBe(true); - }); - - test('when given object contains array value', () => { - expect(predicate(equals, deepArray, [{ hello: 'world' }])).toBe(true); - }); - }); - - describe('returns false', () => { - test.each([['world'], [false], [undefined], [null], [''], [0]])( - 'when given object does not contain primitive value: %s', - value => { - expect(predicate(equals, {}, value)).toBe(false); - }, - ); - - test('when given object does not contain object value', () => { - expect(predicate(equals, deep, { foo: 'bar' })).toBe(false); - }); - - test('when given object does not contain array value', () => { - expect(predicate(equals, deepArray, [{ bar: 'foo' }])).toBe(false); - }); - }); -}); diff --git a/src/matchers/toContainValues.js b/src/matchers/toContainValues.js new file mode 100644 index 00000000..4df03919 --- /dev/null +++ b/src/matchers/toContainValues.js @@ -0,0 +1,26 @@ +import { contains } from '../utils'; + +export function toContainValues(actual, expected) { + const { printReceived, printExpected, matcherHint } = this.utils; + + const passMessage = + matcherHint('.not.toContainValues') + + '\n\n' + + 'Expected object to not contain all values:\n' + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const failMessage = + matcherHint('.toContainValues') + + '\n\n' + + 'Expected object to contain all values:\n' + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const values = Object.keys(actual).map(k => actual[k]); + const pass = expected.every(value => contains(this.equals, values, value)); + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toContainValues/index.js b/src/matchers/toContainValues/index.js deleted file mode 100644 index 54a8a079..00000000 --- a/src/matchers/toContainValues/index.js +++ /dev/null @@ -1,26 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, actual, expected) => () => - utils.matcherHint('.not.toContainValues') + - '\n\n' + - 'Expected object to not contain all values:\n' + - ` ${utils.printExpected(expected)}\n` + - 'Received:\n' + - ` ${utils.printReceived(actual)}`; - -const failMessage = (utils, actual, expected) => () => - utils.matcherHint('.toContainValues') + - '\n\n' + - 'Expected object to contain all values:\n' + - ` ${utils.printExpected(expected)}\n` + - 'Received:\n' + - ` ${utils.printReceived(actual)}`; - -export function toContainValues(actual, expected) { - const pass = predicate(this.equals, actual, expected); - if (pass) { - return { pass: true, message: passMessage(this.utils, actual, expected) }; - } - - return { pass: false, message: failMessage(this.utils, actual, expected) }; -} diff --git a/src/matchers/toContainValues/predicate.js b/src/matchers/toContainValues/predicate.js deleted file mode 100644 index b159db68..00000000 --- a/src/matchers/toContainValues/predicate.js +++ /dev/null @@ -1,6 +0,0 @@ -import { contains } from '../../utils'; - -export default (equals, object, values) => { - const objectValues = Object.keys(object).map(k => object[k]); - return values.every(value => contains(equals, objectValues, value)); -}; diff --git a/src/matchers/toContainValues/predicate.test.js b/src/matchers/toContainValues/predicate.test.js deleted file mode 100644 index d7dc39a7..00000000 --- a/src/matchers/toContainValues/predicate.test.js +++ /dev/null @@ -1,48 +0,0 @@ -import { equals } from 'expect/build/jasmineUtils'; -import predicate from './predicate'; - -describe('toContainValue Predicate', () => { - const shallow = { - hello: 'world', - foo: 0, - bar: false, - }; - const deep = { - message: shallow, - donald: 'duck', - }; - const deepArray = { - message: [shallow], - donald: 'duck', - }; - - describe('returns true', () => { - test('when given object contains all primitive values', () => { - expect(predicate(equals, shallow, ['world', false, 0])).toBe(true); - }); - - test('when given object contains all values including objects', () => { - expect(predicate(equals, deep, ['duck', { hello: 'world', foo: 0, bar: false }])).toBe(true); - }); - - test('when given object contains all values including arrays', () => { - expect(predicate(equals, deepArray, ['duck', [{ hello: 'world', foo: 0, bar: false }]])).toBe(true); - }); - }); - - describe('returns false', () => { - test('when given object does not contain all primitive value', () => { - expect(predicate(equals, shallow, [false, undefined])).toBe(false); - }); - - test('when given object does not contain all values including objects', () => { - expect(predicate(equals, deep, ['made up value', 'duck', { hello: 'world', foo: 0, bar: false }])).toBe(false); - }); - - test('when given object does not contain all values including arrays', () => { - expect(predicate(equals, deepArray, ['duck', 'made up value', [{ hello: 'world', foo: 0, bar: false }]])).toBe( - false, - ); - }); - }); -}); diff --git a/src/matchers/toEndWith.js b/src/matchers/toEndWith.js new file mode 100644 index 00000000..9678a75d --- /dev/null +++ b/src/matchers/toEndWith.js @@ -0,0 +1,23 @@ +export function toEndWith(actual, expected) { + const { printReceived, printExpected, matcherHint } = this.utils; + + const passMessage = + matcherHint('.not.toEndWith') + + '\n\n' + + 'Expected string to not end with:\n' + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const failMessage = + matcherHint('.toEndWith') + + '\n\n' + + 'Expected string to end with:\n' + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const pass = actual.endsWith(expected); + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toEndWith/index.js b/src/matchers/toEndWith/index.js deleted file mode 100644 index af836387..00000000 --- a/src/matchers/toEndWith/index.js +++ /dev/null @@ -1,26 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, suffix, string) => () => - utils.matcherHint('.not.toEndWith') + - '\n\n' + - 'Expected string to not end with:\n' + - ` ${utils.printExpected(suffix)}\n` + - 'Received:\n' + - ` ${utils.printReceived(string)}`; - -const failMessage = (utils, suffix, string) => () => - utils.matcherHint('.toEndWith') + - '\n\n' + - 'Expected string to end with:\n' + - ` ${utils.printExpected(suffix)}\n` + - 'Received:\n' + - ` ${utils.printReceived(string)}`; - -export function toEndWith(string, suffix) { - const pass = predicate(suffix, string); - if (pass) { - return { pass: true, message: passMessage(this.utils, suffix, string) }; - } - - return { pass: false, message: failMessage(this.utils, suffix, string) }; -} diff --git a/src/matchers/toEndWith/predicate.js b/src/matchers/toEndWith/predicate.js deleted file mode 100644 index ad4b96c1..00000000 --- a/src/matchers/toEndWith/predicate.js +++ /dev/null @@ -1 +0,0 @@ -export default (suffix, string) => string.endsWith(suffix); diff --git a/src/matchers/toEndWith/predicate.test.js b/src/matchers/toEndWith/predicate.test.js deleted file mode 100644 index 1fc62097..00000000 --- a/src/matchers/toEndWith/predicate.test.js +++ /dev/null @@ -1,11 +0,0 @@ -import predicate from './predicate'; - -describe('toEndWith Predicate', () => { - test('returns true when string ends with given suffix', () => { - expect(predicate('world', 'hello world')).toBe(true); - }); - - test('returns false when string does not end with given suffix', () => { - expect(predicate('hello', 'hello world')).toBe(false); - }); -}); diff --git a/src/matchers/toEqualCaseInsensitive.js b/src/matchers/toEqualCaseInsensitive.js new file mode 100644 index 00000000..13653f69 --- /dev/null +++ b/src/matchers/toEqualCaseInsensitive.js @@ -0,0 +1,23 @@ +export function toEqualCaseInsensitive(actual, expected) { + const { printReceived, printExpected, matcherHint } = this.utils; + + const passMessage = + matcherHint('.not.toEqualCaseInsensitive') + + '\n\n' + + 'Expected values to not be equal while ignoring case (using ===):\n' + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const failMessage = + matcherHint('.toEqualCaseInsensitive') + + '\n\n' + + 'Expected values to be equal while ignoring case (using ===):\n' + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const pass = String(actual).toLowerCase() === String(expected).toLowerCase(); + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toEqualCaseInsensitive/index.js b/src/matchers/toEqualCaseInsensitive/index.js deleted file mode 100644 index b96cbe23..00000000 --- a/src/matchers/toEqualCaseInsensitive/index.js +++ /dev/null @@ -1,33 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, received, expected) => () => { - return ( - utils.matcherHint('.not.toEqualCaseInsensitive') + - '\n\n' + - 'Expected values to not be equal while ignoring case (using ===):\n' + - ` ${utils.printExpected(expected)}\n` + - 'Received:\n' + - ` ${utils.printReceived(received)}` - ); -}; - -const failMessage = (utils, received, expected) => () => { - return ( - utils.matcherHint('.toEqualCaseInsensitive') + - '\n\n' + - 'Expected values to be equal while ignoring case (using ===):\n' + - ` ${utils.printExpected(expected)}\n` + - 'Received:\n' + - ` ${utils.printReceived(received)}` - ); -}; - -export function toEqualCaseInsensitive(received, expected) { - const pass = predicate(received, expected); - - return { - pass, - message: pass ? passMessage(this.utils, received, expected) : failMessage(this.utils, received, expected), - actual: received, - }; -} diff --git a/src/matchers/toEqualCaseInsensitive/predicate.js b/src/matchers/toEqualCaseInsensitive/predicate.js deleted file mode 100644 index 9f1d6a8e..00000000 --- a/src/matchers/toEqualCaseInsensitive/predicate.js +++ /dev/null @@ -1 +0,0 @@ -export default (a, b) => String(a).toLowerCase() === String(b).toLowerCase(); diff --git a/src/matchers/toEqualCaseInsensitive/predicate.test.js b/src/matchers/toEqualCaseInsensitive/predicate.test.js deleted file mode 100644 index 37113cfb..00000000 --- a/src/matchers/toEqualCaseInsensitive/predicate.test.js +++ /dev/null @@ -1,13 +0,0 @@ -import predicate from './predicate'; - -describe('toEqualCaseInsensitive Predicate', () => { - test('returns true given equal strings regardless of case', () => { - expect(predicate('hello WORLD', 'hello world')).toEqual(true); - expect(predicate('HeLLo WORLD', 'hello world')).toEqual(true); - expect(predicate('HELLO WORLD', 'hello world')).toEqual(true); - }); - - test('returns false when given two different strings', () => { - expect(predicate('hello', 'world')).toEqual(false); - }); -}); diff --git a/src/matchers/toEqualIgnoringWhitespace.js b/src/matchers/toEqualIgnoringWhitespace.js new file mode 100644 index 00000000..15479066 --- /dev/null +++ b/src/matchers/toEqualIgnoringWhitespace.js @@ -0,0 +1,43 @@ +import { diffStringsRaw, DIFF_EQUAL } from 'jest-diff'; +import { printExpected, printReceived } from '../utils/print'; + +const removeWhitespace = str => str.trim().replace(/\s+/g, ''); + +const predicate = (received, expected) => { + /* calculate diff of received w.r.t expected string */ + const diff = diffStringsRaw(expected, received); + + /* mark every diff result object with value of white-space as DIFF_EQUAL */ + diff.forEach(diffObject => { + if (diffObject[1].trim()) return; + diffObject[0] = DIFF_EQUAL; + }); + + /* determine whether strings are equal after removing white-space */ + const pass = removeWhitespace(received) === removeWhitespace(expected); + + return { + diff, + pass, + }; +}; + +export function toEqualIgnoringWhitespace(actual, expected) { + const { matcherHint, EXPECTED_COLOR } = this.utils; + const { pass, diff } = predicate(actual, expected); + + const passMessage = + matcherHint('.not.toEqualIgnoringWhitespace') + + '\n\n' + + 'Expected values to not be equal while ignoring white-space (using ===):\n' + + `Expected: not ${EXPECTED_COLOR(expected)}\n\n`; + + const failMessage = + matcherHint('.toEqualIgnoringWhitespace') + + '\n\n' + + 'Expected values to be equal while ignoring white-space (using ===):\n' + + `Expected:\n ${printExpected(this.utils, diff)}\n\n` + + `Received:\n ${printReceived(this.utils, diff)}`; + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toEqualIgnoringWhitespace/index.js b/src/matchers/toEqualIgnoringWhitespace/index.js deleted file mode 100644 index ba4ffa30..00000000 --- a/src/matchers/toEqualIgnoringWhitespace/index.js +++ /dev/null @@ -1,25 +0,0 @@ -import predicate from './predicate'; -import { printExpected, printReceived } from './print-util'; - -const passMessage = (utils, expected) => () => - utils.matcherHint('.not.toEqualIgnoringWhitespace') + - '\n\n' + - 'Expected values to not be equal while ignoring white-space (using ===):\n' + - `Expected: not ${utils.EXPECTED_COLOR(expected)}\n\n`; - -const failMessage = (utils, diff) => () => - utils.matcherHint('.toEqualIgnoringWhitespace') + - '\n\n' + - 'Expected values to be equal while ignoring white-space (using ===):\n' + - `Expected:\n ${printExpected(utils, diff)}\n\n` + - `Received:\n ${printReceived(utils, diff)}`; - -export function toEqualIgnoringWhitespace(received, expected) { - const { pass, diff } = predicate(received, expected); - - return { - pass: pass, - message: pass ? passMessage(this.utils, expected) : failMessage(this.utils, diff), - actual: received, - }; -} diff --git a/src/matchers/toEqualIgnoringWhitespace/predicate.js b/src/matchers/toEqualIgnoringWhitespace/predicate.js deleted file mode 100644 index f7499870..00000000 --- a/src/matchers/toEqualIgnoringWhitespace/predicate.js +++ /dev/null @@ -1,22 +0,0 @@ -import { diffStringsRaw, DIFF_EQUAL } from 'jest-diff'; - -const removeWhitespace = str => str.trim().replace(/\s+/g, ''); - -export default (received, expected) => { - /* calculate diff of received w.r.t expected string */ - const diff = diffStringsRaw(expected, received); - - /* mark every diff result object with value of white-space as DIFF_EQUAL */ - diff.forEach(diffObject => { - if (diffObject[1].trim()) return; - diffObject[0] = DIFF_EQUAL; - }); - - /* determine whether strings are equal after removing white-space */ - const pass = removeWhitespace(received) === removeWhitespace(expected); - - return { - diff, - pass, - }; -}; diff --git a/src/matchers/toEqualIgnoringWhitespace/predicate.test.js b/src/matchers/toEqualIgnoringWhitespace/predicate.test.js deleted file mode 100644 index 897782ce..00000000 --- a/src/matchers/toEqualIgnoringWhitespace/predicate.test.js +++ /dev/null @@ -1,55 +0,0 @@ -import predicate from './predicate'; - -describe('toEqualIgnoringWhitespace predicate', () => { - it('should generate correct pass and diff for inputs without white-space', () => { - const { pass, diff } = predicate('inputs A', 'inputs B'); - - expect(pass).toEqual(false); - expect(diff).toEqual([ - { - 0: 0, - 1: 'inputs ', - }, - { - 0: -1, - 1: 'B', - }, - { - 0: 1, - 1: 'A', - }, - ]); - }); - - it('should generate correct pass and diff for inputs with white-space', () => { - const { pass, diff } = predicate(' inputs A', 'input B '); - - expect(pass).toEqual(false); - expect(diff).toEqual([ - { - 0: 0, - 1: ' ', - }, - { - 0: 0, - 1: 'input', - }, - { - 0: 1, - 1: 's', - }, - { - 0: 0, - 1: ' ', - }, - { - 0: -1, - 1: 'B ', - }, - { - 0: 1, - 1: 'A', - }, - ]); - }); -}); diff --git a/src/matchers/toHaveBeenCalledAfter.js b/src/matchers/toHaveBeenCalledAfter.js new file mode 100644 index 00000000..39103089 --- /dev/null +++ b/src/matchers/toHaveBeenCalledAfter.js @@ -0,0 +1,60 @@ +import { isJestMockOrSpy } from '../utils'; + +export function toHaveBeenCalledAfter(actual, expected) { + const { printReceived, printExpected, matcherHint } = this.utils; + + if (!isJestMockOrSpy(actual)) { + return { pass: false, message: mockCheckFailMessage(this.utils, actual, true) }; + } + + if (!isJestMockOrSpy(expected)) { + return { pass: false, message: mockCheckFailMessage(this.utils, expected, false) }; + } + + const firstInvocationCallOrder = actual.mock.invocationCallOrder; + const secondInvocationCallOrder = expected.mock.invocationCallOrder; + const pass = predicate(firstInvocationCallOrder, secondInvocationCallOrder); + + const passMessage = + matcherHint('.not.toHaveBeenCalledAfter') + + '\n\n' + + 'Expected first mock to not have been called after, invocationCallOrder:\n' + + ` ${printExpected(firstInvocationCallOrder)}\n` + + 'Received second mock with invocationCallOrder:\n' + + ` ${printReceived(secondInvocationCallOrder)}`; + + const failMessage = + matcherHint('.toHaveBeenCalledAfter') + + '\n\n' + + 'Expected first mock to have been called after, invocationCallOrder:\n' + + ` ${printExpected(firstInvocationCallOrder)}\n` + + 'Received second mock with invocationCallOrder:\n' + + ` ${printReceived(secondInvocationCallOrder)}`; + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} + +const smallest = ns => ns.reduce((acc, n) => (acc < n ? acc : n)); + +const predicate = (firstInvocationCallOrder, secondInvocationCallOrder) => { + if (firstInvocationCallOrder.length === 0) return true; + if (secondInvocationCallOrder.length === 0) return false; + + const firstSmallest = smallest(firstInvocationCallOrder); + const secondSmallest = smallest(secondInvocationCallOrder); + + return firstSmallest > secondSmallest; +}; + +const mockCheckFailMessage = (utils, value, isReceivedValue) => () => { + const valueKind = isReceivedValue ? 'Received' : 'Expected'; + const valueKindPrintFunc = isReceivedValue ? utils.printReceived : utils.printExpected; + + return ( + utils.matcherHint('.toHaveBeenCalledAfter') + + '\n\n' + + `Matcher error: ${valueKindPrintFunc(valueKind.toLowerCase())} must be a mock or spy function` + + '\n\n' + + utils.printWithType(valueKind, value, valueKindPrintFunc) + ); +}; diff --git a/src/matchers/toHaveBeenCalledAfter/index.js b/src/matchers/toHaveBeenCalledAfter/index.js deleted file mode 100644 index 5b16c6c6..00000000 --- a/src/matchers/toHaveBeenCalledAfter/index.js +++ /dev/null @@ -1,51 +0,0 @@ -import { isJestMockOrSpy } from '../../utils'; - -import predicate from './predicate'; - -const passMessage = (utils, firstInvocationCallOrder, secondInvocationCallOrder) => () => - utils.matcherHint('.not.toHaveBeenCalledAfter') + - '\n\n' + - 'Expected first mock to not have been called after, invocationCallOrder:\n' + - ` ${utils.printExpected(firstInvocationCallOrder)}\n` + - 'Received second mock with invocationCallOrder:\n' + - ` ${utils.printReceived(secondInvocationCallOrder)}`; - -const failMessage = (utils, firstInvocationCallOrder, secondInvocationCallOrder) => () => - utils.matcherHint('.toHaveBeenCalledAfter') + - '\n\n' + - 'Expected first mock to have been called after, invocationCallOrder:\n' + - ` ${utils.printExpected(firstInvocationCallOrder)}\n` + - 'Received second mock with invocationCallOrder:\n' + - ` ${utils.printReceived(secondInvocationCallOrder)}`; - -const mockCheckFailMessage = (utils, value, isReceivedValue) => () => { - const valueKind = isReceivedValue ? 'Received' : 'Expected'; - const valueKindPrintFunc = isReceivedValue ? utils.printReceived : utils.printExpected; - - return ( - utils.matcherHint('.toHaveBeenCalledAfter') + - '\n\n' + - `Matcher error: ${valueKindPrintFunc(valueKind.toLowerCase())} must be a mock or spy function` + - '\n\n' + - utils.printWithType(valueKind, value, valueKindPrintFunc) - ); -}; - -export function toHaveBeenCalledAfter(firstMock, secondMock) { - if (!isJestMockOrSpy(firstMock)) { - return { pass: false, message: mockCheckFailMessage(this.utils, firstMock, true) }; - } - - if (!isJestMockOrSpy(secondMock)) { - return { pass: false, message: mockCheckFailMessage(this.utils, secondMock, false) }; - } - - const firstInvocationCallOrder = firstMock.mock.invocationCallOrder; - const secondInvocationCallOrder = secondMock.mock.invocationCallOrder; - const pass = predicate(firstInvocationCallOrder, secondInvocationCallOrder); - if (pass) { - return { pass: true, message: passMessage(this.utils, firstInvocationCallOrder, secondInvocationCallOrder) }; - } - - return { pass: false, message: failMessage(this.utils, firstInvocationCallOrder, secondInvocationCallOrder) }; -} diff --git a/src/matchers/toHaveBeenCalledAfter/predicate.js b/src/matchers/toHaveBeenCalledAfter/predicate.js deleted file mode 100644 index 8939f381..00000000 --- a/src/matchers/toHaveBeenCalledAfter/predicate.js +++ /dev/null @@ -1,11 +0,0 @@ -const smallest = ns => ns.reduce((acc, n) => (acc < n ? acc : n)); - -export default (firstInvocationCallOrder, secondInvocationCallOrder) => { - if (firstInvocationCallOrder.length === 0) return true; - if (secondInvocationCallOrder.length === 0) return false; - - const firstSmallest = smallest(firstInvocationCallOrder); - const secondSmallest = smallest(secondInvocationCallOrder); - - return firstSmallest > secondSmallest; -}; diff --git a/src/matchers/toHaveBeenCalledAfter/predicate.test.js b/src/matchers/toHaveBeenCalledAfter/predicate.test.js deleted file mode 100644 index 41ed37fa..00000000 --- a/src/matchers/toHaveBeenCalledAfter/predicate.test.js +++ /dev/null @@ -1,35 +0,0 @@ -import predicate from './predicate'; - -describe('.toHaveBeenCalledAfter predicate', () => { - const now = Date.now(); - const lessThan = now - 100; - const greaterThan = now + 100; - - test('returns true when given an empty array as the first time stamps', () => { - expect(predicate([])).toBe(true); - }); - - test('returns true when given first timestamps is an empty array and given second time stamps', () => { - expect(predicate([], [now])).toBe(true); - }); - - test('returns false when given first timestamps and an empty array as the second time stamps', () => { - expect(predicate([now], [])).toBe(false); - }); - - test('returns false when given first timestamp is less than and second time stamp', () => { - expect(predicate([now], [greaterThan])).toBe(false); - }); - - test('returns true when given first timestamp is greater than and second time stamp', () => { - expect(predicate([now], [lessThan])).toBe(true); - }); - - test('returns false when given first timestamps contains a timestamp less than any of the second timestamps', () => { - expect(predicate([now, lessThan, greaterThan], [now, greaterThan])).toBe(false); - }); - - test('returns true when given first timestamps does not contain a timestamp less than any of the second timestamps', () => { - expect(predicate([greaterThan, now, greaterThan], [lessThan])).toBe(true); - }); -}); diff --git a/src/matchers/toHaveBeenCalledBefore.js b/src/matchers/toHaveBeenCalledBefore.js new file mode 100644 index 00000000..ee5fa2bf --- /dev/null +++ b/src/matchers/toHaveBeenCalledBefore.js @@ -0,0 +1,60 @@ +import { isJestMockOrSpy } from '../utils'; + +export function toHaveBeenCalledBefore(actual, expected) { + const { printReceived, printExpected, matcherHint } = this.utils; + + if (!isJestMockOrSpy(actual)) { + return { pass: false, message: mockCheckFailMessage(this.utils, actual, true) }; + } + + if (!isJestMockOrSpy(expected)) { + return { pass: false, message: mockCheckFailMessage(this.utils, expected, false) }; + } + + const firstInvocationCallOrder = actual.mock.invocationCallOrder; + const secondInvocationCallOrder = expected.mock.invocationCallOrder; + const pass = predicate(firstInvocationCallOrder, secondInvocationCallOrder); + + const passMessage = + matcherHint('.not.toHaveBeenCalledBefore') + + '\n\n' + + 'Expected first mock to not have been called before, invocationCallOrder:\n' + + ` ${printExpected(firstInvocationCallOrder)}\n` + + 'Received second mock with invocationCallOrder:\n' + + ` ${printReceived(secondInvocationCallOrder)}`; + + const failMessage = + matcherHint('.toHaveBeenCalledBefore') + + '\n\n' + + 'Expected first mock to have been called before, invocationCallOrder:\n' + + ` ${printExpected(firstInvocationCallOrder)}\n` + + 'Received second mock with invocationCallOrder:\n' + + ` ${printReceived(secondInvocationCallOrder)}`; + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} + +const mockCheckFailMessage = (utils, value, isReceivedValue) => () => { + const valueKind = isReceivedValue ? 'Received' : 'Expected'; + const valueKindPrintFunc = isReceivedValue ? utils.printReceived : utils.printExpected; + + return ( + utils.matcherHint('.toHaveBeenCalledAfter') + + '\n\n' + + `Matcher error: ${valueKindPrintFunc(valueKind.toLowerCase())} must be a mock or spy function` + + '\n\n' + + utils.printWithType(valueKind, value, valueKindPrintFunc) + ); +}; + +const smallest = ns => ns.reduce((acc, n) => (acc < n ? acc : n)); + +const predicate = (firstInvocationCallOrder, secondInvocationCallOrder) => { + if (firstInvocationCallOrder.length === 0) return false; + if (secondInvocationCallOrder.length === 0) return true; + + const firstSmallest = smallest(firstInvocationCallOrder); + const secondSmallest = smallest(secondInvocationCallOrder); + + return firstSmallest < secondSmallest; +}; diff --git a/src/matchers/toHaveBeenCalledBefore/index.js b/src/matchers/toHaveBeenCalledBefore/index.js deleted file mode 100644 index ad262b5b..00000000 --- a/src/matchers/toHaveBeenCalledBefore/index.js +++ /dev/null @@ -1,51 +0,0 @@ -import { isJestMockOrSpy } from '../../utils'; - -import predicate from './predicate'; - -const passMessage = (utils, firstInvocationCallOrder, secondInvocationCallOrder) => () => - utils.matcherHint('.not.toHaveBeenCalledBefore') + - '\n\n' + - 'Expected first mock to not have been called before, invocationCallOrder:\n' + - ` ${utils.printExpected(firstInvocationCallOrder)}\n` + - 'Received second mock with invocationCallOrder:\n' + - ` ${utils.printReceived(secondInvocationCallOrder)}`; - -const failMessage = (utils, firstInvocationCallOrder, secondInvocationCallOrder) => () => - utils.matcherHint('.toHaveBeenCalledBefore') + - '\n\n' + - 'Expected first mock to have been called before, invocationCallOrder:\n' + - ` ${utils.printExpected(firstInvocationCallOrder)}\n` + - 'Received second mock with invocationCallOrder:\n' + - ` ${utils.printReceived(secondInvocationCallOrder)}`; - -const mockCheckFailMessage = (utils, value, isReceivedValue) => () => { - const valueKind = isReceivedValue ? 'Received' : 'Expected'; - const valueKindPrintFunc = isReceivedValue ? utils.printReceived : utils.printExpected; - - return ( - utils.matcherHint('.toHaveBeenCalledAfter') + - '\n\n' + - `Matcher error: ${valueKindPrintFunc(valueKind.toLowerCase())} must be a mock or spy function` + - '\n\n' + - utils.printWithType(valueKind, value, valueKindPrintFunc) - ); -}; - -export function toHaveBeenCalledBefore(firstMock, secondMock) { - if (!isJestMockOrSpy(firstMock)) { - return { pass: false, message: mockCheckFailMessage(this.utils, firstMock, true) }; - } - - if (!isJestMockOrSpy(secondMock)) { - return { pass: false, message: mockCheckFailMessage(this.utils, secondMock, false) }; - } - - const firstInvocationCallOrder = firstMock.mock.invocationCallOrder; - const secondInvocationCallOrder = secondMock.mock.invocationCallOrder; - const pass = predicate(firstInvocationCallOrder, secondInvocationCallOrder); - if (pass) { - return { pass: true, message: passMessage(this.utils, firstInvocationCallOrder, secondInvocationCallOrder) }; - } - - return { pass: false, message: failMessage(this.utils, firstInvocationCallOrder, secondInvocationCallOrder) }; -} diff --git a/src/matchers/toHaveBeenCalledBefore/predicate.js b/src/matchers/toHaveBeenCalledBefore/predicate.js deleted file mode 100644 index f37be4de..00000000 --- a/src/matchers/toHaveBeenCalledBefore/predicate.js +++ /dev/null @@ -1,11 +0,0 @@ -const smallest = ns => ns.reduce((acc, n) => (acc < n ? acc : n)); - -export default (firstInvocationCallOrder, secondInvocationCallOrder) => { - if (firstInvocationCallOrder.length === 0) return false; - if (secondInvocationCallOrder.length === 0) return true; - - const firstSmallest = smallest(firstInvocationCallOrder); - const secondSmallest = smallest(secondInvocationCallOrder); - - return firstSmallest < secondSmallest; -}; diff --git a/src/matchers/toHaveBeenCalledBefore/predicate.test.js b/src/matchers/toHaveBeenCalledBefore/predicate.test.js deleted file mode 100644 index d741c5f6..00000000 --- a/src/matchers/toHaveBeenCalledBefore/predicate.test.js +++ /dev/null @@ -1,31 +0,0 @@ -import predicate from './predicate'; - -describe('.toHaveBeenCalledBefore predicate', () => { - const now = Date.now(); - const lessThan = now - 100; - const greaterThan = now + 100; - - test('returns false when given an empty array as the first time stamps', () => { - expect(predicate([])).toBe(false); - }); - - test('returns true when given first timestamps and an empty array as the second time stamps', () => { - expect(predicate([now], [])).toBe(true); - }); - - test('returns true when given first timestamp is less than and second time stamp', () => { - expect(predicate([now], [greaterThan])).toBe(true); - }); - - test('returns false when given first timestamp is greater than and second time stamp', () => { - expect(predicate([now], [lessThan])).toBe(false); - }); - - test('returns true when given first timestamps contains a timestamp less than any of the second timestamps', () => { - expect(predicate([now, lessThan, greaterThan], [now, greaterThan])).toBe(true); - }); - - test('returns false when given first timestamps does not contain a timestamp less than any of the second timestamps', () => { - expect(predicate([greaterThan, now, greaterThan], [lessThan])).toBe(false); - }); -}); diff --git a/src/matchers/toHaveBeenCalledOnce.js b/src/matchers/toHaveBeenCalledOnce.js new file mode 100644 index 00000000..7d7f75c3 --- /dev/null +++ b/src/matchers/toHaveBeenCalledOnce.js @@ -0,0 +1,32 @@ +import { isJestMockOrSpy } from '../utils'; + +export function toHaveBeenCalledOnce(received) { + const { printReceived, printWithType, matcherHint } = this.utils; + + if (!isJestMockOrSpy(received)) { + return { + pass: false, + message: () => + matcherHint('.toHaveBeenCalledAfter') + + '\n\n' + + `Matcher error: ${printReceived('received')} must be a mock or spy function` + + '\n\n' + + printWithType('Received', received, printReceived), + }; + } + + const passMessage = + matcherHint('.not.toHaveBeenCalledOnce') + + '\n\n' + + 'Expected mock function to have been called any amount of times but one, but it was called exactly once.'; + + const failMessage = + matcherHint('.toHaveBeenCalledOnce') + + '\n\n' + + 'Expected mock function to have been called exactly once, but it was called:\n' + + ` ${printReceived(received.mock.calls.length)} times`; + + const pass = received.mock.calls.length === 1; + + return { pass, message: () => (pass ? passMessage : failMessage), actual: received }; +} diff --git a/src/matchers/toHaveBeenCalledOnce/index.js b/src/matchers/toHaveBeenCalledOnce/index.js deleted file mode 100644 index de2b9a08..00000000 --- a/src/matchers/toHaveBeenCalledOnce/index.js +++ /dev/null @@ -1,41 +0,0 @@ -import { isJestMockOrSpy } from '../../utils'; - -import predicate from './predicate'; - -const passMessage = utils => () => - utils.matcherHint('.not.toHaveBeenCalledOnce') + - '\n\n' + - 'Expected mock function to have been called any amount of times but one, but it was called exactly once.'; - -const failMessage = (utils, mockFn) => () => { - return ( - utils.matcherHint('.toHaveBeenCalledOnce') + - '\n\n' + - 'Expected mock function to have been called exactly once, but it was called:\n' + - ` ${utils.printReceived(mockFn.mock.calls.length)} times` - ); -}; - -const mockCheckFailMessage = (utils, value) => () => { - return ( - utils.matcherHint('.toHaveBeenCalledAfter') + - '\n\n' + - `Matcher error: ${utils.printReceived('received')} must be a mock or spy function` + - '\n\n' + - utils.printWithType('Received', value, utils.printReceived) - ); -}; - -export function toHaveBeenCalledOnce(received) { - if (!isJestMockOrSpy(received)) { - return { pass: false, message: mockCheckFailMessage(this.utils, received) }; - } - - const pass = predicate(received); - - return { - pass, - message: pass ? passMessage(this.utils, received) : failMessage(this.utils, received), - actual: received, - }; -} diff --git a/src/matchers/toHaveBeenCalledOnce/predicate.js b/src/matchers/toHaveBeenCalledOnce/predicate.js deleted file mode 100644 index df139ad0..00000000 --- a/src/matchers/toHaveBeenCalledOnce/predicate.js +++ /dev/null @@ -1 +0,0 @@ -export default mockFn => mockFn.mock.calls.length === 1; diff --git a/src/matchers/toHaveBeenCalledOnce/predicate.test.js b/src/matchers/toHaveBeenCalledOnce/predicate.test.js deleted file mode 100644 index ba5d9e5d..00000000 --- a/src/matchers/toHaveBeenCalledOnce/predicate.test.js +++ /dev/null @@ -1,25 +0,0 @@ -import predicate from './predicate'; - -describe('.toHaveBeenCalledOnce predicate', () => { - let mock; - beforeEach(() => { - // Refresh on each test - mock = jest.fn(); - }); - - test('returns true if mock was invoked exactly once', () => { - mock(); - expect(predicate(mock)).toBe(true); - }); - - test('returns true if mock was invoked any amount of times but one', () => { - expect(predicate(mock)).toBe(false); - - mock(); - mock(); - expect(predicate(mock)).toBe(false); - - new Array(20).fill(mock).forEach(e => e()); - expect(predicate(mock)).toBe(false); - }); -}); diff --git a/src/matchers/toInclude.js b/src/matchers/toInclude.js new file mode 100644 index 00000000..b511675c --- /dev/null +++ b/src/matchers/toInclude.js @@ -0,0 +1,23 @@ +export function toInclude(actual, expected) { + const { printReceived, printExpected, matcherHint } = this.utils; + + const passMessage = + matcherHint('.not.toInclude') + + '\n\n' + + 'Expected string to not include:\n' + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const failMessage = + matcherHint('.toInclude') + + '\n\n' + + 'Expected string to include:\n' + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const pass = actual.includes(expected); + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toInclude/index.js b/src/matchers/toInclude/index.js deleted file mode 100644 index bc4f221d..00000000 --- a/src/matchers/toInclude/index.js +++ /dev/null @@ -1,26 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, actual, expected) => () => - utils.matcherHint('.not.toInclude') + - '\n\n' + - 'Expected string to not include:\n' + - ` ${utils.printExpected(expected)}\n` + - 'Received:\n' + - ` ${utils.printReceived(actual)}`; - -const failMessage = (utils, actual, expected) => () => - utils.matcherHint('.toInclude') + - '\n\n' + - 'Expected string to include:\n' + - ` ${utils.printExpected(expected)}\n` + - 'Received:\n' + - ` ${utils.printReceived(actual)}`; - -export function toInclude(actual, expected) { - const pass = predicate(actual, expected); - if (pass) { - return { pass: true, message: passMessage(this.utils, actual, expected) }; - } - - return { pass: false, message: failMessage(this.utils, actual, expected) }; -} diff --git a/src/matchers/toInclude/predicate.js b/src/matchers/toInclude/predicate.js deleted file mode 100644 index 62cd1aac..00000000 --- a/src/matchers/toInclude/predicate.js +++ /dev/null @@ -1 +0,0 @@ -export default (string, substring) => string.includes(substring); diff --git a/src/matchers/toInclude/predicate.test.js b/src/matchers/toInclude/predicate.test.js deleted file mode 100644 index f32f708c..00000000 --- a/src/matchers/toInclude/predicate.test.js +++ /dev/null @@ -1,13 +0,0 @@ -import predicate from './predicate'; - -const data = 'hello world'; - -describe('.toInclude', () => { - test('passes when string contains substring', () => { - expect(predicate(data, 'ell')).toBe(true); - }); - - test('fails when string does not contain substring', () => { - expect(predicate(data, 'bob')).toBe(false); - }); -}); diff --git a/src/matchers/toIncludeAllMembers.js b/src/matchers/toIncludeAllMembers.js new file mode 100644 index 00000000..bf53d987 --- /dev/null +++ b/src/matchers/toIncludeAllMembers.js @@ -0,0 +1,26 @@ +import { contains } from '../utils'; + +export function toIncludeAllMembers(actual, expected) { + const { printReceived, printExpected, matcherHint } = this.utils; + + const passMessage = + matcherHint('.not.toIncludeAllMembers') + + '\n\n' + + 'Expected list to not have all of the following members:\n' + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const failMessage = + matcherHint('.toIncludeAllMembers') + + '\n\n' + + 'Expected list to have all of the following members:\n' + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const pass = + Array.isArray(actual) && Array.isArray(expected) && expected.every(val => contains(this.equals, actual, val)); + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toIncludeAllMembers/index.js b/src/matchers/toIncludeAllMembers/index.js deleted file mode 100644 index 301f9d5e..00000000 --- a/src/matchers/toIncludeAllMembers/index.js +++ /dev/null @@ -1,25 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, actual, expected) => () => - utils.matcherHint('.not.toIncludeAllMembers') + - '\n\n' + - 'Expected list to not have all of the following members:\n' + - ` ${utils.printExpected(expected)}\n` + - 'Received:\n' + - ` ${utils.printReceived(actual)}`; - -const failMessage = (utils, actual, expected) => () => - utils.matcherHint('.toIncludeAllMembers') + - '\n\n' + - 'Expected list to have all of the following members:\n' + - ` ${utils.printExpected(expected)}\n` + - 'Received:\n' + - ` ${utils.printReceived(actual)}`; - -export function toIncludeAllMembers(actual, expected) { - const pass = predicate(this.equals, actual, expected); - if (pass) { - return { pass: true, message: passMessage(this.utils, actual, expected) }; - } - return { pass: false, message: failMessage(this.utils, actual, expected) }; -} diff --git a/src/matchers/toIncludeAllMembers/predicate.js b/src/matchers/toIncludeAllMembers/predicate.js deleted file mode 100644 index 76a7a967..00000000 --- a/src/matchers/toIncludeAllMembers/predicate.js +++ /dev/null @@ -1,5 +0,0 @@ -import { contains } from '../../utils'; - -export default (equals, array, set) => { - return Array.isArray(array) && Array.isArray(set) && set.every(val => contains(equals, array, val)); -}; diff --git a/src/matchers/toIncludeAllMembers/predicate.test.js b/src/matchers/toIncludeAllMembers/predicate.test.js deleted file mode 100644 index 48225821..00000000 --- a/src/matchers/toIncludeAllMembers/predicate.test.js +++ /dev/null @@ -1,29 +0,0 @@ -import { equals } from 'expect/build/jasmineUtils'; -import predicate from './predicate'; - -describe('toIncludeAllMembers Predicate', () => { - const array1 = [1, 2, 3]; - const set1 = [2, 1, 3]; - const array2 = [4, 5, 6]; - const set2 = [1, 2, 3]; - - describe('returns true', () => { - test('when Array contains all the same members of given set', () => { - expect(predicate(equals, array1, set1)).toBe(true); - }); - - test('when Array contains all of the same nested members of given set', () => { - expect(predicate(equals, [{ hello: 'world' }, { foo: 'bar' }], [{ foo: 'bar' }])).toBe(true); - }); - }); - - describe('returns false', () => { - test('when Array does not contain any of the members of given set', () => { - expect(predicate(equals, array2, set2)).toBe(false); - }); - - test('when Array contains does not contain all of the same nested members of given set', () => { - expect(predicate(equals, [{ hello: 'world' }, { foo: 'bar' }], [{ foo: 'qux' }])).toBe(false); - }); - }); -}); diff --git a/src/matchers/toIncludeAllPartialMembers.js b/src/matchers/toIncludeAllPartialMembers.js new file mode 100644 index 00000000..0b176134 --- /dev/null +++ b/src/matchers/toIncludeAllPartialMembers.js @@ -0,0 +1,30 @@ +import { containsEntry } from '../utils'; + +export function toIncludeAllPartialMembers(actual, expected) { + const { printReceived, printExpected, matcherHint } = this.utils; + + const passMessage = + matcherHint('.not.toIncludeAllPartialMembers') + + '\n\n' + + 'Expected list to not have all of the following partial members:\n' + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const failMessage = + matcherHint('.toIncludeAllPartialMembers') + + '\n\n' + + 'Expected list to have all of the following partial members:\n' + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const pass = + Array.isArray(actual) && + Array.isArray(expected) && + expected.every(partial => + actual.some(value => Object.entries(partial).every(entry => containsEntry(this.equals, value, entry))), + ); + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toIncludeAllPartialMembers/index.js b/src/matchers/toIncludeAllPartialMembers/index.js deleted file mode 100644 index f988953e..00000000 --- a/src/matchers/toIncludeAllPartialMembers/index.js +++ /dev/null @@ -1,26 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, actual, expected) => () => - utils.matcherHint('.not.toIncludeAllPartialMembers') + - '\n\n' + - 'Expected list to not have all of the following partial members:\n' + - ` ${utils.printExpected(expected)}\n` + - 'Received:\n' + - ` ${utils.printReceived(actual)}`; - -const failMessage = (utils, actual, expected) => () => - utils.matcherHint('.toIncludeAllPartialMembers') + - '\n\n' + - 'Expected list to have all of the following partial members:\n' + - ` ${utils.printExpected(expected)}\n` + - 'Received:\n' + - ` ${utils.printReceived(actual)}`; - -export function toIncludeAllPartialMembers(actual, expected) { - const pass = predicate(this.equals, actual, expected); - if (pass) { - return { pass: true, message: passMessage(this.utils, actual, expected) }; - } - - return { pass: false, message: failMessage(this.utils, actual, expected) }; -} diff --git a/src/matchers/toIncludeAllPartialMembers/predicate.js b/src/matchers/toIncludeAllPartialMembers/predicate.js deleted file mode 100644 index 0d97f350..00000000 --- a/src/matchers/toIncludeAllPartialMembers/predicate.js +++ /dev/null @@ -1,6 +0,0 @@ -import toContainEntries from '../toContainEntries/predicate'; - -export default (equals, array, set) => - Array.isArray(array) && - Array.isArray(set) && - set.every(partial => array.some(value => toContainEntries(equals, value, Object.entries(partial)))); diff --git a/src/matchers/toIncludeAllPartialMembers/predicate.test.js b/src/matchers/toIncludeAllPartialMembers/predicate.test.js deleted file mode 100644 index 27aee6e1..00000000 --- a/src/matchers/toIncludeAllPartialMembers/predicate.test.js +++ /dev/null @@ -1,24 +0,0 @@ -import { equals } from 'expect/build/jasmineUtils'; -import predicate from './predicate'; - -describe('toIncludeAllPartialMembers Predicate', () => { - describe('returns true', () => { - test('when Array contains all of the same nested partial members of given set', () => { - expect(predicate(equals, [{ hello: 'world' }, { foo: 'bar', baz: 'qux' }], [{ foo: 'bar' }])).toBe(true); - }); - }); - - describe('returns false', () => { - test('when Array contains does not contain all of the same nested partial members of given item', () => { - expect(predicate(equals, [{ hello: 'world' }, { foo: 'bar', baz: 'qux' }], [{ foo: 'bar', bax: 'qux' }])).toBe( - false, - ); - }); - - test('when Array contains does not contain all of the same nested partial members of given set', () => { - expect( - predicate(equals, [{ hello: 'world' }, { foo: 'bar', baz: 'qux' }], [{ hello: 'world' }, { foo: 'qux' }]), - ).toBe(false); - }); - }); -}); diff --git a/src/matchers/toIncludeAnyMembers.js b/src/matchers/toIncludeAnyMembers.js new file mode 100644 index 00000000..10eece2f --- /dev/null +++ b/src/matchers/toIncludeAnyMembers.js @@ -0,0 +1,26 @@ +import { contains } from '../utils'; + +export function toIncludeAnyMembers(actual, expected) { + const { printReceived, printExpected, matcherHint } = this.utils; + + const passMessage = + matcherHint('.not.toIncludeAnyMembers') + + '\n\n' + + 'Expected list to not include any of the following members:\n' + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const failMessage = + matcherHint('.toIncludeAnyMembers') + + '\n\n' + + 'Expected list to include any of the following members:\n' + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const pass = + Array.isArray(actual) && Array.isArray(expected) && expected.some(member => contains(this.equals, actual, member)); + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toIncludeAnyMembers/index.js b/src/matchers/toIncludeAnyMembers/index.js deleted file mode 100644 index 8cba068e..00000000 --- a/src/matchers/toIncludeAnyMembers/index.js +++ /dev/null @@ -1,26 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, actual, expected) => () => - utils.matcherHint('.not.toIncludeAnyMembers') + - '\n\n' + - 'Expected list to not include any of the following members:\n' + - ` ${utils.printExpected(expected)}\n` + - 'Received:\n' + - ` ${utils.printReceived(actual)}`; - -const failMessage = (utils, actual, expected) => () => - utils.matcherHint('.toIncludeAnyMembers') + - '\n\n' + - 'Expected list to include any of the following members:\n' + - ` ${utils.printExpected(expected)}\n` + - 'Received:\n' + - ` ${utils.printReceived(actual)}`; - -export function toIncludeAnyMembers(actual, expected) { - const pass = predicate(this.equals, actual, expected); - if (pass) { - return { pass: true, message: passMessage(this.utils, actual, expected) }; - } - - return { pass: false, message: failMessage(this.utils, actual, expected) }; -} diff --git a/src/matchers/toIncludeAnyMembers/predicate.js b/src/matchers/toIncludeAnyMembers/predicate.js deleted file mode 100644 index 77acfc1e..00000000 --- a/src/matchers/toIncludeAnyMembers/predicate.js +++ /dev/null @@ -1,5 +0,0 @@ -import { contains } from '../../utils'; - -export default (equals, array, members) => { - return Array.isArray(array) && Array.isArray(members) && members.some(member => contains(equals, array, member)); -}; diff --git a/src/matchers/toIncludeAnyMembers/predicate.test.js b/src/matchers/toIncludeAnyMembers/predicate.test.js deleted file mode 100644 index ded00975..00000000 --- a/src/matchers/toIncludeAnyMembers/predicate.test.js +++ /dev/null @@ -1,38 +0,0 @@ -import { equals } from 'expect/build/jasmineUtils'; -import predicate from './predicate'; - -describe('toIncludeAnyMembers Predicate', () => { - const array = ['world', false, undefined, null, '', 0]; - const shallow = { hello: 'world' }; - - describe('returns true', () => { - test.each([['world'], [false], [undefined], [null], [''], [0]])( - 'when given array contains primitive value: %s', - given => { - expect(predicate(equals, [given], array)).toBe(true); - }, - ); - - test('when given array contains object value', () => { - expect(predicate(equals, [shallow, 7], [shallow])).toBe(true); - }); - - test('when given object contains array value', () => { - expect(predicate(equals, [[shallow]], [[shallow], 7])).toBe(true); - }); - }); - - describe('returns false', () => { - test('when given array does not contain primitive value', () => { - expect(predicate(equals, [3, 4, 5], [1])).toBe(false); - }); - - test('when given array does not contain object value', () => { - expect(predicate(equals, [3], [{ foo: 'bar' }])).toBe(false); - }); - - test('when given object does not contain array value', () => { - expect(predicate(equals, [7], [[7]])).toBe(false); - }); - }); -}); diff --git a/src/matchers/toIncludeMultiple.js b/src/matchers/toIncludeMultiple.js new file mode 100644 index 00000000..20403718 --- /dev/null +++ b/src/matchers/toIncludeMultiple.js @@ -0,0 +1,23 @@ +export function toIncludeMultiple(actual, expected) { + const { printReceived, printExpected, matcherHint } = this.utils; + + const passMessage = + matcherHint('.not.toIncludeMultiple') + + '\n\n' + + 'Expected string to not contain all substrings: \n' + + ` ${printExpected(expected)}\n` + + 'Received: \n' + + ` ${printReceived(actual)}`; + + const failMessage = + matcherHint('.toIncludeMultiple') + + '\n\n' + + 'Expected string to contain all substrings: \n' + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const pass = expected.every(value => actual.includes(value)); + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toIncludeMultiple/index.js b/src/matchers/toIncludeMultiple/index.js deleted file mode 100644 index d7dc2b22..00000000 --- a/src/matchers/toIncludeMultiple/index.js +++ /dev/null @@ -1,26 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, actual, expected) => () => - utils.matcherHint('.not.toIncludeMultiple') + - '\n\n' + - 'Expected string to not contain all substrings: \n' + - ` ${utils.printExpected(expected)}\n` + - 'Received: \n' + - ` ${utils.printReceived(actual)}`; - -const failMessage = (utils, actual, expected) => () => - utils.matcherHint('.toIncludeMultiple') + - '\n\n' + - 'Expected string to contain all substrings: \n' + - ` ${utils.printExpected(expected)}\n` + - 'Received:\n' + - ` ${utils.printReceived(actual)}`; - -export function toIncludeMultiple(actual, expected) { - const pass = predicate(actual, expected); - if (pass) { - return { pass: true, message: passMessage(this.utils, actual, expected) }; - } - - return { pass: false, message: failMessage(this.utils, actual, expected) }; -} diff --git a/src/matchers/toIncludeMultiple/predicate.js b/src/matchers/toIncludeMultiple/predicate.js deleted file mode 100644 index 036416fc..00000000 --- a/src/matchers/toIncludeMultiple/predicate.js +++ /dev/null @@ -1,3 +0,0 @@ -export default (actual, values) => { - return values.every(value => actual.includes(value)); -}; diff --git a/src/matchers/toIncludeMultiple/predicate.test.js b/src/matchers/toIncludeMultiple/predicate.test.js deleted file mode 100644 index 9d5fee2a..00000000 --- a/src/matchers/toIncludeMultiple/predicate.test.js +++ /dev/null @@ -1,15 +0,0 @@ -import predicate from './predicate'; - -describe('toIncludeMultiple Predicate', () => { - describe('returns true', () => { - test('when string contains all substrings', () => { - expect(predicate('hello world', ['hello', 'world'])).toBe(true); - }); - }); - - describe('returns false', () => { - test('when string does not contain one or more substrings', () => { - expect(predicate('hello world', ['hello', 'world', 'bob'])).toBe(false); - }); - }); -}); diff --git a/src/matchers/toIncludeRepeated.js b/src/matchers/toIncludeRepeated.js new file mode 100644 index 00000000..8ed49e6d --- /dev/null +++ b/src/matchers/toIncludeRepeated.js @@ -0,0 +1,23 @@ +export function toIncludeRepeated(actual, expected, occurrences) { + const { printReceived, printExpected, matcherHint } = this.utils; + + const passMessage = + matcherHint('.not.toIncludeRepeated') + + '\n\n' + + `Expected string to not include repeated ${occurrences} times:\n` + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const failMessage = + matcherHint('.toIncludeRepeated') + + '\n\n' + + `Expected string to include repeated ${occurrences} times:\n` + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const pass = (actual.match(new RegExp(expected, 'g')) || []).length === occurrences; + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toIncludeRepeated/index.js b/src/matchers/toIncludeRepeated/index.js deleted file mode 100644 index 97d144c6..00000000 --- a/src/matchers/toIncludeRepeated/index.js +++ /dev/null @@ -1,26 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, actual, expected, occurrences) => () => - utils.matcherHint('.not.toIncludeRepeated') + - '\n\n' + - `Expected string to not include repeated ${occurrences} times:\n` + - ` ${utils.printExpected(expected)}\n` + - 'Received:\n' + - ` ${utils.printReceived(actual)}`; - -const failMessage = (utils, actual, expected, occurrences) => () => - utils.matcherHint('.toIncludeRepeated') + - '\n\n' + - `Expected string to include repeated ${occurrences} times:\n` + - ` ${utils.printExpected(expected)}\n` + - 'Received:\n' + - ` ${utils.printReceived(actual)}`; - -export function toIncludeRepeated(actual, expected, occurrences) { - const pass = predicate(actual, expected, occurrences); - if (pass) { - return { pass: true, message: passMessage(this.utils, actual, expected, occurrences) }; - } - - return { pass: false, message: failMessage(this.utils, actual, expected, occurrences) }; -} diff --git a/src/matchers/toIncludeRepeated/predicate.js b/src/matchers/toIncludeRepeated/predicate.js deleted file mode 100644 index 2479304e..00000000 --- a/src/matchers/toIncludeRepeated/predicate.js +++ /dev/null @@ -1,2 +0,0 @@ -export default (string, substring, occurrences) => - (string.match(new RegExp(substring, 'g')) || []).length === occurrences; diff --git a/src/matchers/toIncludeRepeated/predicate.test.js b/src/matchers/toIncludeRepeated/predicate.test.js deleted file mode 100644 index 73daeeaa..00000000 --- a/src/matchers/toIncludeRepeated/predicate.test.js +++ /dev/null @@ -1,25 +0,0 @@ -import predicate from './predicate'; - -const string = 'hello world'; -const multi = ` - hello world - hello world -`; - -describe('Predicate > .toIncludeRepeated', () => { - test('passes when string contains substring', () => { - expect(predicate(string, 'l', 3)).toBe(true); - }); - - test('passes when given a multiline string includes given substring hello 2 times', () => { - expect(predicate(multi, 'hello', 2)).toBe(true); - }); - - test('fails when string does not contain substring', () => { - expect(predicate(string, 'bob', 1)).toBe(false); - }); - - test('fails when string does not contain substring for the given occurrences', () => { - expect(predicate(string, 'l', 10)).toBe(false); - }); -}); diff --git a/src/matchers/toIncludeSameMembers.js b/src/matchers/toIncludeSameMembers.js new file mode 100644 index 00000000..db3b6ddc --- /dev/null +++ b/src/matchers/toIncludeSameMembers.js @@ -0,0 +1,43 @@ +export function toIncludeSameMembers(actual, expected) { + const { printReceived, printExpected, matcherHint } = this.utils; + + const passMessage = + matcherHint('.not.toIncludeSameMembers') + + '\n\n' + + 'Expected list to not exactly match the members of:\n' + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const failMessage = + matcherHint('.toIncludeSameMembers') + + '\n\n' + + 'Expected list to have the following members and no more:\n' + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const pass = predicate(this.equals, actual, expected); + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} + +const predicate = (equals, actual, expected) => { + if (!Array.isArray(actual) || !Array.isArray(expected) || actual.length !== expected.length) { + return false; + } + + const remaining = expected.reduce((remaining, secondValue) => { + if (remaining === null) return remaining; + + const index = remaining.findIndex(firstValue => equals(secondValue, firstValue)); + + if (index === -1) { + return null; + } + + return remaining.slice(0, index).concat(remaining.slice(index + 1)); + }, actual); + + return !!remaining && remaining.length === 0; +}; diff --git a/src/matchers/toIncludeSameMembers/index.js b/src/matchers/toIncludeSameMembers/index.js deleted file mode 100644 index 29521d03..00000000 --- a/src/matchers/toIncludeSameMembers/index.js +++ /dev/null @@ -1,25 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, actual, expected) => () => - utils.matcherHint('.not.toIncludeSameMembers') + - '\n\n' + - 'Expected list to not exactly match the members of:\n' + - ` ${utils.printExpected(expected)}\n` + - 'Received:\n' + - ` ${utils.printReceived(actual)}`; - -const failMessage = (utils, actual, expected) => () => - utils.matcherHint('.toIncludeSameMembers') + - '\n\n' + - 'Expected list to have the following members and no more:\n' + - ` ${utils.printExpected(expected)}\n` + - 'Received:\n' + - ` ${utils.printReceived(actual)}`; - -export function toIncludeSameMembers(actual, expected) { - const pass = predicate(this.equals, actual, expected); - if (pass) { - return { pass: true, message: passMessage(this.utils, actual, expected) }; - } - return { pass: false, message: failMessage(this.utils, actual, expected) }; -} diff --git a/src/matchers/toIncludeSameMembers/predicate.js b/src/matchers/toIncludeSameMembers/predicate.js deleted file mode 100644 index b9ef04cf..00000000 --- a/src/matchers/toIncludeSameMembers/predicate.js +++ /dev/null @@ -1,22 +0,0 @@ -const filterMatches = (equals, first, second) => - second.reduce((remaining, secondValue) => { - if (remaining === null) return remaining; - - const index = remaining.findIndex(firstValue => equals(secondValue, firstValue)); - - if (index === -1) { - return null; - } - - return remaining.slice(0, index).concat(remaining.slice(index + 1)); - }, first); - -export default (equals, first, second) => { - if (!Array.isArray(first) || !Array.isArray(second) || first.length !== second.length) { - return false; - } - - const remaining = filterMatches(equals, first, second); - - return !!remaining && remaining.length === 0; -}; diff --git a/src/matchers/toIncludeSameMembers/predicate.test.js b/src/matchers/toIncludeSameMembers/predicate.test.js deleted file mode 100644 index 9b920fc7..00000000 --- a/src/matchers/toIncludeSameMembers/predicate.test.js +++ /dev/null @@ -1,54 +0,0 @@ -import { equals } from 'expect/build/jasmineUtils'; -import predicate from './predicate'; - -describe('toIncludeSameMembers Predicate', () => { - test('returns true when arrays are empty', () => { - expect(predicate(equals, [], [])).toBe(true); - }); - - test('returns true when arrays match with the same order', () => { - expect(predicate(equals, [1, 2, 3], [1, 2, 3])).toBe(true); - }); - - test('returns true when arrays match with reverse order', () => { - expect(predicate(equals, [1, 2, 3], [3, 2, 1])).toBe(true); - }); - - test('returns true when arrays match with duplicate elements and same order', () => { - expect(predicate(equals, [1, 2, 2], [1, 2, 2])).toBe(true); - }); - - test('returns true when arrays match with duplicate elements and reverse order', () => { - expect(predicate(equals, [1, 2, 2], [2, 2, 1])).toBe(true); - }); - - test('returns false when inputs are not arrays', () => { - expect(predicate(equals, null, null)).toBe(false); - expect(predicate(equals, null, [])).toBe(false); - expect(predicate(equals, [], null)).toBe(false); - }); - - test('returns false when arrays have different lengths', () => { - expect(predicate(equals, [], [1])).toBe(false); - expect(predicate(equals, [1], [])).toBe(false); - expect(predicate(equals, [1, 2, 3], [1, 2])).toBe(false); - }); - - test('returns false when no elements match', () => { - expect(predicate(equals, [1, 2], [3, 4])).toBe(false); - }); - - test('returns false when only one element matches', () => { - expect(predicate(equals, [1, 2, 3], [3, 4, 5])).toBe(false); - }); - - test('returns false when all but one element matches', () => { - expect(predicate(equals, [1, 2, 3], [2, 3, 4])).toBe(false); - }); - - test('does not modify the array content', () => { - const arr = [1, 2, 3]; - predicate(equals, arr, arr); - expect(arr).toEqual([1, 2, 3]); - }); -}); diff --git a/src/matchers/toPartiallyContain.js b/src/matchers/toPartiallyContain.js new file mode 100644 index 00000000..ad7c27d6 --- /dev/null +++ b/src/matchers/toPartiallyContain.js @@ -0,0 +1,30 @@ +import { containsEntry } from '../utils'; + +export function toPartiallyContain(actual, expected) { + const { printReceived, printExpected, matcherHint } = this.utils; + + const passMessage = + matcherHint('.not.toPartiallyContain') + + '\n\n' + + 'Expected array not to partially contain:\n' + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const failMessage = + matcherHint('.toPartiallyContain') + + '\n\n' + + 'Expected array to partially contain:\n' + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const pass = + Array.isArray(actual) && + Array.isArray([expected]) && + [expected].every(partial => + actual.some(value => Object.entries(partial).every(entry => containsEntry(this.equals, value, entry))), + ); + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toPartiallyContain/index.js b/src/matchers/toPartiallyContain/index.js deleted file mode 100644 index 2cd6668d..00000000 --- a/src/matchers/toPartiallyContain/index.js +++ /dev/null @@ -1,26 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, actual, expected) => () => - utils.matcherHint('.not.toPartiallyContain') + - '\n\n' + - 'Expected array not to partially contain:\n' + - ` ${utils.printExpected(expected)}\n` + - 'Received:\n' + - ` ${utils.printReceived(actual)}`; - -const failMessage = (utils, actual, expected) => () => - utils.matcherHint('.toPartiallyContain') + - '\n\n' + - 'Expected array to partially contain:\n' + - ` ${utils.printExpected(expected)}\n` + - 'Received:\n' + - ` ${utils.printReceived(actual)}`; - -export function toPartiallyContain(actual, expected) { - const pass = predicate(this.equals, actual, expected); - if (pass) { - return { pass: true, message: passMessage(this.utils, actual, expected) }; - } - - return { pass: false, message: failMessage(this.utils, actual, expected) }; -} diff --git a/src/matchers/toPartiallyContain/predicate.js b/src/matchers/toPartiallyContain/predicate.js deleted file mode 100644 index 5c27ed58..00000000 --- a/src/matchers/toPartiallyContain/predicate.js +++ /dev/null @@ -1,3 +0,0 @@ -import toIncludeAllPartialMembers from '../toIncludeAllPartialMembers/predicate'; - -export default (equals, array, item) => toIncludeAllPartialMembers(equals, array, [item]); diff --git a/src/matchers/toPartiallyContain/predicate.test.js b/src/matchers/toPartiallyContain/predicate.test.js deleted file mode 100644 index 6867ed54..00000000 --- a/src/matchers/toPartiallyContain/predicate.test.js +++ /dev/null @@ -1,14 +0,0 @@ -import { equals } from 'expect/build/jasmineUtils'; -import predicate from './predicate'; - -describe('.toPartiallyContain', () => { - const item = { foo: 'bar', baz: 'qux' }; - - test('passes when array partially contains the given item', () => { - expect(predicate(equals, [{ foo: 'bar', baz: 'qux', bax: 'zax' }], item)).toBe(true); - }); - - test('fails when array does not contain the given item', () => { - expect(predicate(equals, [{ a: 1, b: 2 }], item)).toBe(false); - }); -}); diff --git a/src/matchers/toReject.js b/src/matchers/toReject.js new file mode 100644 index 00000000..142508db --- /dev/null +++ b/src/matchers/toReject.js @@ -0,0 +1,16 @@ +export async function toReject(actual) { + const { matcherHint } = this.utils; + + const passMessage = + matcherHint('.not.toReject', 'received', '') + '\n\n' + 'Expected promise to resolve, however it rejected.\n'; + + const failMessage = + matcherHint('.toReject', 'received', '') + '\n\n' + 'Expected promise to reject, however it resolved.\n'; + + const pass = await actual.then( + () => false, + () => true, + ); + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toReject/index.js b/src/matchers/toReject/index.js deleted file mode 100644 index beb32412..00000000 --- a/src/matchers/toReject/index.js +++ /dev/null @@ -1,15 +0,0 @@ -import predicate from './predicate'; - -const passMessage = utils => () => - utils.matcherHint('.not.toReject', 'received', '') + '\n\n' + 'Expected promise to resolve, however it rejected.\n'; - -const failMessage = utils => () => - utils.matcherHint('.toReject', 'received', '') + '\n\n' + 'Expected promise to reject, however it resolved.\n'; - -export async function toReject(promise) { - const pass = await predicate(promise); - if (pass) { - return { pass: true, message: passMessage(this.utils) }; - } - return { pass: false, message: failMessage(this.utils) }; -} diff --git a/src/matchers/toReject/predicate.js b/src/matchers/toReject/predicate.js deleted file mode 100644 index aba70da0..00000000 --- a/src/matchers/toReject/predicate.js +++ /dev/null @@ -1,5 +0,0 @@ -export default promise => - promise.then( - () => false, - () => true, - ); diff --git a/src/matchers/toReject/predicate.test.js b/src/matchers/toReject/predicate.test.js deleted file mode 100644 index 89f7a32d..00000000 --- a/src/matchers/toReject/predicate.test.js +++ /dev/null @@ -1,13 +0,0 @@ -import predicate from './predicate'; - -describe('toReject predicate', () => { - test('should return true when passed a promise that rejects', async () => { - const promise = Promise.reject(); - expect(await predicate(promise)).toBe(true); - }); - - test('should return false when passed a promise that resolves', async () => { - const promise = Promise.resolve(); - expect(await predicate(promise)).toBe(false); - }); -}); diff --git a/src/matchers/toResolve.js b/src/matchers/toResolve.js new file mode 100644 index 00000000..4ec0ae9f --- /dev/null +++ b/src/matchers/toResolve.js @@ -0,0 +1,16 @@ +export async function toResolve(actual) { + const { matcherHint } = this.utils; + + const passMessage = + matcherHint('.not.toResolve', 'received', '') + '\n\n' + 'Expected promise to reject, however it resolved.\n'; + + const failMessage = + matcherHint('.toResolve', 'received', '') + '\n\n' + 'Expected promise to resolve, however it rejected.\n'; + + const pass = await actual.then( + () => true, + () => false, + ); + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toResolve/index.js b/src/matchers/toResolve/index.js deleted file mode 100644 index ff8c0b7b..00000000 --- a/src/matchers/toResolve/index.js +++ /dev/null @@ -1,15 +0,0 @@ -import predicate from './predicate'; - -const passMessage = utils => () => - utils.matcherHint('.not.toResolve', 'received', '') + '\n\n' + 'Expected promise to reject, however it resolved.\n'; - -const failMessage = utils => () => - utils.matcherHint('.toResolve', 'received', '') + '\n\n' + 'Expected promise to resolve, however it rejected.\n'; - -export async function toResolve(promise) { - const pass = await predicate(promise); - if (pass) { - return { pass: true, message: passMessage(this.utils) }; - } - return { pass: false, message: failMessage(this.utils) }; -} diff --git a/src/matchers/toResolve/predicate.js b/src/matchers/toResolve/predicate.js deleted file mode 100644 index 92c2e2d4..00000000 --- a/src/matchers/toResolve/predicate.js +++ /dev/null @@ -1,5 +0,0 @@ -export default promise => - promise.then( - () => true, - () => false, - ); diff --git a/src/matchers/toResolve/predicate.test.js b/src/matchers/toResolve/predicate.test.js deleted file mode 100644 index 454da749..00000000 --- a/src/matchers/toResolve/predicate.test.js +++ /dev/null @@ -1,13 +0,0 @@ -import predicate from './predicate'; - -describe('toResolve predicate', () => { - test('should return true when passed a promise that resolves', async () => { - const promise = Promise.resolve(); - expect(await predicate(promise)).toBe(true); - }); - - test('should return false when passed a promise that rejects', async () => { - const promise = Promise.reject(); - expect(await predicate(promise)).toBe(false); - }); -}); diff --git a/src/matchers/toSatisfy.js b/src/matchers/toSatisfy.js new file mode 100644 index 00000000..997f14fb --- /dev/null +++ b/src/matchers/toSatisfy.js @@ -0,0 +1,23 @@ +export function toSatisfy(actual, expected) { + const { printReceived, printExpected, matcherHint } = this.utils; + + const passMessage = + matcherHint('.not.toSatisfy', 'received', '') + + '\n\n' + + 'Expected value to not satisfy:\n' + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const failMessage = + matcherHint('.toSatisfy', 'received', '') + + '\n\n' + + 'Expected value to satisfy:\n' + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const pass = expected(actual); + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toSatisfy/index.js b/src/matchers/toSatisfy/index.js deleted file mode 100644 index c52c2007..00000000 --- a/src/matchers/toSatisfy/index.js +++ /dev/null @@ -1,24 +0,0 @@ -const passMessage = (utils, received, expected) => () => - utils.matcherHint('.not.toSatisfy', 'received', '') + - '\n\n' + - 'Expected value to not satisfy:\n' + - ` ${utils.printExpected(expected)}\n` + - 'Received:\n' + - ` ${utils.printReceived(received)}`; - -const failMessage = (utils, received, expected) => () => - utils.matcherHint('.toSatisfy', 'received', '') + - '\n\n' + - 'Expected value to satisfy:\n' + - ` ${utils.printExpected(expected)}\n` + - 'Received:\n' + - ` ${utils.printReceived(received)}`; - -export function toSatisfy(actual, predicate) { - const pass = predicate(actual); - if (pass) { - return { pass: true, message: passMessage(this.utils, actual, predicate) }; - } - - return { pass: false, message: failMessage(this.utils, actual, predicate) }; -} diff --git a/src/matchers/toSatisfyAll.js b/src/matchers/toSatisfyAll.js new file mode 100644 index 00000000..a2209151 --- /dev/null +++ b/src/matchers/toSatisfyAll.js @@ -0,0 +1,23 @@ +export function toSatisfyAll(actual, expected) { + const { printReceived, printExpected, matcherHint } = this.utils; + + const passMessage = + matcherHint('.not.toSatisfyAll') + + '\n\n' + + 'Expected array to not satisfy predicate for all values:\n' + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const failMessage = + matcherHint('.toSatisfyAll') + + '\n\n' + + 'Expected array to satisfy predicate for all values:\n' + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const pass = actual.every(expected); + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toSatisfyAll/index.js b/src/matchers/toSatisfyAll/index.js deleted file mode 100644 index 066f964e..00000000 --- a/src/matchers/toSatisfyAll/index.js +++ /dev/null @@ -1,26 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, actual, expected) => () => - utils.matcherHint('.not.toSatisfyAll') + - '\n\n' + - 'Expected array to not satisfy predicate for all values:\n' + - ` ${utils.printExpected(expected)}\n` + - 'Received:\n' + - ` ${utils.printReceived(actual)}`; - -const failMessage = (utils, actual, expected) => () => - utils.matcherHint('.toSatisfyAll') + - '\n\n' + - 'Expected array to satisfy predicate for all values:\n' + - ` ${utils.printExpected(expected)}\n` + - 'Received:\n' + - ` ${utils.printReceived(actual)}`; - -export function toSatisfyAll(actual, expected) { - const pass = predicate(actual, expected); - if (pass) { - return { pass: true, message: passMessage(this.utils, actual, expected) }; - } - - return { pass: false, message: failMessage(this.utils, actual, expected) }; -} diff --git a/src/matchers/toSatisfyAll/predicate.js b/src/matchers/toSatisfyAll/predicate.js deleted file mode 100644 index 96775890..00000000 --- a/src/matchers/toSatisfyAll/predicate.js +++ /dev/null @@ -1 +0,0 @@ -export default (array, predicate) => array.every(predicate); diff --git a/src/matchers/toSatisfyAll/predicate.test.js b/src/matchers/toSatisfyAll/predicate.test.js deleted file mode 100644 index b683f42c..00000000 --- a/src/matchers/toSatisfyAll/predicate.test.js +++ /dev/null @@ -1,29 +0,0 @@ -import predicate from './predicate'; - -describe('toSatisfyAll', () => { - let isOdd = el => el % 2 === 1; - - describe('returns true', () => { - test('when all elements satisfy', () => { - expect(predicate([1, 3, 7, 9], isOdd)).toBe(true); - }); - - test('works for repeated elements', () => { - expect(predicate([1, 3, 3, 9], isOdd)).toBe(true); - }); - }); - - describe('returns false', () => { - test('when all elements fail', () => { - expect(predicate([10, 2, 4, 6], isOdd)).toBe(false); - }); - - test('when either element fails', () => { - expect(predicate([5, 7, 8, 9], isOdd)).toBe(false); - }); - - test('works for repeated elements', () => { - expect(predicate([1, 3, 3, 9, 10, 11], isOdd)).toBe(false); - }); - }); -}); diff --git a/src/matchers/toSatisfyAny.js b/src/matchers/toSatisfyAny.js new file mode 100644 index 00000000..665b36fc --- /dev/null +++ b/src/matchers/toSatisfyAny.js @@ -0,0 +1,23 @@ +export function toSatisfyAny(actual, expected) { + const { printReceived, printExpected, matcherHint } = this.utils; + + const passMessage = + matcherHint('.not.toSatisfyAny') + + '\n\n' + + 'Expected array to not satisfy predicate for any value:\n' + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const failMessage = + matcherHint('.toSatisfyAny') + + '\n\n' + + 'Expected array to satisfy predicate for any values:\n' + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const pass = actual.some(expected); + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toSatisfyAny/index.js b/src/matchers/toSatisfyAny/index.js deleted file mode 100644 index ddcf7dc5..00000000 --- a/src/matchers/toSatisfyAny/index.js +++ /dev/null @@ -1,26 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, actual, expected) => () => - utils.matcherHint('.not.toSatisfyAny') + - '\n\n' + - 'Expected array to not satisfy predicate for any value:\n' + - ` ${utils.printExpected(expected)}\n` + - 'Received:\n' + - ` ${utils.printReceived(actual)}`; - -const failMessage = (utils, actual, expected) => () => - utils.matcherHint('.toSatisfyAny') + - '\n\n' + - 'Expected array to satisfy predicate for any values:\n' + - ` ${utils.printExpected(expected)}\n` + - 'Received:\n' + - ` ${utils.printReceived(actual)}`; - -export function toSatisfyAny(actual, expected) { - const pass = predicate(actual, expected); - if (pass) { - return { pass: true, message: passMessage(this.utils, actual, expected) }; - } - - return { pass: false, message: failMessage(this.utils, actual, expected) }; -} diff --git a/src/matchers/toSatisfyAny/predicate.js b/src/matchers/toSatisfyAny/predicate.js deleted file mode 100644 index 074e723d..00000000 --- a/src/matchers/toSatisfyAny/predicate.js +++ /dev/null @@ -1 +0,0 @@ -export default (array, predicate) => array.some(predicate); diff --git a/src/matchers/toSatisfyAny/predicate.test.js b/src/matchers/toSatisfyAny/predicate.test.js deleted file mode 100644 index 2712429a..00000000 --- a/src/matchers/toSatisfyAny/predicate.test.js +++ /dev/null @@ -1,25 +0,0 @@ -import predicate from './predicate'; - -describe('toSatisfyAny', () => { - let isOdd = el => el % 2 === 1; - - describe('returns true', () => { - test('when any elements satisfy', () => { - expect(predicate([2, 3, 6, 8], isOdd)).toBe(true); - }); - - test('works for repeated elements', () => { - expect(predicate([2, 3, 3, 8], isOdd)).toBe(true); - }); - }); - - describe('returns false', () => { - test('when all elements fail', () => { - expect(predicate([10, 2, 4, 6], isOdd)).toBe(false); - }); - - test('works for repeated elements', () => { - expect(predicate([2, 4, 4, 8, 10], isOdd)).toBe(false); - }); - }); -}); diff --git a/src/matchers/toStartWith.js b/src/matchers/toStartWith.js new file mode 100644 index 00000000..45dd36fd --- /dev/null +++ b/src/matchers/toStartWith.js @@ -0,0 +1,23 @@ +export function toStartWith(actual, expected) { + const { printReceived, printExpected, matcherHint } = this.utils; + + const passMessage = + matcherHint('.not.toStartWith') + + '\n\n' + + 'Expected string to not start with:\n' + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const failMessage = + matcherHint('.toStartWith') + + '\n\n' + + 'Expected string to start with:\n' + + ` ${printExpected(expected)}\n` + + 'Received:\n' + + ` ${printReceived(actual)}`; + + const pass = actual.startsWith(expected); + + return { pass, message: () => (pass ? passMessage : failMessage) }; +} diff --git a/src/matchers/toStartWith/index.js b/src/matchers/toStartWith/index.js deleted file mode 100644 index 93d52a81..00000000 --- a/src/matchers/toStartWith/index.js +++ /dev/null @@ -1,26 +0,0 @@ -import predicate from './predicate'; - -const passMessage = (utils, prefix, string) => () => - utils.matcherHint('.not.toStartWith') + - '\n\n' + - 'Expected string to not start with:\n' + - ` ${utils.printExpected(prefix)}\n` + - 'Received:\n' + - ` ${utils.printReceived(string)}`; - -const failMessage = (utils, prefix, string) => () => - utils.matcherHint('.toStartWith') + - '\n\n' + - 'Expected string to start with:\n' + - ` ${utils.printExpected(prefix)}\n` + - 'Received:\n' + - ` ${utils.printReceived(string)}`; - -export function toStartWith(string, prefix) { - const pass = predicate(prefix, string); - if (pass) { - return { pass: true, message: passMessage(this.utils, prefix, string) }; - } - - return { pass: false, message: failMessage(this.utils, prefix, string) }; -} diff --git a/src/matchers/toStartWith/predicate.js b/src/matchers/toStartWith/predicate.js deleted file mode 100644 index 2a5e4f1b..00000000 --- a/src/matchers/toStartWith/predicate.js +++ /dev/null @@ -1 +0,0 @@ -export default (prefix, string) => string.startsWith(prefix); diff --git a/src/matchers/toStartWith/predicate.test.js b/src/matchers/toStartWith/predicate.test.js deleted file mode 100644 index 7e1eac72..00000000 --- a/src/matchers/toStartWith/predicate.test.js +++ /dev/null @@ -1,11 +0,0 @@ -import predicate from './predicate'; - -describe('toStartWith Predicate', () => { - test('returns true when string starts with given prefix', () => { - expect(predicate('hello', 'hello world')).toBe(true); - }); - - test('returns false when string does not start with given prefix', () => { - expect(predicate('world', 'hello world')).toBe(false); - }); -}); diff --git a/src/matchers/toThrowWithMessage/index.js b/src/matchers/toThrowWithMessage.js similarity index 91% rename from src/matchers/toThrowWithMessage/index.js rename to src/matchers/toThrowWithMessage.js index aa45f853..ad44ee4e 100644 --- a/src/matchers/toThrowWithMessage/index.js +++ b/src/matchers/toThrowWithMessage.js @@ -1,4 +1,9 @@ -import predicate from './predicate'; +const predicate = (error, type, message) => { + if (message instanceof RegExp) { + return error && error instanceof type && message.test(error.message); + } + return error && error instanceof type && error.message === message; +}; const positiveHint = utils => utils.matcherHint('.toThrowWithMessage', 'function', 'type', { secondArgument: 'message' }); diff --git a/src/matchers/toThrowWithMessage/predicate.js b/src/matchers/toThrowWithMessage/predicate.js deleted file mode 100644 index 31811811..00000000 --- a/src/matchers/toThrowWithMessage/predicate.js +++ /dev/null @@ -1,6 +0,0 @@ -export default (error, type, message) => { - if (message instanceof RegExp) { - return error && error instanceof type && message.test(error.message); - } - return error && error instanceof type && error.message === message; -}; diff --git a/src/matchers/toThrowWithMessage/predicate.test.js b/src/matchers/toThrowWithMessage/predicate.test.js deleted file mode 100644 index 513c50d7..00000000 --- a/src/matchers/toThrowWithMessage/predicate.test.js +++ /dev/null @@ -1,89 +0,0 @@ -import predicate from './predicate'; - -describe('toThrowWithMessage Predicate for a string message and same type of error returns true', () => { - test.each` - error | type | errorMessage - ${new Error('the')} | ${Error} | ${'the'} - ${new SyntaxError('quick')} | ${SyntaxError} | ${'quick'} - ${new EvalError('brown')} | ${EvalError} | ${'brown'} - ${new RangeError('fox')} | ${RangeError} | ${'fox'} - ${new ReferenceError('jumps')} | ${ReferenceError} | ${'jumps'} - ${new SyntaxError('over')} | ${SyntaxError} | ${'over'} - ${new URIError('the lazy')} | ${URIError} | ${'the lazy'} - `('returns false when not instance of $type with the correct error message', ({ error, type, errorMessage }) => { - expect(predicate(error, type, errorMessage)).toBe(true); - }); -}); - -describe('toThrowWithMessage Predicate for a regex message and same type of error returns true', () => { - test.each` - error | type | errorMessage - ${new Error('the')} | ${Error} | ${/the/} - ${new SyntaxError('quick')} | ${SyntaxError} | ${/quick/} - ${new EvalError('brown')} | ${EvalError} | ${/brown/} - ${new RangeError('fox')} | ${RangeError} | ${/fox/} - ${new ReferenceError('jumps')} | ${ReferenceError} | ${/jumps/} - ${new SyntaxError('over')} | ${SyntaxError} | ${/over/} - ${new URIError('the lazy')} | ${URIError} | ${/the lazy/} - `('returns false when not instance of $type with the correct error message', ({ error, type, errorMessage }) => { - expect(predicate(error, type, errorMessage)).toBe(true); - }); -}); - -describe('toThrowWithMessage Predicate for a string message and same type of error returns false', () => { - test.each` - error | type | errorMessage - ${new Error('the')} | ${Error} | ${'thee'} - ${new SyntaxError('quick')} | ${SyntaxError} | ${'quicks'} - ${new EvalError('brown')} | ${EvalError} | ${'browns'} - ${new RangeError('fox')} | ${RangeError} | ${'foxs'} - ${new ReferenceError('jumps')} | ${ReferenceError} | ${'jumpss'} - ${new SyntaxError('over')} | ${SyntaxError} | ${'overs'} - ${new URIError('the lazy')} | ${URIError} | ${'the lazys'} - `('returns false when not instance of $type with the correct error message', ({ error, type, errorMessage }) => { - expect(predicate(error, type, errorMessage)).toBe(false); - }); - - describe('toThrowWithMessage Predicate for a regex message and same type of error returns false', () => { - test.each` - error | type | errorMessage - ${new Error('the')} | ${Error} | ${/thes/} - ${new SyntaxError('quick')} | ${SyntaxError} | ${/quicks/} - ${new EvalError('brown')} | ${EvalError} | ${/browns/} - ${new RangeError('fox')} | ${RangeError} | ${/foxs/} - ${new ReferenceError('jumps')} | ${ReferenceError} | ${/jumpss/} - ${new SyntaxError('over')} | ${SyntaxError} | ${/overs/} - ${new URIError('the lazy')} | ${URIError} | ${/the lazys/} - `('returns false when not instance of $type with the correct error message', ({ error, type, errorMessage }) => { - expect(predicate(error, type, errorMessage)).toBe(false); - }); - }); - - describe('toThrowWithMessage Predicate for a regex message and not same type of error returns false', () => { - test.each` - error | type | errorMessage - ${new EvalError('quick')} | ${SyntaxError} | ${/quick/} - ${new RangeError('brown')} | ${EvalError} | ${/brown/} - ${new ReferenceError('fox')} | ${RangeError} | ${/fox/} - ${new SyntaxError('jumps')} | ${ReferenceError} | ${/jumps/} - ${new URIError('over')} | ${SyntaxError} | ${/over/} - ${new SyntaxError('the lazy')} | ${URIError} | ${/the lazy/} - `('returns false when not instance of $type with the correct error message', ({ error, type, errorMessage }) => { - expect(predicate(error, type, errorMessage)).toBe(false); - }); - }); - - describe('toThrowWithMessage Predicate for a string message and not same type of error returns false', () => { - test.each` - error | type | errorMessage - ${new EvalError('quick')} | ${SyntaxError} | ${'quick'} - ${new RangeError('brown')} | ${EvalError} | ${'brown'} - ${new ReferenceError('fox')} | ${RangeError} | ${'fox'} - ${new SyntaxError('jumps')} | ${ReferenceError} | ${'jumps'} - ${new URIError('over')} | ${SyntaxError} | ${'over'} - ${new SyntaxError('the lazy')} | ${URIError} | ${'the lazy'} - `('returns false when not instance of $type with the correct error message', ({ error, type, errorMessage }) => { - expect(predicate(error, type, errorMessage)).toBe(false); - }); - }); -}); diff --git a/src/utils/index.js b/src/utils/index.js index c799b73f..1d38547b 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -9,3 +9,6 @@ export const determinePropertyMessage = (actual, property, message = 'Not Access export const isJestMockOrSpy = value => { return !!(value && value._isMockFunction === true && typeof value.mock === 'object'); }; + +export const containsEntry = (equals, obj, [key, value]) => + obj.hasOwnProperty && Object.prototype.hasOwnProperty.call(obj, key) && equals(obj[key], value); diff --git a/src/matchers/toEqualIgnoringWhitespace/print-util.js b/src/utils/print.js similarity index 100% rename from src/matchers/toEqualIgnoringWhitespace/print-util.js rename to src/utils/print.js diff --git a/src/matchers/fail/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/fail.test.js.snap similarity index 100% rename from src/matchers/fail/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/fail.test.js.snap diff --git a/src/matchers/pass/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/pass.test.js.snap similarity index 100% rename from src/matchers/pass/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/pass.test.js.snap diff --git a/src/matchers/toBeAfter/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toBeAfter.test.js.snap similarity index 100% rename from src/matchers/toBeAfter/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toBeAfter.test.js.snap diff --git a/src/matchers/toBeAfterOrEqualTo/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toBeAfterOrEqualTo.test.js.snap similarity index 100% rename from src/matchers/toBeAfterOrEqualTo/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toBeAfterOrEqualTo.test.js.snap diff --git a/src/matchers/toBeArray/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toBeArray.test.js.snap similarity index 100% rename from src/matchers/toBeArray/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toBeArray.test.js.snap diff --git a/src/matchers/toBeArrayOfSize/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toBeArrayOfSize.test.js.snap similarity index 100% rename from src/matchers/toBeArrayOfSize/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toBeArrayOfSize.test.js.snap diff --git a/src/matchers/toBeBefore/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toBeBefore.test.js.snap similarity index 100% rename from src/matchers/toBeBefore/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toBeBefore.test.js.snap diff --git a/src/matchers/toBeBeforeOrEqualTo/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toBeBeforeOrEqualTo.test.js.snap similarity index 100% rename from src/matchers/toBeBeforeOrEqualTo/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toBeBeforeOrEqualTo.test.js.snap diff --git a/src/matchers/toBeBetween/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toBeBetween.test.js.snap similarity index 100% rename from src/matchers/toBeBetween/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toBeBetween.test.js.snap diff --git a/src/matchers/toBeBoolean/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toBeBoolean.test.js.snap similarity index 100% rename from src/matchers/toBeBoolean/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toBeBoolean.test.js.snap diff --git a/src/matchers/toBeDate/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toBeDate.test.js.snap similarity index 100% rename from src/matchers/toBeDate/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toBeDate.test.js.snap diff --git a/src/matchers/toBeDateString/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toBeDateString.test.js.snap similarity index 100% rename from src/matchers/toBeDateString/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toBeDateString.test.js.snap diff --git a/src/matchers/toBeEmpty/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toBeEmpty.test.js.snap similarity index 100% rename from src/matchers/toBeEmpty/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toBeEmpty.test.js.snap diff --git a/src/matchers/toBeEmptyObject/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toBeEmptyObject.test.js.snap similarity index 100% rename from src/matchers/toBeEmptyObject/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toBeEmptyObject.test.js.snap diff --git a/src/matchers/toBeEven/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toBeEven.test.js.snap similarity index 100% rename from src/matchers/toBeEven/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toBeEven.test.js.snap diff --git a/src/matchers/toBeExtensible/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toBeExtensible.test.js.snap similarity index 100% rename from src/matchers/toBeExtensible/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toBeExtensible.test.js.snap diff --git a/src/matchers/toBeFalse/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toBeFalse.test.js.snap similarity index 100% rename from src/matchers/toBeFalse/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toBeFalse.test.js.snap diff --git a/src/matchers/toBeFinite/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toBeFinite.test.js.snap similarity index 100% rename from src/matchers/toBeFinite/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toBeFinite.test.js.snap diff --git a/src/matchers/toBeFrozen/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toBeFrozen.test.js.snap similarity index 100% rename from src/matchers/toBeFrozen/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toBeFrozen.test.js.snap diff --git a/src/matchers/toBeFunction/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toBeFunction.test.js.snap similarity index 100% rename from src/matchers/toBeFunction/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toBeFunction.test.js.snap diff --git a/src/matchers/toBeHexadecimal/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toBeHexadecimal.test.js.snap similarity index 100% rename from src/matchers/toBeHexadecimal/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toBeHexadecimal.test.js.snap diff --git a/src/matchers/toBeInteger/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toBeInteger.test.js.snap similarity index 100% rename from src/matchers/toBeInteger/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toBeInteger.test.js.snap diff --git a/src/matchers/toBeNaN/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toBeNaN.test.js.snap similarity index 100% rename from src/matchers/toBeNaN/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toBeNaN.test.js.snap diff --git a/src/matchers/toBeNegative/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toBeNegative.test.js.snap similarity index 100% rename from src/matchers/toBeNegative/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toBeNegative.test.js.snap diff --git a/src/matchers/toBeNil/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toBeNil.test.js.snap similarity index 100% rename from src/matchers/toBeNil/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toBeNil.test.js.snap diff --git a/src/matchers/toBeNumber/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toBeNumber.test.js.snap similarity index 100% rename from src/matchers/toBeNumber/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toBeNumber.test.js.snap diff --git a/src/matchers/toBeObject/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toBeObject.test.js.snap similarity index 100% rename from src/matchers/toBeObject/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toBeObject.test.js.snap diff --git a/src/matchers/toBeOdd/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toBeOdd.test.js.snap similarity index 100% rename from src/matchers/toBeOdd/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toBeOdd.test.js.snap diff --git a/src/matchers/toBeOneOf/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toBeOneOf.test.js.snap similarity index 100% rename from src/matchers/toBeOneOf/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toBeOneOf.test.js.snap diff --git a/src/matchers/toBePositive/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toBePositive.test.js.snap similarity index 100% rename from src/matchers/toBePositive/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toBePositive.test.js.snap diff --git a/src/matchers/toBeSealed/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toBeSealed.test.js.snap similarity index 100% rename from src/matchers/toBeSealed/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toBeSealed.test.js.snap diff --git a/src/matchers/toBeString/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toBeString.test.js.snap similarity index 100% rename from src/matchers/toBeString/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toBeString.test.js.snap diff --git a/src/matchers/toBeSymbol/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toBeSymbol.test.js.snap similarity index 100% rename from src/matchers/toBeSymbol/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toBeSymbol.test.js.snap diff --git a/src/matchers/toBeTrue/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toBeTrue.test.js.snap similarity index 100% rename from src/matchers/toBeTrue/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toBeTrue.test.js.snap diff --git a/src/matchers/toBeValidDate/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toBeValidDate.test.js.snap similarity index 100% rename from src/matchers/toBeValidDate/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toBeValidDate.test.js.snap diff --git a/src/matchers/toBeWithin/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toBeWithin.test.js.snap similarity index 100% rename from src/matchers/toBeWithin/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toBeWithin.test.js.snap diff --git a/src/matchers/toContainAllEntries/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toContainAllEntries.test.js.snap similarity index 100% rename from src/matchers/toContainAllEntries/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toContainAllEntries.test.js.snap diff --git a/src/matchers/toContainAllKeys/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toContainAllKeys.test.js.snap similarity index 100% rename from src/matchers/toContainAllKeys/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toContainAllKeys.test.js.snap diff --git a/src/matchers/toContainAllValues/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toContainAllValues.test.js.snap similarity index 100% rename from src/matchers/toContainAllValues/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toContainAllValues.test.js.snap diff --git a/src/matchers/toContainAnyEntries/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toContainAnyEntries.test.js.snap similarity index 100% rename from src/matchers/toContainAnyEntries/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toContainAnyEntries.test.js.snap diff --git a/src/matchers/toContainAnyKeys/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toContainAnyKeys.test.js.snap similarity index 100% rename from src/matchers/toContainAnyKeys/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toContainAnyKeys.test.js.snap diff --git a/src/matchers/toContainAnyValues/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toContainAnyValues.test.js.snap similarity index 100% rename from src/matchers/toContainAnyValues/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toContainAnyValues.test.js.snap diff --git a/src/matchers/toContainEntries/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toContainEntries.test.js.snap similarity index 100% rename from src/matchers/toContainEntries/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toContainEntries.test.js.snap diff --git a/src/matchers/toContainEntry/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toContainEntry.test.js.snap similarity index 100% rename from src/matchers/toContainEntry/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toContainEntry.test.js.snap diff --git a/src/matchers/toContainKey/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toContainKey.test.js.snap similarity index 100% rename from src/matchers/toContainKey/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toContainKey.test.js.snap diff --git a/src/matchers/toContainKeys/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toContainKeys.test.js.snap similarity index 100% rename from src/matchers/toContainKeys/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toContainKeys.test.js.snap diff --git a/src/matchers/toContainValue/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toContainValue.test.js.snap similarity index 100% rename from src/matchers/toContainValue/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toContainValue.test.js.snap diff --git a/src/matchers/toContainValues/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toContainValues.test.js.snap similarity index 100% rename from src/matchers/toContainValues/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toContainValues.test.js.snap diff --git a/src/matchers/toEndWith/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toEndWith.test.js.snap similarity index 100% rename from src/matchers/toEndWith/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toEndWith.test.js.snap diff --git a/src/matchers/toEqualCaseInsensitive/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toEqualCaseInsensitive.test.js.snap similarity index 100% rename from src/matchers/toEqualCaseInsensitive/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toEqualCaseInsensitive.test.js.snap diff --git a/src/matchers/toEqualIgnoringWhitespace/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toEqualIgnoringWhitespace.test.js.snap similarity index 100% rename from src/matchers/toEqualIgnoringWhitespace/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toEqualIgnoringWhitespace.test.js.snap diff --git a/src/matchers/toHaveBeenCalledAfter/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toHaveBeenCalledAfter.test.js.snap similarity index 100% rename from src/matchers/toHaveBeenCalledAfter/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toHaveBeenCalledAfter.test.js.snap diff --git a/src/matchers/toHaveBeenCalledBefore/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toHaveBeenCalledBefore.test.js.snap similarity index 100% rename from src/matchers/toHaveBeenCalledBefore/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toHaveBeenCalledBefore.test.js.snap diff --git a/src/matchers/toHaveBeenCalledOnce/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toHaveBeenCalledOnce.test.js.snap similarity index 100% rename from src/matchers/toHaveBeenCalledOnce/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toHaveBeenCalledOnce.test.js.snap diff --git a/src/matchers/toInclude/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toInclude.test.js.snap similarity index 100% rename from src/matchers/toInclude/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toInclude.test.js.snap diff --git a/src/matchers/toIncludeAllMembers/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toIncludeAllMembers.test.js.snap similarity index 100% rename from src/matchers/toIncludeAllMembers/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toIncludeAllMembers.test.js.snap diff --git a/src/matchers/toIncludeAllPartialMembers/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toIncludeAllPartialMembers.test.js.snap similarity index 100% rename from src/matchers/toIncludeAllPartialMembers/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toIncludeAllPartialMembers.test.js.snap diff --git a/src/matchers/toIncludeAnyMembers/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toIncludeAnyMembers.test.js.snap similarity index 100% rename from src/matchers/toIncludeAnyMembers/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toIncludeAnyMembers.test.js.snap diff --git a/src/matchers/toIncludeMultiple/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toIncludeMultiple.test.js.snap similarity index 100% rename from src/matchers/toIncludeMultiple/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toIncludeMultiple.test.js.snap diff --git a/src/matchers/toIncludeRepeated/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toIncludeRepeated.test.js.snap similarity index 100% rename from src/matchers/toIncludeRepeated/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toIncludeRepeated.test.js.snap diff --git a/src/matchers/toIncludeSameMembers/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toIncludeSameMembers.test.js.snap similarity index 100% rename from src/matchers/toIncludeSameMembers/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toIncludeSameMembers.test.js.snap diff --git a/src/matchers/toPartiallyContain/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toPartiallyContain.test.js.snap similarity index 100% rename from src/matchers/toPartiallyContain/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toPartiallyContain.test.js.snap diff --git a/src/matchers/toReject/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toReject.test.js.snap similarity index 100% rename from src/matchers/toReject/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toReject.test.js.snap diff --git a/src/matchers/toResolve/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toResolve.test.js.snap similarity index 100% rename from src/matchers/toResolve/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toResolve.test.js.snap diff --git a/src/matchers/toSatisfy/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toSatisfy.test.js.snap similarity index 100% rename from src/matchers/toSatisfy/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toSatisfy.test.js.snap diff --git a/src/matchers/toSatisfyAll/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toSatisfyAll.test.js.snap similarity index 100% rename from src/matchers/toSatisfyAll/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toSatisfyAll.test.js.snap diff --git a/src/matchers/toSatisfyAny/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toSatisfyAny.test.js.snap similarity index 100% rename from src/matchers/toSatisfyAny/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toSatisfyAny.test.js.snap diff --git a/src/matchers/toStartWith/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toStartWith.test.js.snap similarity index 100% rename from src/matchers/toStartWith/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toStartWith.test.js.snap diff --git a/src/matchers/toThrowWithMessage/__snapshots__/index.test.js.snap b/test/matchers/__snapshots__/toThrowWithMessage.test.js.snap similarity index 87% rename from src/matchers/toThrowWithMessage/__snapshots__/index.test.js.snap rename to test/matchers/__snapshots__/toThrowWithMessage.test.js.snap index 333b3e57..ddb19853 100644 --- a/src/matchers/toThrowWithMessage/__snapshots__/index.test.js.snap +++ b/test/matchers/__snapshots__/toThrowWithMessage.test.js.snap @@ -83,6 +83,26 @@ exports[`.toThrowWithMessage fails when callback function is not provided 1`] = Received value must be a function but instead \\"undefined\\" was found" `; +exports[`.toThrowWithMessage fails when error message does not match expected message 1`] = ` +"expect(function).toThrowWithMessage(type, message) + +Expected to throw: + [SyntaxError: Expected message] +Thrown: + [SyntaxError: Actual message] +" +`; + +exports[`.toThrowWithMessage fails when error message does not match expected message regex 1`] = ` +"expect(function).toThrowWithMessage(type, message) + +Expected to throw: + [SyntaxError: /Expected message/] +Thrown: + [SyntaxError: Actual message] +" +`; + exports[`.toThrowWithMessage fails when error message is not provided 1`] = ` "expect(function).toThrowWithMessage(type, message) diff --git a/src/matchers/fail/index.test.js b/test/matchers/fail.test.js similarity index 91% rename from src/matchers/fail/index.test.js rename to test/matchers/fail.test.js index 4e4257b0..7bfa1002 100644 --- a/src/matchers/fail/index.test.js +++ b/test/matchers/fail.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/fail'; expect.extend(matcher); diff --git a/src/matchers/index.test.js b/test/matchers/index.test.js similarity index 91% rename from src/matchers/index.test.js rename to test/matchers/index.test.js index e3f7ab0b..6a8d386e 100644 --- a/src/matchers/index.test.js +++ b/test/matchers/index.test.js @@ -1,6 +1,6 @@ import * as fs from 'fs'; import * as path from 'path'; -import * as matchers from './index'; +import * as matchers from 'src/matchers/index'; expect.extend(matchers); @@ -47,7 +47,7 @@ describe('asymmetric matchers', () => { describe('all matchers', () => { test('must be exported', () => { - const directories = fs.readdirSync(__dirname).filter(dir => fs.statSync(path.join(__dirname, dir)).isDirectory()); + const directories = fs.readdirSync(path.join(__dirname, '../../src/matchers')).filter(n => n !== 'index.js'); const namedMatchers = Object.keys(matchers); try { diff --git a/src/matchers/pass/index.test.js b/test/matchers/pass.test.js similarity index 91% rename from src/matchers/pass/index.test.js rename to test/matchers/pass.test.js index 28e8e064..f5d43c73 100644 --- a/src/matchers/pass/index.test.js +++ b/test/matchers/pass.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/pass'; expect.extend(matcher); diff --git a/src/matchers/toBeAfter/index.test.js b/test/matchers/toBeAfter.test.js similarity index 93% rename from src/matchers/toBeAfter/index.test.js rename to test/matchers/toBeAfter.test.js index 0fb69fc7..9e2217e9 100644 --- a/src/matchers/toBeAfter/index.test.js +++ b/test/matchers/toBeAfter.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toBeAfter'; expect.extend(matcher); diff --git a/src/matchers/toBeAfterOrEqualTo/index.test.js b/test/matchers/toBeAfterOrEqualTo.test.js similarity index 94% rename from src/matchers/toBeAfterOrEqualTo/index.test.js rename to test/matchers/toBeAfterOrEqualTo.test.js index e5692698..20a154b2 100644 --- a/src/matchers/toBeAfterOrEqualTo/index.test.js +++ b/test/matchers/toBeAfterOrEqualTo.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toBeAfterOrEqualTo'; expect.extend(matcher); diff --git a/src/matchers/toBeArray/index.test.js b/test/matchers/toBeArray.test.js similarity index 92% rename from src/matchers/toBeArray/index.test.js rename to test/matchers/toBeArray.test.js index ae9494b6..8e5b4226 100644 --- a/src/matchers/toBeArray/index.test.js +++ b/test/matchers/toBeArray.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toBeArray'; expect.extend(matcher); diff --git a/src/matchers/toBeArrayOfSize/index.test.js b/test/matchers/toBeArrayOfSize.test.js similarity index 96% rename from src/matchers/toBeArrayOfSize/index.test.js rename to test/matchers/toBeArrayOfSize.test.js index 14719760..db37c408 100644 --- a/src/matchers/toBeArrayOfSize/index.test.js +++ b/test/matchers/toBeArrayOfSize.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toBeArrayOfSize'; expect.extend(matcher); diff --git a/src/matchers/toBeBefore/index.test.js b/test/matchers/toBeBefore.test.js similarity index 93% rename from src/matchers/toBeBefore/index.test.js rename to test/matchers/toBeBefore.test.js index 3dee8923..8ccbe16d 100644 --- a/src/matchers/toBeBefore/index.test.js +++ b/test/matchers/toBeBefore.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toBeBefore'; expect.extend(matcher); diff --git a/src/matchers/toBeBeforeOrEqualTo/index.test.js b/test/matchers/toBeBeforeOrEqualTo.test.js similarity index 94% rename from src/matchers/toBeBeforeOrEqualTo/index.test.js rename to test/matchers/toBeBeforeOrEqualTo.test.js index 01d9a608..f4a6f635 100644 --- a/src/matchers/toBeBeforeOrEqualTo/index.test.js +++ b/test/matchers/toBeBeforeOrEqualTo.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toBeBeforeOrEqualTo'; expect.extend(matcher); diff --git a/src/matchers/toBeBetween/index.test.js b/test/matchers/toBeBetween.test.js similarity index 94% rename from src/matchers/toBeBetween/index.test.js rename to test/matchers/toBeBetween.test.js index 2f99fa2c..bf24cb19 100644 --- a/src/matchers/toBeBetween/index.test.js +++ b/test/matchers/toBeBetween.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toBeBetween'; expect.extend(matcher); diff --git a/src/matchers/toBeBoolean/index.test.js b/test/matchers/toBeBoolean.test.js similarity index 95% rename from src/matchers/toBeBoolean/index.test.js rename to test/matchers/toBeBoolean.test.js index 8e3cd9d2..f3f57c33 100644 --- a/src/matchers/toBeBoolean/index.test.js +++ b/test/matchers/toBeBoolean.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toBeBoolean'; expect.extend(matcher); diff --git a/src/matchers/toBeDate/index.test.js b/test/matchers/toBeDate.test.js similarity index 92% rename from src/matchers/toBeDate/index.test.js rename to test/matchers/toBeDate.test.js index 42f15b68..236ec071 100644 --- a/src/matchers/toBeDate/index.test.js +++ b/test/matchers/toBeDate.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toBeDate'; expect.extend(matcher); diff --git a/src/matchers/toBeDateString/index.test.js b/test/matchers/toBeDateString.test.js similarity index 91% rename from src/matchers/toBeDateString/index.test.js rename to test/matchers/toBeDateString.test.js index 6afc1468..1298aff0 100644 --- a/src/matchers/toBeDateString/index.test.js +++ b/test/matchers/toBeDateString.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toBeDateString'; expect.extend(matcher); diff --git a/src/matchers/toBeEmpty/index.test.js b/test/matchers/toBeEmpty.test.js similarity index 61% rename from src/matchers/toBeEmpty/index.test.js rename to test/matchers/toBeEmpty.test.js index 6566fe50..825c6b66 100644 --- a/src/matchers/toBeEmpty/index.test.js +++ b/test/matchers/toBeEmpty.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toBeEmpty'; expect.extend(matcher); @@ -19,6 +19,20 @@ describe('.toBeEmpty', () => { expect({}).toBeEmpty(); }); + test('When empty Set is passed', () => { + expect(new Set()).toBeEmpty(); + }); + + test('When empty Map is passed', () => { + expect(new Map([])).toBeEmpty(); + }); + + test('When empty generator is passed', () => { + function* yieldsNothing() {} + + expect(yieldsNothing()).toBeEmpty(); + }); + test('fails when given non-empty string', () => { expect(() => expect('string').toBeEmpty()).toThrowErrorMatchingSnapshot(); }); @@ -41,6 +55,22 @@ describe('.not.toBeEmpty', () => { expect({ foo: 'bar' }).not.toBeEmpty(); }); + test('When empty Set is passed', () => { + expect(new Set(['value'])).not.toBeEmpty(); + }); + + test('When empty Map is passed', () => { + expect(new Map([['k', 'v']])).not.toBeEmpty(); + }); + + test('When empty generator is passed', () => { + function* yieldsNothing() { + yield 'a thing'; + } + + expect(yieldsNothing()).not.toBeEmpty(); + }); + test('fails when given empty string', () => { expect(() => expect('').not.toBeEmpty()).toThrowErrorMatchingSnapshot(); }); diff --git a/src/matchers/toBeEmptyObject/index.test.js b/test/matchers/toBeEmptyObject.test.js similarity index 91% rename from src/matchers/toBeEmptyObject/index.test.js rename to test/matchers/toBeEmptyObject.test.js index 5e55adfc..714206e3 100644 --- a/src/matchers/toBeEmptyObject/index.test.js +++ b/test/matchers/toBeEmptyObject.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toBeEmptyObject'; expect.extend(matcher); diff --git a/src/matchers/toBeEven/index.test.js b/test/matchers/toBeEven.test.js similarity index 93% rename from src/matchers/toBeEven/index.test.js rename to test/matchers/toBeEven.test.js index a96cdb02..672cfac6 100644 --- a/src/matchers/toBeEven/index.test.js +++ b/test/matchers/toBeEven.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toBeEven'; expect.extend(matcher); diff --git a/src/matchers/toBeExtensible/index.test.js b/test/matchers/toBeExtensible.test.js similarity index 94% rename from src/matchers/toBeExtensible/index.test.js rename to test/matchers/toBeExtensible.test.js index ee29de4b..7f8b5abd 100644 --- a/src/matchers/toBeExtensible/index.test.js +++ b/test/matchers/toBeExtensible.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toBeExtensible'; expect.extend(matcher); diff --git a/src/matchers/toBeFalse/index.test.js b/test/matchers/toBeFalse.test.js similarity index 92% rename from src/matchers/toBeFalse/index.test.js rename to test/matchers/toBeFalse.test.js index 0b0d6bcd..9ffa7d2d 100644 --- a/src/matchers/toBeFalse/index.test.js +++ b/test/matchers/toBeFalse.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toBeFalse'; expect.extend(matcher); diff --git a/src/matchers/toBeFinite/index.test.js b/test/matchers/toBeFinite.test.js similarity index 95% rename from src/matchers/toBeFinite/index.test.js rename to test/matchers/toBeFinite.test.js index e04717dc..6e4acabc 100644 --- a/src/matchers/toBeFinite/index.test.js +++ b/test/matchers/toBeFinite.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toBeFinite'; expect.extend(matcher); diff --git a/src/matchers/toBeFrozen/index.test.js b/test/matchers/toBeFrozen.test.js similarity index 91% rename from src/matchers/toBeFrozen/index.test.js rename to test/matchers/toBeFrozen.test.js index 01c10fc8..7111ff1e 100644 --- a/src/matchers/toBeFrozen/index.test.js +++ b/test/matchers/toBeFrozen.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toBeFrozen'; expect.extend(matcher); diff --git a/src/matchers/toBeFunction/index.test.js b/test/matchers/toBeFunction.test.js similarity index 92% rename from src/matchers/toBeFunction/index.test.js rename to test/matchers/toBeFunction.test.js index 5b9ec560..037a9e18 100644 --- a/src/matchers/toBeFunction/index.test.js +++ b/test/matchers/toBeFunction.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toBeFunction'; expect.extend(matcher); diff --git a/src/matchers/toBeHexadecimal/index.test.js b/test/matchers/toBeHexadecimal.test.js similarity index 92% rename from src/matchers/toBeHexadecimal/index.test.js rename to test/matchers/toBeHexadecimal.test.js index 34b055b7..2e1d5c79 100644 --- a/src/matchers/toBeHexadecimal/index.test.js +++ b/test/matchers/toBeHexadecimal.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toBeHexadecimal'; expect.extend(matcher); diff --git a/src/matchers/toBeInteger/index.test.js b/test/matchers/toBeInteger.test.js similarity index 90% rename from src/matchers/toBeInteger/index.test.js rename to test/matchers/toBeInteger.test.js index 903e2b7e..e70214d4 100644 --- a/src/matchers/toBeInteger/index.test.js +++ b/test/matchers/toBeInteger.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toBeInteger'; expect.extend(matcher); diff --git a/src/matchers/toBeNaN/index.test.js b/test/matchers/toBeNaN.test.js similarity index 92% rename from src/matchers/toBeNaN/index.test.js rename to test/matchers/toBeNaN.test.js index bb1233cd..e62f34a0 100644 --- a/src/matchers/toBeNaN/index.test.js +++ b/test/matchers/toBeNaN.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toBeNaN'; expect.extend(matcher); diff --git a/src/matchers/toBeNegative/index.test.js b/test/matchers/toBeNegative.test.js similarity index 94% rename from src/matchers/toBeNegative/index.test.js rename to test/matchers/toBeNegative.test.js index 0773afe5..52013620 100644 --- a/src/matchers/toBeNegative/index.test.js +++ b/test/matchers/toBeNegative.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toBeNegative'; expect.extend(matcher); diff --git a/src/matchers/toBeNil/index.test.js b/test/matchers/toBeNil.test.js similarity index 93% rename from src/matchers/toBeNil/index.test.js rename to test/matchers/toBeNil.test.js index 15f07ddc..243ff6e1 100644 --- a/src/matchers/toBeNil/index.test.js +++ b/test/matchers/toBeNil.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toBeNil'; expect.extend(matcher); diff --git a/src/matchers/toBeNumber/index.test.js b/test/matchers/toBeNumber.test.js similarity index 93% rename from src/matchers/toBeNumber/index.test.js rename to test/matchers/toBeNumber.test.js index 67dc0e15..a79d5c91 100644 --- a/src/matchers/toBeNumber/index.test.js +++ b/test/matchers/toBeNumber.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toBeNumber'; expect.extend(matcher); diff --git a/src/matchers/toBeObject/index.test.js b/test/matchers/toBeObject.test.js similarity index 93% rename from src/matchers/toBeObject/index.test.js rename to test/matchers/toBeObject.test.js index ef7fb954..4e874fb9 100644 --- a/src/matchers/toBeObject/index.test.js +++ b/test/matchers/toBeObject.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toBeObject'; expect.extend(matcher); diff --git a/src/matchers/toBeOdd/index.test.js b/test/matchers/toBeOdd.test.js similarity index 93% rename from src/matchers/toBeOdd/index.test.js rename to test/matchers/toBeOdd.test.js index 4177cd1e..d464a816 100644 --- a/src/matchers/toBeOdd/index.test.js +++ b/test/matchers/toBeOdd.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toBeOdd'; expect.extend(matcher); diff --git a/src/matchers/toBeOneOf/index.test.js b/test/matchers/toBeOneOf.test.js similarity index 92% rename from src/matchers/toBeOneOf/index.test.js rename to test/matchers/toBeOneOf.test.js index f1c93aba..ef83de0d 100644 --- a/src/matchers/toBeOneOf/index.test.js +++ b/test/matchers/toBeOneOf.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toBeOneOf'; expect.extend(matcher); diff --git a/src/matchers/toBePositive/index.test.js b/test/matchers/toBePositive.test.js similarity index 92% rename from src/matchers/toBePositive/index.test.js rename to test/matchers/toBePositive.test.js index ce962e37..d30e4e9a 100644 --- a/src/matchers/toBePositive/index.test.js +++ b/test/matchers/toBePositive.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toBePositive'; expect.extend(matcher); diff --git a/src/matchers/toBeSealed/index.test.js b/test/matchers/toBeSealed.test.js similarity index 91% rename from src/matchers/toBeSealed/index.test.js rename to test/matchers/toBeSealed.test.js index f05e27d4..d50596af 100644 --- a/src/matchers/toBeSealed/index.test.js +++ b/test/matchers/toBeSealed.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toBeSealed'; expect.extend(matcher); diff --git a/src/matchers/toBeString/index.test.js b/test/matchers/toBeString.test.js similarity index 94% rename from src/matchers/toBeString/index.test.js rename to test/matchers/toBeString.test.js index 34bb4740..c8b1bfb1 100644 --- a/src/matchers/toBeString/index.test.js +++ b/test/matchers/toBeString.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toBeString'; expect.extend(matcher); diff --git a/src/matchers/toBeSymbol/index.test.js b/test/matchers/toBeSymbol.test.js similarity index 92% rename from src/matchers/toBeSymbol/index.test.js rename to test/matchers/toBeSymbol.test.js index febc1c61..0c2faf30 100644 --- a/src/matchers/toBeSymbol/index.test.js +++ b/test/matchers/toBeSymbol.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toBeSymbol'; expect.extend(matcher); diff --git a/src/matchers/toBeTrue/index.test.js b/test/matchers/toBeTrue.test.js similarity index 92% rename from src/matchers/toBeTrue/index.test.js rename to test/matchers/toBeTrue.test.js index 8c43826d..c3f6312b 100644 --- a/src/matchers/toBeTrue/index.test.js +++ b/test/matchers/toBeTrue.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toBeTrue'; expect.extend(matcher); diff --git a/src/matchers/toBeValidDate/index.test.js b/test/matchers/toBeValidDate.test.js similarity index 94% rename from src/matchers/toBeValidDate/index.test.js rename to test/matchers/toBeValidDate.test.js index 9d4ac245..d36e1e62 100644 --- a/src/matchers/toBeValidDate/index.test.js +++ b/test/matchers/toBeValidDate.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toBeValidDate'; expect.extend(matcher); diff --git a/src/matchers/toBeWithin/index.test.js b/test/matchers/toBeWithin.test.js similarity index 93% rename from src/matchers/toBeWithin/index.test.js rename to test/matchers/toBeWithin.test.js index ac3d6619..ccbd8648 100644 --- a/src/matchers/toBeWithin/index.test.js +++ b/test/matchers/toBeWithin.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toBeWithin'; expect.extend(matcher); diff --git a/src/matchers/toContainAllEntries/index.test.js b/test/matchers/toContainAllEntries.test.js similarity index 94% rename from src/matchers/toContainAllEntries/index.test.js rename to test/matchers/toContainAllEntries.test.js index 5ef2ebf0..9fe79612 100644 --- a/src/matchers/toContainAllEntries/index.test.js +++ b/test/matchers/toContainAllEntries.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toContainAllEntries'; expect.extend(matcher); diff --git a/src/matchers/toContainAllKeys/index.test.js b/test/matchers/toContainAllKeys.test.js similarity index 94% rename from src/matchers/toContainAllKeys/index.test.js rename to test/matchers/toContainAllKeys.test.js index 81099a9e..a13bc126 100644 --- a/src/matchers/toContainAllKeys/index.test.js +++ b/test/matchers/toContainAllKeys.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toContainAllKeys'; expect.extend(matcher); diff --git a/src/matchers/toContainAllValues/index.test.js b/test/matchers/toContainAllValues.test.js similarity index 97% rename from src/matchers/toContainAllValues/index.test.js rename to test/matchers/toContainAllValues.test.js index 3fe695eb..90243fae 100644 --- a/src/matchers/toContainAllValues/index.test.js +++ b/test/matchers/toContainAllValues.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toContainAllValues'; expect.extend(matcher); diff --git a/src/matchers/toContainAnyEntries/index.test.js b/test/matchers/toContainAnyEntries.test.js similarity index 94% rename from src/matchers/toContainAnyEntries/index.test.js rename to test/matchers/toContainAnyEntries.test.js index 88db18e8..76856c56 100644 --- a/src/matchers/toContainAnyEntries/index.test.js +++ b/test/matchers/toContainAnyEntries.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toContainAnyEntries'; expect.extend(matcher); diff --git a/src/matchers/toContainAnyKeys/index.test.js b/test/matchers/toContainAnyKeys.test.js similarity index 93% rename from src/matchers/toContainAnyKeys/index.test.js rename to test/matchers/toContainAnyKeys.test.js index 71d72e8f..eb44c740 100644 --- a/src/matchers/toContainAnyKeys/index.test.js +++ b/test/matchers/toContainAnyKeys.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toContainAnyKeys'; expect.extend(matcher); diff --git a/src/matchers/toContainAnyValues/index.test.js b/test/matchers/toContainAnyValues.test.js similarity index 95% rename from src/matchers/toContainAnyValues/index.test.js rename to test/matchers/toContainAnyValues.test.js index faaf3d81..aef2caf3 100644 --- a/src/matchers/toContainAnyValues/index.test.js +++ b/test/matchers/toContainAnyValues.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toContainAnyValues'; expect.extend(matcher); diff --git a/src/matchers/toContainEntries/index.test.js b/test/matchers/toContainEntries.test.js similarity index 93% rename from src/matchers/toContainEntries/index.test.js rename to test/matchers/toContainEntries.test.js index 391854ee..a940d11b 100644 --- a/src/matchers/toContainEntries/index.test.js +++ b/test/matchers/toContainEntries.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toContainEntries'; expect.extend(matcher); diff --git a/src/matchers/toContainEntry/index.test.js b/test/matchers/toContainEntry.test.js similarity index 93% rename from src/matchers/toContainEntry/index.test.js rename to test/matchers/toContainEntry.test.js index 449b4d45..c6206a3b 100644 --- a/src/matchers/toContainEntry/index.test.js +++ b/test/matchers/toContainEntry.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toContainEntry'; expect.extend(matcher); diff --git a/src/matchers/toContainKey/index.test.js b/test/matchers/toContainKey.test.js similarity index 92% rename from src/matchers/toContainKey/index.test.js rename to test/matchers/toContainKey.test.js index 5e71b27d..548da91d 100644 --- a/src/matchers/toContainKey/index.test.js +++ b/test/matchers/toContainKey.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toContainKey'; expect.extend(matcher); diff --git a/src/matchers/toContainKeys/index.test.js b/test/matchers/toContainKeys.test.js similarity index 92% rename from src/matchers/toContainKeys/index.test.js rename to test/matchers/toContainKeys.test.js index e3b8e1a5..3483397b 100644 --- a/src/matchers/toContainKeys/index.test.js +++ b/test/matchers/toContainKeys.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toContainKeys'; expect.extend(matcher); diff --git a/src/matchers/toContainValue/index.test.js b/test/matchers/toContainValue.test.js similarity index 97% rename from src/matchers/toContainValue/index.test.js rename to test/matchers/toContainValue.test.js index cf682d36..46685b34 100644 --- a/src/matchers/toContainValue/index.test.js +++ b/test/matchers/toContainValue.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toContainValue'; expect.extend(matcher); diff --git a/src/matchers/toContainValues/index.test.js b/test/matchers/toContainValues.test.js similarity index 97% rename from src/matchers/toContainValues/index.test.js rename to test/matchers/toContainValues.test.js index 553195c8..ad3400ef 100644 --- a/src/matchers/toContainValues/index.test.js +++ b/test/matchers/toContainValues.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toContainValues'; expect.extend(matcher); diff --git a/src/matchers/toEndWith/index.test.js b/test/matchers/toEndWith.test.js similarity index 95% rename from src/matchers/toEndWith/index.test.js rename to test/matchers/toEndWith.test.js index 2f5366e5..31fb2129 100644 --- a/src/matchers/toEndWith/index.test.js +++ b/test/matchers/toEndWith.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toEndWith'; expect.extend(matcher); diff --git a/src/matchers/toEqualCaseInsensitive/index.test.js b/test/matchers/toEqualCaseInsensitive.test.js similarity index 91% rename from src/matchers/toEqualCaseInsensitive/index.test.js rename to test/matchers/toEqualCaseInsensitive.test.js index a9e0fa5f..45e153be 100644 --- a/src/matchers/toEqualCaseInsensitive/index.test.js +++ b/test/matchers/toEqualCaseInsensitive.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toEqualCaseInsensitive'; expect.extend(matcher); diff --git a/src/matchers/toEqualIgnoringWhitespace/index.test.js b/test/matchers/toEqualIgnoringWhitespace.test.js similarity index 97% rename from src/matchers/toEqualIgnoringWhitespace/index.test.js rename to test/matchers/toEqualIgnoringWhitespace.test.js index b6403149..e1c0f209 100644 --- a/src/matchers/toEqualIgnoringWhitespace/index.test.js +++ b/test/matchers/toEqualIgnoringWhitespace.test.js @@ -1,4 +1,4 @@ -import * as matcher from '.'; +import * as matcher from 'src/matchers/toEqualIgnoringWhitespace'; expect.extend(matcher); diff --git a/src/matchers/toHaveBeenCalledAfter/index.test.js b/test/matchers/toHaveBeenCalledAfter.test.js similarity index 87% rename from src/matchers/toHaveBeenCalledAfter/index.test.js rename to test/matchers/toHaveBeenCalledAfter.test.js index 750fabe5..e69d3ee9 100644 --- a/src/matchers/toHaveBeenCalledAfter/index.test.js +++ b/test/matchers/toHaveBeenCalledAfter.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toHaveBeenCalledAfter'; expect.extend(matcher); @@ -70,6 +70,20 @@ describe('.toHaveBeenCalledAfter', () => { const mock2 = () => {}; expect(() => expect(mock1).toHaveBeenCalledAfter(mock2)).toThrowErrorMatchingSnapshot(); }); + + test('passes when given first timestamps does not contain a timestamp less than any of the second timestamps', () => { + const now = Date.now(); + const lessThan = now - 100; + const greaterThan = now + 100; + + const mock1 = jest.fn(); + const mock2 = jest.fn(); + mock1.mock.invocationCallOrder[0] = greaterThan; // amend the value for the snapshot + mock1.mock.invocationCallOrder[1] = now; + mock1.mock.invocationCallOrder[1] = greaterThan; + mock2.mock.invocationCallOrder[0] = lessThan; + expect(mock1).toHaveBeenCalledAfter(mock2); + }); }); describe('.not.toHaveBeenCalledAfter', () => { diff --git a/src/matchers/toHaveBeenCalledBefore/index.test.js b/test/matchers/toHaveBeenCalledBefore.test.js similarity index 87% rename from src/matchers/toHaveBeenCalledBefore/index.test.js rename to test/matchers/toHaveBeenCalledBefore.test.js index 0db113ee..570c9eae 100644 --- a/src/matchers/toHaveBeenCalledBefore/index.test.js +++ b/test/matchers/toHaveBeenCalledBefore.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toHaveBeenCalledBefore'; expect.extend(matcher); @@ -45,6 +45,20 @@ describe('.toHaveBeenCalledBefore', () => { expect(mock1).toHaveBeenCalledBefore(mock2); }); + test('passes when given first timestamps does not contain a timestamp greater than any of the second timestamps', () => { + const now = Date.now(); + const lessThan = now - 100; + const greaterThan = now + 100; + + const mock1 = jest.fn(); + const mock2 = jest.fn(); + mock1.mock.invocationCallOrder[0] = lessThan; // amend the value for the snapshot + mock1.mock.invocationCallOrder[1] = now; + mock1.mock.invocationCallOrder[1] = lessThan; + mock2.mock.invocationCallOrder[0] = greaterThan; + expect(mock1).toHaveBeenCalledBefore(mock2); + }); + test('fails when given first mock is called after several calls to second mock', () => { const mock1 = jest.fn(); const mock2 = jest.fn(); diff --git a/src/matchers/toHaveBeenCalledOnce/index.test.js b/test/matchers/toHaveBeenCalledOnce.test.js similarity index 95% rename from src/matchers/toHaveBeenCalledOnce/index.test.js rename to test/matchers/toHaveBeenCalledOnce.test.js index e9eb1c37..c398375f 100644 --- a/src/matchers/toHaveBeenCalledOnce/index.test.js +++ b/test/matchers/toHaveBeenCalledOnce.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toHaveBeenCalledOnce'; expect.extend(matcher); diff --git a/src/matchers/toInclude/index.test.js b/test/matchers/toInclude.test.js similarity index 93% rename from src/matchers/toInclude/index.test.js rename to test/matchers/toInclude.test.js index 2e1ee4b5..736d72fb 100644 --- a/src/matchers/toInclude/index.test.js +++ b/test/matchers/toInclude.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toInclude'; expect.extend(matcher); diff --git a/src/matchers/toIncludeAllMembers/index.test.js b/test/matchers/toIncludeAllMembers.test.js similarity index 96% rename from src/matchers/toIncludeAllMembers/index.test.js rename to test/matchers/toIncludeAllMembers.test.js index 3862da88..9324195e 100644 --- a/src/matchers/toIncludeAllMembers/index.test.js +++ b/test/matchers/toIncludeAllMembers.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toIncludeAllMembers'; expect.extend(matcher); diff --git a/src/matchers/toIncludeAllPartialMembers/index.test.js b/test/matchers/toIncludeAllPartialMembers.test.js similarity index 95% rename from src/matchers/toIncludeAllPartialMembers/index.test.js rename to test/matchers/toIncludeAllPartialMembers.test.js index 05d0280d..d5d93afa 100644 --- a/src/matchers/toIncludeAllPartialMembers/index.test.js +++ b/test/matchers/toIncludeAllPartialMembers.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toIncludeAllPartialMembers'; expect.extend(matcher); diff --git a/src/matchers/toIncludeAnyMembers/index.test.js b/test/matchers/toIncludeAnyMembers.test.js similarity index 97% rename from src/matchers/toIncludeAnyMembers/index.test.js rename to test/matchers/toIncludeAnyMembers.test.js index c1ccd162..a19c85f5 100644 --- a/src/matchers/toIncludeAnyMembers/index.test.js +++ b/test/matchers/toIncludeAnyMembers.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toIncludeAnyMembers'; expect.extend(matcher); diff --git a/src/matchers/toIncludeMultiple/index.test.js b/test/matchers/toIncludeMultiple.test.js similarity index 92% rename from src/matchers/toIncludeMultiple/index.test.js rename to test/matchers/toIncludeMultiple.test.js index aaf39bb6..dd1ee46f 100644 --- a/src/matchers/toIncludeMultiple/index.test.js +++ b/test/matchers/toIncludeMultiple.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toIncludeMultiple'; expect.extend(matcher); diff --git a/src/matchers/toIncludeRepeated/index.test.js b/test/matchers/toIncludeRepeated.test.js similarity index 96% rename from src/matchers/toIncludeRepeated/index.test.js rename to test/matchers/toIncludeRepeated.test.js index 4d250f59..c047aa6f 100644 --- a/src/matchers/toIncludeRepeated/index.test.js +++ b/test/matchers/toIncludeRepeated.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toIncludeRepeated'; expect.extend(matcher); diff --git a/src/matchers/toIncludeSameMembers/index.test.js b/test/matchers/toIncludeSameMembers.test.js similarity index 84% rename from src/matchers/toIncludeSameMembers/index.test.js rename to test/matchers/toIncludeSameMembers.test.js index 62001bad..020ab2ac 100644 --- a/src/matchers/toIncludeSameMembers/index.test.js +++ b/test/matchers/toIncludeSameMembers.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toIncludeSameMembers'; expect.extend(matcher); @@ -42,4 +42,12 @@ describe('.not.toIncludeSameMembers', () => { expect([1, 2]).not.toIncludeSameMembers([1, 2, 2]); expect([1, 2, 3]).not.toIncludeSameMembers([2, 3, 4]); }); + + test('passes when no elements match', () => { + expect([1, 2]).not.toIncludeSameMembers([3, 4]); + }); + + test('passes when only one element matches', () => { + expect([1, 2, 3]).not.toIncludeSameMembers([3, 4, 5]); + }); }); diff --git a/src/matchers/toPartiallyContain/index.test.js b/test/matchers/toPartiallyContain.test.js similarity index 93% rename from src/matchers/toPartiallyContain/index.test.js rename to test/matchers/toPartiallyContain.test.js index 1729c6bb..9c1c9fb3 100644 --- a/src/matchers/toPartiallyContain/index.test.js +++ b/test/matchers/toPartiallyContain.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toPartiallyContain'; expect.extend(matcher); diff --git a/src/matchers/toReject/index.test.js b/test/matchers/toReject.test.js similarity index 94% rename from src/matchers/toReject/index.test.js rename to test/matchers/toReject.test.js index d92535e3..8c0326e8 100644 --- a/src/matchers/toReject/index.test.js +++ b/test/matchers/toReject.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toReject'; expect.extend(matcher); diff --git a/src/matchers/toResolve/index.test.js b/test/matchers/toResolve.test.js similarity index 94% rename from src/matchers/toResolve/index.test.js rename to test/matchers/toResolve.test.js index 92bcb75e..1f03bc2b 100644 --- a/src/matchers/toResolve/index.test.js +++ b/test/matchers/toResolve.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toResolve'; expect.extend(matcher); diff --git a/src/matchers/toSatisfy/index.test.js b/test/matchers/toSatisfy.test.js similarity index 93% rename from src/matchers/toSatisfy/index.test.js rename to test/matchers/toSatisfy.test.js index 0991e2b3..4f3589f5 100644 --- a/src/matchers/toSatisfy/index.test.js +++ b/test/matchers/toSatisfy.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toSatisfy'; expect.extend(matcher); diff --git a/src/matchers/toSatisfyAll/index.test.js b/test/matchers/toSatisfyAll.test.js similarity index 94% rename from src/matchers/toSatisfyAll/index.test.js rename to test/matchers/toSatisfyAll.test.js index f6f1f510..ae0993b7 100644 --- a/src/matchers/toSatisfyAll/index.test.js +++ b/test/matchers/toSatisfyAll.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toSatisfyAll'; expect.extend(matcher); diff --git a/src/matchers/toSatisfyAny/index.test.js b/test/matchers/toSatisfyAny.test.js similarity index 94% rename from src/matchers/toSatisfyAny/index.test.js rename to test/matchers/toSatisfyAny.test.js index 0a7ebba6..c132525f 100644 --- a/src/matchers/toSatisfyAny/index.test.js +++ b/test/matchers/toSatisfyAny.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toSatisfyAny'; expect.extend(matcher); diff --git a/src/matchers/toStartWith/index.test.js b/test/matchers/toStartWith.test.js similarity index 94% rename from src/matchers/toStartWith/index.test.js rename to test/matchers/toStartWith.test.js index 4d99e89e..9ff51202 100644 --- a/src/matchers/toStartWith/index.test.js +++ b/test/matchers/toStartWith.test.js @@ -1,4 +1,4 @@ -import * as matcher from './'; +import * as matcher from 'src/matchers/toStartWith'; expect.extend(matcher); diff --git a/src/matchers/toThrowWithMessage/index.test.js b/test/matchers/toThrowWithMessage.test.js similarity index 89% rename from src/matchers/toThrowWithMessage/index.test.js rename to test/matchers/toThrowWithMessage.test.js index 37f90513..7d1d7485 100644 --- a/src/matchers/toThrowWithMessage/index.test.js +++ b/test/matchers/toThrowWithMessage.test.js @@ -1,5 +1,5 @@ import { matcherHint, printExpected, printReceived } from 'jest-matcher-utils'; -import * as matcher from './'; +import * as matcher from 'src/matchers/toThrowWithMessage'; const { toThrowWithMessage } = matcher; expect.extend(matcher); @@ -81,6 +81,34 @@ describe('.toThrowWithMessage', () => { expect(message()).toMatchSnapshot(); }); + test('fails when error message does not match expected message', () => { + const callback = () => { + throw SyntaxError('Actual message'); + }; + const { pass, message } = toThrowWithMessage.call( + { utils: { matcherHint: matcherHint, printExpected: printExpected, printReceived: printReceived } }, + callback, + SyntaxError, + 'Expected message', + ); + expect(pass).toBe(false); + expect(message()).toMatchSnapshot(); + }); + + test('fails when error message does not match expected message regex', () => { + const callback = () => { + throw SyntaxError('Actual message'); + }; + const { pass, message } = toThrowWithMessage.call( + { utils: { matcherHint: matcherHint, printExpected: printExpected, printReceived: printReceived } }, + callback, + SyntaxError, + /Expected message/, + ); + expect(pass).toBe(false); + expect(message()).toMatchSnapshot(); + }); + test('passes when given an Error with a string error message', () => { const callback = () => { throw TypeError('Expected message'); diff --git a/src/utils/index.test.js b/test/utils/index.test.js similarity index 99% rename from src/utils/index.test.js rename to test/utils/index.test.js index 6aa14be9..f979a04e 100644 --- a/src/utils/index.test.js +++ b/test/utils/index.test.js @@ -1,5 +1,5 @@ import { equals } from 'expect/build/jasmineUtils'; -import { contains, determinePropertyMessage, isJestMockOrSpy } from './'; +import { contains, determinePropertyMessage, isJestMockOrSpy } from 'src/utils'; describe('Utils', () => { describe('.contains', () => { diff --git a/src/matchers/toEqualIgnoringWhitespace/print-util.test.js b/test/utils/print.test.js similarity index 96% rename from src/matchers/toEqualIgnoringWhitespace/print-util.test.js rename to test/utils/print.test.js index b33f3c35..1cb3c6eb 100644 --- a/src/matchers/toEqualIgnoringWhitespace/print-util.test.js +++ b/test/utils/print.test.js @@ -1,4 +1,4 @@ -import { tokenize } from './print-util'; +import { tokenize } from 'src/utils/print'; describe('print-util module', () => { it('should tokenize given string', () => {