JSPM

jest-multiline-matchers

0.2.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 277
  • Score
    100M100P100Q91549F
  • License MIT

Package Exports

  • jest-multiline-matchers

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (jest-multiline-matchers) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

jest-multiline-matchers

Use multiline template strings as values for your expectations without worrying about indentation.

Install

yarn add --dev jest-multiline-matchers

or

npm install --dev jest-multiline-matchers

Usage

See explaination of the problems with multiline template strings and how they are handled in the README for dedent-preserving-indents which is the library used under the hood of these matchers.

toEqualMultiline

A version of toEqual that supports multiline template strings.

it(`returns a multiline string`, () => {
  const expected = `
           Here is a multiline string
             - It can be indented
               However you like.
        And it doesn't just check the first line`
  expect(f()).toEqualMultiline(expected)
})

The expected string will have its whitespace trimmed to the line with the least whitespace, so the above would become:

  Here is a multiline string
      - It can be indented
        However you like.
And it doesn't just check the first line

toThrowMultiline

A version of toThrow that supports multiline template strings. Note that unlike toThrow it assumes you will supply an expected value you want to compare to the Error's message.

it(`returns a multiline string`, () => {
  const expected = `
          Here is a an error message
            - It is formatted
              However you like`
  expect(() => f()).toThrowMultiline(expected)
})

Using the matchers with Jest

Add the following line to your jest.config.js:

{
  ...
  setupTestFrameworkScriptFile: `<rootDir>/src/__tests__/testHelpers/customMatchers.js`,
}

Then in customMatchers.js:

import toEqualSuccessWithValue from '../../matchers/validation/toEqualSuccessWithValue'
import toEqualFailureWithValue from '../../matchers/validation/toEqualFailureWithValue'

expect.extend({
  toEqualSuccessWithValue,
  toEqualFailureWithValue,
})