Coder Social home page Coder Social logo

fp-ts-jest-matchers's Introduction

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))
})

fp-ts-jest-matchers's People

Contributors

dependabot[bot] avatar svdo avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  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.