npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jest-expect-message

v1.1.3

Published

Add custom message to Jest expects

Downloads

1,379,180

Readme

🃏🗯

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