Skip to content

Commit

Permalink
Add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mattphillips committed Aug 7, 2018
1 parent 79d8b42 commit 91d0a74
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
19 changes: 18 additions & 1 deletion .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,22 @@
],
"imageSize": 100,
"commit": false,
"contributors": []
"contributors": [
{
"login": "mattphillips",
"name": "Matt Phillips",
"avatar_url": "https://avatars0.githubusercontent.com/u/5610087?v=4",
"profile": "http://mattphillips.io",
"contributions": [
"code",
"doc",
"example",
"ideas",
"infra",
"test",
"tool"
]
}
],
"repoType": "github"
}
66 changes: 66 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<div align="center">
<h1>jest-expect-message</h1>

πŸƒπŸ—―

Add custom message to Jest expects
</div>

Expand All @@ -14,10 +16,52 @@ Add custom message to Jest expects
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)
[![Roadmap](https://img.shields.io/badge/%F0%9F%93%94-roadmap-CD9523.svg?style=flat-square)](https://github.com/mattphillips/jest-expect-message/blob/master/docs/ROADMAP.md)
[![Examples](https://img.shields.io/badge/%F0%9F%92%A1-examples-ff615b.svg?style=flat-square)](https://github.com/mattphillips/jest-expect-message/blob/master/docs/EXAMPLES.md)

## Problem

In many testing libraries it is possible to supply a custom message for a given expectation, this is currently not
possible in Jest.

For example:

```js
test('returns 2 when adding 1 and 1', () => {
expect(1 + 1, 'Woah this should be 2!').toBe(3);
});
```

This will throw the following error in Jest:

```sh
Expect takes at most one argument.
```

## Solution

`jest-expect-message` allows you to call `expect` with a second argument of a `String` message.

For example the same test as above:

```js
test('returns 2 when adding 1 and 1', () => {
expect(1 + 1, 'Woah this should be 2!').toBe(3);
});
```

With `jest-expect-message` this will fail with your custom error message:

```sh
● returns 2 when adding 1 and 1

Custom message:
Woah this should be 2!

expect(received).toBe(expected) // Object.is equality

Expected: 3
Received: 2
```

## Installation

With npm:
Expand All @@ -32,11 +76,33 @@ yarn add -D jest-expect-message

## Setup

Add `jest-expect-message` to your Jest `setupTestFrameworkScriptFile` configuration.
[See for help](https://facebook.github.io/jest/docs/en/configuration.html#setuptestframeworkscriptfile-string)

```json
"jest": {
"setupTestFrameworkScriptFile": "jest-expect-message"
}
```

## Usage

- `expect(actual, message)`
- `actual`: The value you would normally pass into an `expect` to assert against with a given matcher.
- `message`: String, the custom message you want to be printed should the `expect` fail.

```js
test('returns 2 when adding 1 and 1', () => {
expect(1 + 1, 'Woah this should be 2!').toBe(3);
});
```

## Contributors

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- prettier-ignore -->
| [<img src="https://avatars0.githubusercontent.com/u/5610087?v=4" width="100px;"/><br /><sub><b>Matt Phillips</b></sub>](http://mattphillips.io)<br />[πŸ’»](https://github.com/mattphillips/jest-expect-message/commits?author=mattphillips "Code") [πŸ“–](https://github.com/mattphillips/jest-expect-message/commits?author=mattphillips "Documentation") [πŸ’‘](#example-mattphillips "Examples") [πŸ€”](#ideas-mattphillips "Ideas, Planning, & Feedback") [πŸš‡](#infra-mattphillips "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](https://github.com/mattphillips/jest-expect-message/commits?author=mattphillips "Tests") [πŸ”§](#tool-mattphillips "Tools") |
| :---: |
<!-- ALL-CONTRIBUTORS-LIST:END -->

## LICENSE
Expand Down

0 comments on commit 91d0a74

Please sign in to comment.