Next Planet of Monkeys

    @querair/jest-expect-message

    1.0.3 • Public • Published

    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+

    Add this to package.json

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

    Or if you encountered with this error "We detected setupFilesAfterEnv in your package.json ...", undo setupFilesAfterEnv and create a file src/setupTests.js containing a requirement statement:

    // src/setupTests.js
    require("jest-expect-message")

    Jest v23-

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

    Usage

    • expect(actual, message, wrapper)
      • 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.
      • wrapper (optional): Function, if you want to wrap the message with a custom text instead of default "Custom message:...".
    test('returns 2 when adding 1 and 1', () => {
      expect(1 + 1, 'Woah this should be 2!').toBe(3);
    });
    
    // with a custom wrapper
    test('returns 2 when adding 1 and 1', () => {
      const wrapper = msg => `| Custom Message << ${msg} >> |`
      expect(1 + 1, 'Woah this should be 2!', wrapper).toBe(3);
    });

    Contributors


    Matt Phillips

    💻 📖 💡 🤔 🚇 ⚠️ 🔧

    Mohammad

    💻 📖 💡 🤔 ⚠️

    LICENSE

    MIT

    Install

    npm i @querair/jest-expect-message

    DownloadsWeekly Downloads

    3

    Version

    1.0.3

    License

    MIT

    Unpacked Size

    26.1 kB

    Total Files

    19

    Last publish

    Collaborators

    • mjafari98
    • quera