GithubHelp home page GithubHelp logo

seggewiss / jest-expect-message Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mattphillips/jest-expect-message

0.0 0.0 0.0 219 KB

Add custom message to Jest expects ๐Ÿƒ๐Ÿ—ฏ

Home Page: https://www.npmjs.com/package/jest-expect-message

License: MIT License

JavaScript 100.00%

jest-expect-message's Introduction

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

jest-expect-message's People

Contributors

mattphillips avatar coridyn avatar alexneo2003 avatar swa1ps avatar benjaminschwendner avatar jongio avatar madox2 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.