• Stars
    star
    357
  • Rank 119,149 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 6 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Add custom message to Jest expects 🃏🗯

jest-expect-message

🃏🗯

Add custom message to Jest expects


Build Status Code Coverage version downloads MIT License PRs Welcome Roadmap Examples

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:

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:

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:

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:

  ● 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:

npm install --save-dev jest-expect-message

With yarn:

yarn add -D jest-expect-message

Setup

Add jest-expect-message to your Jest setupFilesAfterEnv configuration. See for help

Jest v24+

"jest": {
  "setupFilesAfterEnv": ["jest-expect-message"]
}

Jest v23-

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

If you have a custom setup file and want to use this library then add the following to your setup file.

import 'jest-expect-message';

Configure Typescript

Add the following entry to your tsconfig to enable Typescript support.

  "files": ["node_modules/jest-expect-message/types/index.d.ts"],

Example

Custom message example with typescript

Configure ESlint

"rules": {
  "jest/valid-expect": [
    "error",
    {
      "maxArgs": 2
    }
  ]
}

Usage

  • expect(actual, message, options?)
    • 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.
    • options: An optional object that controls what is shown as part of the custom message.
      • showPrefix: boolean: If false will not show the Custom message: prefix. Default: true
      • showMatcherMessage: boolean: If false will not show the matchers original error message. Default: true
      • showStack: boolean: If false will not show the matchers stack trace. Default: true
test('returns 2 when adding 1 and 1', () => {
  expect(1 + 1, 'Woah this should be 2!').toBe(3);
});
// ↓ ↓ ↓ ↓ ↓ ↓
/*
  ● 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

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

showPrefix: false

test('returns 2 when adding 1 and 1', () => {
  expect(1 + 1, 'Woah this should be 2!', { showPrefix: false }).toBe(3);
});
// ↓ ↓ ↓ ↓ ↓ ↓
/*
  ● returns 2 when adding 1 and 1

    Woah this should be 2!

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

    Expected: 3
    Received: 2

  1 |   test('returns 2 when adding 1 and 1', () => {
> 2 |     expect(1 + 1, 'Woah this should be 2!', { showPrefix: false }).toBe(3);
    |                                                                    ^
  3 |   });
*/

showMatcherMessage: false

test('returns 2 when adding 1 and 1', () => {
  expect(1 + 1, 'Woah this should be 2!', { showMatcherMessage: false }).toBe(3);
});
// ↓ ↓ ↓ ↓ ↓ ↓
/*
  ● returns 2 when adding 1 and 1

    Custom message:
      Woah this should be 2!

  1 |   test('returns 2 when adding 1 and 1', () => {
> 2 |     expect(1 + 1, 'Woah this should be 2!', { showMatcherMessage: false }).toBe(3);
    |                                                                            ^
  3 |   });
*/

showStack: false

test('returns 2 when adding 1 and 1', () => {
  expect(1 + 1, 'Woah this should be 2!', { showStack: false }).toBe(3);
});
// ↓ ↓ ↓ ↓ ↓ ↓
/*
  ● 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
*/

LICENSE

MIT

More Repositories

1

deep-object-diff

Deep diffs two objects, including nested structures of arrays and objects, and returns the difference. ❄️
JavaScript
1,006
star
2

babel-plugin-console

Babel Plugin that adds useful build time console functions 🎮
JavaScript
295
star
3

jest-chain

Chain Jest matchers together to create one powerful assertion 🃏⛓
JavaScript
274
star
4

babel-jest-assertions

Babel Plugin that adds safety to your tests by verifying assertions are actually ran 🃏⁉️
JavaScript
96
star
5

jest-each

A parameterised testing library for Jest. https://www.npmjs.com/package/jest-each 🏃
JavaScript
92
star
6

babel-plugin-gwt

Data Driven Testing babel plugin inspired by Groovy's Spock framework 🖖
JavaScript
14
star
7

react-point-break

React CSS media queries with breakpoint render props Component and Provider. 🔫
JavaScript
7
star
8

oss-box

Open source project boilerplate generator 📦
JavaScript
4
star
9

ts-prelude

TypeScript
3
star
10

io-ts-graphql-codegen

TypeScript
2
star
11

flow-data-structures

Basic data structures implements using Flowtype
JavaScript
2
star
12

redux-identity

A simple redux reducer that returns the given state
JavaScript
1
star
13

jest-workspace-projects

JavaScript
1
star
14

trait-contract

Utility to build a Trait (Interface) for a given Type and supply an implementation which is validated against the Trait contract
JavaScript
1
star
15

eslint-plugin-jest-extended

ESLint plugin for jest-extended matchers
JavaScript
1
star
16

data-structures

Groovy
1
star
17

reason-refs

C++
1
star
18

es6-prototype-boilerplate

Boilerplate setup for quick prototyping with es6 using budo to build and mocha for testing.
JavaScript
1
star
19

react-native-counter

JavaScript
1
star