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

expect-gen

v0.2.5

Published

Assertion library for unit testing JS generators. Works well with redux-saga. Allows snapshot testing.

Downloads

2,487

Readme

Expect Gen

As in "expect generator...". An assertion / snapshot library for testing iterators and generators. It was designed for, and works particularly well with, redux-saga but can be used for anything that uses generators.

Example

function* myEffect(fakeAction) {
  const fooIds = yield select(fooIds);
  yield put(foosLoading());
  const results = yield call(apiFetch, options);
}

import expectGen from 'expect-gen';

it('runs effect with fakeFooIds and fakeResults', () => {
  expectGen(myEffect, fakeAction)
    // asserts step yields first `select(fooIds)`
    .yields(
      select(fooIds),
      // `fakeFooIds` gets pass into the following `next`
      fakeFooIds
    )
    .yields(
      put(foosLoading())
    )
    .yields(
      call(apiFetch, options),
      fakeResults
    )
    .finishes()
    .run();
});

it('runs effect with fakeFooIds and fakeResults', () => {
  const snapshot = expectGen(myEffect, fakeAction)
    // Doesn't run assertion for `next`
    .next(fakeFooIds)
    .next()
    .yields(
      call(apiFetch, options),
      fakeResults
    )
    .finishes()
    .toJSON();

  expect(snapshot).toMatchSnapshot();
});

API

expectGen

expectGen(generator, [...args])

  • Calls the first argument with the rest of the arguments
  • Returns instance of StepManager

StepManager

expectGen returns an instance StepManager. It gives us a set of chainable methods:

yields

expectGen(generator, [...args])
  .yields(expectedValue, [result])
  • Runs an assertion that iterator will yield the expectedValue
  • Passes result into generator's next method
  • Returns same instance StepManager

next

expectGen(generator, [...args])
  .next([result])
  • Passes result into generator's next method
  • Makes no assertion
  • Returns same instance StepManager

throws

expectGen(generator, [...args])
  .throws(error)
  • Throws error into generator
  • Checks that iterator throws an uncaught exception
  • Returns same instance StepManager

catches

expectGen(generator, [...args])
  .catches(error, [expectedValue])
  • Throws error into generator
  • Runs an assertion that thrown iterator will yield the expectedValue
  • Returns same instance StepManager

catchesAndFinishes

expectGen(generator, [...args])
  .catchesAndFinishes(error, [result])
  • Throws error into generator
  • Asserts that generator finishes with result
  • Returns same instance StepManager

finishes

expectGen(generator, [...args])
  ...
  .finishes([result])
  • Asserts that generator finishes with result
  • Returns same instance StepManager

run

expectGen(generator, [...args])
  ...
  .run()
  • Executes all assertions
  • Returns results of all generator steps as an array

toJSON

expectGen(generator, [...args])
  ...
  .toJSON()
  • Convert results from run() into a JSON object

Roadmap

Expect Gen is heading towards v1. The following items still need to go in before then. Please open issues for other v1 requirements you find.

v1.0.0

Previous Version

Expect Gen is a second generation version of generator-test-runner. Many of the concepts used here originated there.