Skip to content

Commit

Permalink
feat: delete expect and jest-matcher-utils dependencies (#405)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Drop support for `jest@<27.2.5`
  • Loading branch information
keeganwitt authored Jan 28, 2022
1 parent 524306c commit 98ef8c7
Show file tree
Hide file tree
Showing 108 changed files with 918 additions and 1,041 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
fail-fast: false
matrix:
node-version: [12.x, 14.x, 16.x, 17.x]
jest-version: [24, 25, 26, 27]
jest-version: [27]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
Expand Down
4 changes: 0 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ Head over to [here](https://hacktoberfest.digitalocean.com/sign_up/register) to
- `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.
- [`jest-matchers-utils`](https://github.com/facebook/jest/tree/master/packages/jest-matcher-utils) is being used for syntax highlighting of error messages.
- See the Jest docs for an [example usage](https://facebook.github.io/jest/docs/en/expect.html#thisutils)
- Jest's [`expect`](https://github.com/facebook/jest/tree/master/packages/expect) package is being used to access their deep `equals` function.
- `import { equals } from 'expect/build/jasmineUtils';`
- 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`.

Expand Down
10 changes: 3 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Additional Jest matchers

## Problem

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

## Solution
Expand Down Expand Up @@ -139,7 +139,7 @@ yarn add -D jest-extended

## Setup

Note that `jest-extended` only supports Jest version 24 and newer.
Note that `jest-extended` only supports Jest version `27.2.5` and newer. If you're using an older version of Jest, use `1.2.0`.

```javascript
// ./testSetup.js
Expand Down Expand Up @@ -179,7 +179,7 @@ import 'jest-extended';

_Note: When using `ts-jest >= 25.5.0`_

Since the [breaking changes]() in `25.5.0` you may also need to update your `tsconfig.json` to include the new `global.d.ts` file in the `files` property like so:
Since the breaking changes in `25.5.0` you may also need to update your `tsconfig.json` to include the new `global.d.ts` file in the `files` property like so:

```json
{
Expand Down Expand Up @@ -570,8 +570,6 @@ test('throws an error of type TypeError with message "hello world"', async () =>
Use `.toHaveBeenCalledBefore` when checking if a `Mock` was called before another `Mock`.
_Note: Required Jest version >=23_
```js
it('calls mock1 before mock2', () => {
const mock1 = jest.fn();
Expand All @@ -589,8 +587,6 @@ it('calls mock1 before mock2', () => {
Use `.toHaveBeenCalledAfter` when checking if a `Mock` was called after another `Mock`.
_Note: Required Jest version >=23_
```js
it('calls mock1 after mock2', () => {
const mock1 = jest.fn();
Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,16 @@
"eslint-plugin-jest": "^26.0.0",
"eslint-plugin-prettier": "^4.0.0",
"husky": "^7.0.1",
"jest": "^27.2.4",
"jest": "^27.2.5",
"jest-watch-typeahead": "^1.0.0",
"lint-staged": "^12.0.0",
"prettier": "^2.3.2",
"pretty-format": "^27.2.4",
"typescript": "^4.4.3"
},
"dependencies": {
"expect": "^26.6.2",
"jest-diff": "^27.2.5",
"jest-get-type": "^27.0.6",
"jest-matcher-utils": "^27.2.4"
"jest-get-type": "^27.0.6"
},
"engines": {
"node": "^12.13.0 || ^14.15.0 || >=16.0.0"
Expand Down Expand Up @@ -113,5 +111,8 @@
"printWidth": 120,
"singleQuote": true,
"trailingComma": "all"
},
"peerDependencies": {
"jest": ">=27.2.5"
}
}
22 changes: 10 additions & 12 deletions src/matchers/toBeAfter/index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
import { matcherHint, printReceived } from 'jest-matcher-utils';

import predicate from './predicate';

const passMessage = (received, after) => () =>
matcherHint('.not.toBeAfter', 'received', '') +
const passMessage = (utils, received, after) => () =>
utils.matcherHint('.not.toBeAfter', 'received', '') +
'\n\n' +
`Expected date to be after ${printReceived(after)} but received:\n` +
` ${printReceived(received)}`;
`Expected date to be after ${utils.printReceived(after)} but received:\n` +
` ${utils.printReceived(received)}`;

const failMessage = (received, after) => () =>
matcherHint('.toBeAfter', 'received', '') +
const failMessage = (utils, received, after) => () =>
utils.matcherHint('.toBeAfter', 'received', '') +
'\n\n' +
`Expected date to be after ${printReceived(after)} but received:\n` +
` ${printReceived(received)}`;
`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(date, after) };
return { pass: true, message: passMessage(this.utils, date, after) };
}

return { pass: false, message: failMessage(date, after) };
return { pass: false, message: failMessage(this.utils, date, after) };
}
22 changes: 10 additions & 12 deletions src/matchers/toBeAfterOrEqualTo/index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
import { matcherHint, printReceived } from 'jest-matcher-utils';

import predicate from './predicate';

const passMessage = (received, before) => () =>
matcherHint('.not.toBeAfterOrEqualTo', 'received', '') +
const passMessage = (utils, received, before) => () =>
utils.matcherHint('.not.toBeAfterOrEqualTo', 'received', '') +
'\n\n' +
`Expected date to be after or equal to ${printReceived(before)} but received:\n` +
` ${printReceived(received)}`;
`Expected date to be after or equal to ${utils.printReceived(before)} but received:\n` +
` ${utils.printReceived(received)}`;

const failMessage = (received, before) => () =>
matcherHint('.toBeAfterOrEqualTo', 'received', '') +
const failMessage = (utils, received, before) => () =>
utils.matcherHint('.toBeAfterOrEqualTo', 'received', '') +
'\n\n' +
`Expected date to be after or equal to ${printReceived(before)} but received:\n` +
` ${printReceived(received)}`;
`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(date, after) };
return { pass: true, message: passMessage(this.utils, date, after) };
}

return { pass: false, message: failMessage(date, after) };
return { pass: false, message: failMessage(this.utils, date, after) };
}
18 changes: 8 additions & 10 deletions src/matchers/toBeArray/index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
import { matcherHint, printReceived } from 'jest-matcher-utils';

import predicate from './predicate';

const passMessage = received => () =>
matcherHint('.not.toBeArray', 'received', '') +
const passMessage = (utils, received) => () =>
utils.matcherHint('.not.toBeArray', 'received', '') +
'\n\n' +
'Expected value to not be an array received:\n' +
` ${printReceived(received)}`;
` ${utils.printReceived(received)}`;

const failMessage = received => () =>
matcherHint('.toBeArray', 'received', '') +
const failMessage = (utils, received) => () =>
utils.matcherHint('.toBeArray', 'received', '') +
'\n\n' +
'Expected value to be an array received:\n' +
` ${printReceived(received)}`;
` ${utils.printReceived(received)}`;

export function toBeArray(expected) {
const pass = predicate(expected);
if (pass) {
return { pass: true, message: passMessage(expected) };
return { pass: true, message: passMessage(this.utils, expected) };
}

return { pass: false, message: failMessage(expected) };
return { pass: false, message: failMessage(this.utils, expected) };
}
25 changes: 12 additions & 13 deletions src/matchers/toBeArrayOfSize/index.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
import { matcherHint, printExpected, printReceived } from 'jest-matcher-utils';
import { determinePropertyMessage } from '../../utils';

import predicate from './predicate';

const passMessage = (actual, expected) => () =>
`${matcherHint('.not.toBeArrayOfSize')}
const passMessage = (utils, actual, expected) => () =>
`${utils.matcherHint('.not.toBeArrayOfSize')}
Expected value to not be an array of size:
${printExpected(expected)}
${utils.printExpected(expected)}
Received:
value: ${printReceived(actual)}
length: ${printReceived(determinePropertyMessage(actual, 'length'))}`;
value: ${utils.printReceived(actual)}
length: ${utils.printReceived(determinePropertyMessage(actual, 'length'))}`;

const failMessage = (actual, expected) => () =>
`${matcherHint('.toBeArrayOfSize')}
const failMessage = (utils, actual, expected) => () =>
`${utils.matcherHint('.toBeArrayOfSize')}
Expected value to be an array of size:
${printExpected(expected)}
${utils.printExpected(expected)}
Received:
value: ${printReceived(actual)}
length: ${printReceived(determinePropertyMessage(actual, 'length'))}`;
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(actual, expected) };
return { pass: true, message: passMessage(this.utils, actual, expected) };
}

return { pass: false, message: failMessage(actual, expected) };
return { pass: false, message: failMessage(this.utils, actual, expected) };
}
22 changes: 10 additions & 12 deletions src/matchers/toBeBefore/index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
import { matcherHint, printReceived } from 'jest-matcher-utils';

import predicate from './predicate';

const passMessage = (received, before) => () =>
matcherHint('.not.toBeBefore', 'received', '') +
const passMessage = (utils, received, before) => () =>
utils.matcherHint('.not.toBeBefore', 'received', '') +
'\n\n' +
`Expected date to be before ${printReceived(before)} but received:\n` +
` ${printReceived(received)}`;
`Expected date to be before ${utils.printReceived(before)} but received:\n` +
` ${utils.printReceived(received)}`;

const failMessage = (received, before) => () =>
matcherHint('.toBeBefore', 'received', '') +
const failMessage = (utils, received, before) => () =>
utils.matcherHint('.toBeBefore', 'received', '') +
'\n\n' +
`Expected date to be before ${printReceived(before)} but received:\n` +
` ${printReceived(received)}`;
`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(date, before) };
return { pass: true, message: passMessage(this.utils, date, before) };
}

