/fp-ts-jest-matchers

Jest matchers to nicely test code using fp-ts

Primary LanguageTypeScriptMIT LicenseMIT

Jest Matchers for fp-ts

Build and test npm

This library provides Jest matchers to nicely test code based on fp-ts. Currently it provides matchers for Option and Either.

Update December 2022

I'm not maintianing this library at the moment. It works perfectly fine, but I'm getting tired of the security alerts in the transitive dependencies. If you want to use this, feel free to fork or something like that. Cheers!

Installation

First add the dependency to your project:

yarn add --dev fp-ts-jest-matchers

Then make sure this package is loaded by Jest. If you have a file setupTests.ts, you can include it there:

import 'fp-ts-jest-matchers'

If not, you may need to add this to your Jest config:

"jest": {
  "setupFilesAfterEnv": ["fp-ts-jest-matchers"]
}

Usage

After installation, you can now write assertions like these in your test:

import * as O from 'fp-ts/lib/Option'
import * as E from 'fp-ts/lib/Either'
import * as A from 'fp-ts/lib/Array'

it('can test fp-ts options', () => {
    // Expect an option to be 'none':
    expect(O.none).toBeNone()

    // Expect an option to be 'some' with any value:
    expect(O.some('value')).toBeSome()

    // Expect an option to be 'some' with a specific value:
    expect(O.some('value')).toBeSome('value')

    // Use custom Eq for testing the value:
    expect(O.some(['a', 'b'])).toBeSome(['a', 'b'], A.getEq(eqString))
})

it('can test fp-ts eithers', () => {
    // Expect an either to be a left:
    expect(E.left('left value')).toBeLeft()

    // Expect an either to be a left with a specific value:
    expect(E.left('left value')).toBeLeft('left value')

    // Use custom Eq for testing the value:
    expect(E.left(['a', 'b'])).toBeLeft(['a', 'b'], A.getEq(eqString))

    // Expect an either to be a right:
    expect(E.right('right value')).toBeRight()

    // Expect an either to be a right with a specific value:
    expect(E.right('right value')).toBeRight('right value')

    // Use custom Eq for testing the value:
    expect(E.right(['a', 'b'])).toBeRight(['a', 'b'], A.getEq(eqString))
})