项目作者: jimbol

项目描述 :
Creates a runner for generators which can be used to test generators.
高级语言: JavaScript
项目地址: git://github.com/jimbol/generator-test-runner.git
创建时间: 2016-11-24T18:48:16Z
项目社区:https://github.com/jimbol/generator-test-runner

开源协议:MIT License

下载


The spirit child of this library is now available. Check out expect-gen!

Generator Test Runner

A generator test runner for any test runner. It was originally designed for use with redux-saga, but will work with any generators.

Goals

  • Switching the order of steps should be easy
  • Associate steps with their results
  • Changing step results should be easy

Install

Either

  1. npm install generator-test-runner --save-dev

or

  1. yarn add generator-test-runner --dev

Guide

Generator Test Runner allows you to test generators by

  1. Defining each step a generator will take
  2. Running the generator with each step
  3. Storing the yielded values of each step

Step By Step

Define Generator

Lets start with a generator. This one is a redux-saga effect.

  1. export function* getFoos(action) {
  2. const token = yield select(getToken);
  3. const response = yield call(fetchFoos, action.bars, token);
  4. yield put(handleResponse(response));
  5. }

Example runner

In our mocha tests, we can stub a run like so. Its good to see it all together up front.

  1. import genRunner from 'generator-test-runner';
  2. let action = {};
  3. let token = 'abc-123';
  4. const response = {};
  5. beforeEach(() => {
  6. run = genRunner(getFoos)
  7. .next('init', action)
  8. .next('getToken', token)
  9. .next('fetchFoos', response)
  10. .next('finish')
  11. .run();
  12. });

Init Generator Runner

First we pass the generator into the runner

  1. genRunner(getFoos)

Invoking Generator

The first step after invoking genRunner represents the invocation of the generator itself.

  1. genRunner(getFoos)
  2. .next('init', initialArg)

This is like calling getFoos(initialArgs).

You can also pass multiple arguments into this first next.

  1. genRunner(getFoos)
  2. .next('init', foo, bar, true)

Defining Steps

After that we define each step, giving each a name and result (if needed).

  1. runner.next('getToken', token)

The last step will be the final yield.

  1. runner.next('finish');

Running the Runner

Once you are ready to test, run the runner.

  1. run = runner.run();

Asserting

In our tests we can assert against pieces of the run

  1. expect(run.get('getToken').value).to.deep.equal(select(getToken));
  2. expect(run.get('fetchFoos').value).to.deep.equal(call(fetchFoos, action.bars, token));

Overwriting variables

Just pass in overrides for a given action when you call run.

  1. run = runner.run({
  2. getToken: 'invalid-token',
  3. });

Overriding the first next requires an array (in case you have multiple arguments!)

  1. run = runner.run({
  2. init: 'invalid-token',
  3. });

Matching

We can handle yields which match some conditions by using the match method. match takes three arguments; the name of the match, the match function, and the return value function.

For example:

  1. // In this example, all yields which meet the conditions of the `detectSelector` fn
  2. // Will use the `callSelector` result instead of the normal iterator flow (using `runner.next()`)
  3. // Returns true if step meets desired conditions, otherwise false
  4. const detectSelector = (step: YieldedStep): boolean => {
  5. return step.value && step.value.SELECT;
  6. };
  7. // Returns argument to be passed into `step.next`
  8. const callSelector = (step: YieldedStep): * => {
  9. return step.value.select.selector(); // call spy
  10. };
  11. sinon.stub(selectors, 'getToken').returns('fake-token');
  12. sinon.stub(selectors, 'barIdsSelected').returns([ 1, 2, 3 ]);
  13. run = genRunner(myGenerator)
  14. .match('select', detectSelector, callSelector)
  15. .next('start')
  16. .next('finish')
  17. .run();

run.get('select') will return an array of the matching yields

Forking

We can define a base generator runner, then fork using contexts’ beforeEach functions.

First, define the consistent steps, then build on the initial runner in subsequent beforeEach statements

  1. describe('my generator', () => {
  2. let initialRunner;
  3. beforeEach(() => {
  4. // step1, step2, and step3 always need to be run
  5. // they will run beforeEach test
  6. initialRunner = genRunner(myGenerator)
  7. .next('step1')
  8. .next('step2')
  9. .next('step3', false);
  10. });
  11. context('when step3 returns true', () => {
  12. let run;
  13. beforeEach(() => {
  14. run = initialRunner
  15. .next('returnsEarly')
  16. .run({ step3: true });
  17. });
  18. it('returns early', () => {
  19. expect(run.get('returnsEarly').done).to.be.true;
  20. });
  21. });
  22. context('when step3 returns false', () => {
  23. let run;
  24. beforeEach(() => {
  25. run = initialRunner
  26. .next('step4')
  27. .next('finish')
  28. .run();
  29. });
  30. it('finishes', () => {
  31. expect(run.get('finishes').done).to.be.true;
  32. });
  33. });
  34. });