return { pass: false, message: failMessage(date, before) };
return { pass: false, message: failMessage(this.utils, date, before) };
}
22 changes: 10 additions & 12 deletions src/matchers/toBeBeforeOrEqualTo/index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
import { matcherHint, printReceived } from 'jest-matcher-utils';

import predicate from './predicate';

const passMessage = (received, before) => () =>
matcherHint('.not.toBeBeforeOrEqualTo', 'received', '') +
const passMessage = (utils, received, before) => () =>
utils.matcherHint('.not.toBeBeforeOrEqualTo', 'received', '') +
'\n\n' +
`Expected date to be before or equal to ${printReceived(before)} but received:\n` +
` ${printReceived(received)}`;
`Expected date to be before or equal to ${utils.printReceived(before)} but received:\n` +
` ${utils.printReceived(received)}`;

const failMessage = (received, before) => () =>
matcherHint('.toBeBeforeOrEqualTo', 'received', '') +
const failMessage = (utils, received, before) => () =>
utils.matcherHint('.toBeBeforeOrEqualTo', 'received', '') +
'\n\n' +
`Expected date to be before or equal to ${printReceived(before)} but received:\n` +
` ${printReceived(received)}`;
`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(date, before) };
return { pass: true, message: passMessage(this.utils, date, before) };
}

return { pass: false, message: failMessage(date, before) };
return { pass: false, message: failMessage(this.utils, date, before) };
}
22 changes: 10 additions & 12 deletions src/matchers/toBeBetween/index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
import { matcherHint, printReceived } from 'jest-matcher-utils';

import predicate from './predicate';

const passMessage = (received, startDate, endDate) => () =>
matcherHint('.not.toBeBetween', 'received', '') +
const passMessage = (utils, received, startDate, endDate) => () =>
utils.matcherHint('.not.toBeBetween', 'received', '') +
'\n\n' +
`Expected date to be between ${printReceived(startDate)} and ${printReceived(endDate)} but received:\n` +
` ${printReceived(received)}`;
`Expected date to be between ${utils.printReceived(startDate)} and ${utils.printReceived(endDate)} but received:\n` +
` ${utils.printReceived(received)}`;

const failMessage = (received, startDate, endDate) => () =>
matcherHint('.toBeBetween', 'received', '') +
const failMessage = (utils, received, startDate, endDate) => () =>
utils.matcherHint('.toBeBetween', 'received', '') +
'\n\n' +
`Expected date to be between ${printReceived(startDate)} and ${printReceived(endDate)} but received:\n` +
` ${printReceived(received)}`;
`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(date, startDate, endDate) };
return { pass: true, message: passMessage(this.utils, date, startDate, endDate) };
}

