在生成器中测试抛出错误?

Testing throw error inside generator?

我正在尝试使用 Jest 和 Chai 测试生成器函数中抛出的错误:

    // function

    function * gen () {
        yield put({ type: TIME_OUT_LOGOUT })
        throw new Error('User has logged out')
    }

    //test

    let genFunc = gen()

    expect(genFunc().next).to.deep.equal(put({ type: TIME_OUT_LOGOUT }))

    expect(genFunc().next).to.throw(Error('User has logged out'));

但它不起作用。哪种测试方法才是正确的?

尝试将测试代码从 genFunc().next 更改为 genFunc.next().value


编辑:

断言应该是:

expect(genFunc.next().value).toEqual(put({type: TIME_OUT_LOGOUT})); expect(genFunc.next).toThrow(Error);

对于第二个断言 expect(() => genFunc.next()).toThrow(Error); 也可以,但是包装函数 () => genFunc.next() 是不必要的,因为 genFunc.next 不接受任何参数。