npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

redux-saga-jest

v0.1.0

Published

Helper library for testing redux sagas in jest.

Downloads

1,513

Readme

redux-saga-jest

Helper library for testing redux sagas in jest.

redux-saga-jest extends expect with useful matchers that let you easily validate saga effects, and binds your sagas to test (it alias is also available) and expect functions, providing an easy way to control saga's execution.

Installation

Install redux-saga-jest using yarn:

$ yarn add redux-saga-jest --dev

Or via npm:

$ npm install redux-saga-jest --save-dev

Getting Started

Let's start writing tests for a simple generator (yes, you can use redux-saga-jest to test plain generators, too):

function* simpleGenerator() {
    let msg, n = 1
    while (n < 3) {
        msg = yield msg || n
        n++
    }
}

You will need to import runSaga function from redux-saga-jest and call it with your generator's iterator as an argument. It'll return an object containing enhanced test, it and expect functions. redux-saga-jest will iterate over the generator when you call it and provide the yielded value and done flag as first argument in your test function.

import runSaga from 'redux-saga-jest'

describe('simpleGenerator', () => {
    const { it, expect } = runSaga(simpleGenerator())

    it('should yield 1', ({ value }) => {
        expect(value).toBe(1)
    })

    it('then should yield 2', ({ value }) => {
        expect(value).toBe(2)
    })

    it('then should finish', ({ value, done }) => {
        expect(value).toBeUndefined()
        expect(done).toBe(true)
    })
})

You can return from your test function to tell redux-saga-jest to send data to generator, terminate it, or signal an error, using special functions next, cancel and error respectively. These functions works much like redux-saga effects, returning an object that tells redux-saga-jest what to do next, rather than resuming execution of generator immediately.

import runSaga, { next } from 'redux-saga-jest'

describe('simpleGenerator', () => {
    const { it, expect } = runSaga(simpleGenerator())

    it('should yield 1', ({ value }) => {
        expect(value).toBe(1)
        return next('foo')
    })

    it('then should yield "foo"', ({ value }) => {
        expect(value).toBe('foo')
    })

    it('then should finish', ({ value, done }) => {
        expect(value).toBeUndefined()
        expect(done).toBe(true)
    })
})

Alternatively, use equivalent methods of expect. This will resume the generator immediately and allow you to assert yielded values using method chaining.

import runSaga from 'redux-saga-jest'

describe('simpleGenerator', () => {
    const { it, expect } = runSaga(simpleGenerator())

    it('should yield 1, then yield "bar", then finish', ({ value }) => {
        expect(value).toBe(1)
        expect.next('bar').value().toBe('bar')
        expect.next().done().toBe(true)
    })
})

For the most part testing sagas is no different from testings plain generators.

redux-saga effects are plain objects, so it's a common practice to test sagas using toEqual, but it's a little verbose and requires importing redux-saga effects in your tests. redux-saga-jest makes it more concise using custom matchers.

Consider the following saga:

import { put, take } from 'redux-saga/effects'

function* simpleSaga() {
    yield put({ type: 'FOO', payload: 1})
    const payload = yield take('BAR')
    yield put({ type: 'BAZ', payload })
}

You can test it like this:

import runSaga, { next } from 'redux-saga-jest'
import simpleSaga from './simpleSaga'

describe('simpleSaga', () => {
    const { it, expect } = runSaga(simpleSaga())

    it('should put an action "FOO" with payload 1', ({ value }) => {
        expect(value).put({ type: 'FOO', payload: 1 })
    })

    it('should take an action "BAR"', ({ value }) => {
        expect(value).take('BAR')
        return next(5)
    })

    it('should put an action "BAZ" with payload 5', ({ value }) => {
        expect(value).put({ type: 'BAZ', payload: 5 })
    })

    it('then should finish', ({ value, done }) => {
        expect(value).toBeUndefined()
        expect(done).toBe(true)
    })
})

License

Licensed under the MIT License.