return { pass: false, message: failMessage(date, startDate, endDate) };
return { pass: false, message: failMessage(this.utils, date, startDate, endDate) };
}
18 changes: 8 additions & 10 deletions src/matchers/toBeBoolean/index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
import { matcherHint, printReceived } from 'jest-matcher-utils';

import predicate from './predicate';

const passMessage = received => () =>
matcherHint('.not.toBeBoolean', 'received', '') +
const passMessage = (utils, received) => () =>
utils.matcherHint('.not.toBeBoolean', 'received', '') +
'\n\n' +
'Expected value to not be of type boolean, received:\n' +
` ${printReceived(received)}`;
` ${utils.printReceived(received)}`;

const failMessage = received => () =>
matcherHint('.toBeBoolean', 'received', '') +
const failMessage = (utils, received) => () =>
utils.matcherHint('.toBeBoolean', 'received', '') +
'\n\n' +
'Expected value to be of type boolean, received:\n' +
` ${printReceived(received)}`;
` ${utils.printReceived(received)}`;

export function toBeBoolean(received) {
const pass = predicate(received);
if (pass) {
return { pass: true, message: passMessage(received) };
return { pass: true, message: passMessage(this.utils, received) };
}

return { pass: false, message: failMessage(received) };
return { pass: false, message: failMessage(this.utils, received) };
}
Loading

0 comments on commit 98ef8c7

Please sign in to comment.