jfairbank / redux-saga-test-plan

Test Redux Saga with an easy plan.
http://redux-saga-test-plan.jeremyfairbank.com
MIT License
1.25k stars 127 forks source link

expectSaga doesn't work for Generator function in class #351

Closed dokurocker closed 4 years ago

dokurocker commented 4 years ago

Test class has generator method "gen".

export class Test {
  foo: string;
  constructor(foo) {
    this.foo = foo;
  }
  *gen() {
    console.log(this.foo);
  }
}

gen executes expectSaga

describe('Test', () => {
  test('Test.gen expectSaga', () => {
    const test = new Test('hello');
    expectSaga(test.gen).run();
  });
});

error occurred

TypeError: Cannot read property 'foo' of undefined

Is there any way to set this.foo ?

dokurocker commented 4 years ago

self-solving

export const parrot = (param) => {
  return param;
}
export class Test {
  foo: string;
  constructor(foo) {
    this.foo = foo;
  }
  *gen() {
    const _this = yield call(parrot, this);
    console.log(_this.foo);
  }
}
describe('Test', () => {
  test('Test.gen expectSaga', () => {
    const test = new Test('hello');
    expectSaga(test.gen)
      .provide([matchers.fn.call(parrot), test])
      .run();
  });
});

I want to know a better way... Thanks!