Giter Site home page Giter Site logo

sin / redux-saga-jest Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 0.0 36 KB

Helper library for testing redux sagas in jest.

Home Page: https://npmjs.com/package/redux-saga-jest

License: MIT License

JavaScript 100.00%
redux-saga jest test testing testing-tools sagas generators

redux-saga-jest's Introduction

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.

redux-saga-jest's People

Contributors

sin avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

redux-saga-jest's Issues

TypeError: saga.next is not a function

`import runSaga from 'redux-saga-jest';
import { loadEmailRequest } from 'store/ducks/authentication/actions';

describe('simpleGenerator', () => {
const { it, expect } = runSaga(
loadEmailRequest('[email protected]'),
);

it('should yield 1', ({ value }: any) => {
console.log(value);

expect(value).toBe();

});

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

it('then should finish', ({ value, done }: any) => {
console.log(value, done);

expect(value).toBeUndefined();
expect(done).toBe(true);

});
});`

Error
` FAIL tests/App-test.tsx
simpleGenerator
✕ should yield 1 (2ms)
✕ then should finish

● simpleGenerator › should yield 1

TypeError: saga.next is not a function

  at restartGenerator (node_modules/redux-saga-jest/dist/runSaga.js:12:53)
  at testMiddleware (node_modules/redux-saga-jest/dist/runSaga.js:37:20)

● simpleGenerator › then should finish

TypeError: saga.next is not a function

  at restartGenerator (node_modules/redux-saga-jest/dist/runSaga.js:12:53)
  at testMiddleware (node_modules/redux-saga-jest/dist/runSaga.js:37:20)`

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.