Skip to the content.

API

Contents

createRunner(saga [, …args])

Creates a Runner.

Parameters:

Returns a new Runner instance.

Example:

const runner = createRunner(fetchUser, fetchUserAction);

Runner

The runner interface returned by createRunner().

Properties:

runner.map(effect, value [, …nextValues])

Maps an effect to a value.

Parameters:

Returns the current Runner.

Example:

createRunner(fetchUser, fetchUserAction)
  .map(call(getUser), userId)
  .run();

See also the special values:

runner.catch(error)

Catches silently an error thrown by the saga.

Parameters:

Returns the current Runner.

Example:

createRunner(fetchUser, fetchUserAction)
  .catch(/User not found/)
  .run();

runner.clone()

Clones the current runner instance.

Returns a copy of the current Runner.

Example:

const runnerWithCredentials = createRunner(fetchUser, fetchUserAction).map(
  select(getCredentials),
  credentials,
);

const runner1 = runnerWithCredentials.clone();

runner1
  .map(call(getUser), throwError(error))
  .should.select(getCredentials)
  .should.put({
    type: 'FETCH_USER_FAILURE',
    error: error.message,
  });

const runner2 = runnerWithCredentials.clone();

runner2
  .map(call(getUser), userId)
  .should.select(getCredentials)
  .should.put({
    type: 'FETCH_USER_SUCCESS',
    payload: { user },
  });

runner.run()

Runs the saga.

Returns a RunnerOutput.

Example:

const output = createRunner(fetchUser, fetchUserAction).run();

runner.should

Provides the Assertions.

throwError(error)

Throws an error from the saga when mapped as a value with runner.map().

Parameters:

Returns a ThrowError value.

Example:

createRunner(fetchUser, fetchUserAction)
  .map(getUser, throwError(new Error('Unable to get user')))
  .run();

finalize(value?)

Finalizes the saga when mapped as a value with runner.map().

Can be used to break an infinite loop.

Returns a Finalize value.

Example:

createRunner(fetchUser, fetchUserAction)
  .map(getUser, finalize())
  .run();

or by passing a mapped value to the effect:

createRunner(fetchUser, fetchUserAction)
  .map(getUser, finalize(user))
  .run();

RunnerOutput

The runner output interface returned by runner.run().

Properties:

Assertions

The assertions interface provided by runner.should.

Properties:

All the effect assertions about redux-saga effect creators are provided.

Since the assertions will run the saga, they should always be called after runner.map() or runner.catch().

runner.should.yield(effect)

Asserts that the saga yields an effect.

Parameters:

Returns the current Runner.

Example:

createRunner(fetchUser, fetchUserAction).should.yield(
  put({
    type: 'FETCH_USER_SUCCESS',
    payload: { user },
  }),
);

See effect assertions for a more convenient use.

runner.should.return(value)

Asserts that the saga returns a value.

Parameters:

Returns the current Runner.

Example:

createRunner(fetchUser, fetchUserAction).should.return(user);

runner.should.throw(error)

Asserts that the saga throws an error.

Parameters:

Returns the current Runner.

Example:

createRunner(fetchUser, fetchUserAction)
  .catch(Error)
  .should.throw(/User not found/);

runner.should.not

Negates the assertion that follows.

Examples:

createRunner(fetchUser, fetchUserAction).should.not.call(fetchProduct);

(effect assertions)

Allows to make effect assertions like runner.should.call(fn), runner.should.put(action), etc.

Same behavior as runner.should.yield(...) behind but in a more concise way.

Examples:

createRunner(fetchUser, fetchUserAction)
  .should.take('FETCH_USER')
  .should.select(getCredentials)
  .should.call(getUser, userId)
  .should.put({ type: 'FETCH_USER_SUCCESS' });