私は現在aを持っているサガを持っていて、yield all(...)
私が実際all()
に正しい関数で呼び出していることを確認するためにテストする方法を理解しようとしています。これが私が取り組んでいるものの簡略版です:
function* start() {
// I'd prefer not to start the status polling and the listening for
// the cancellation until after the request has been submitted, but
// I'm having trouble figuring out how to do that. So I'm just going
// to listen for 'em all up front here until I'm smarter.
yield all([
waitForCreateRequest(),
waitForPollStatus(),
waitForCancelRequest(),
])
}
function* waitForCreateRequest() {
while ( true ) {
try {
const { payload } = yield take('CREATE_REQUEST')
// ...
} catch ( error ) {
// ...
}
}
}
function* waitForPollStatus() {
while ( true ) {
try {
const { payload } = yield take('POLL_STATUS')
// ...
} catch ( error ) {
// ...
}
}
}
function* waitForCancelRequest() {
while ( true ) {
try {
yield take('CANCEL_REQUEST')
// ...
} catch ( error ) {
// ...
}
}
}
私が(Mochaとを使用してbdd-lazy-var
)書いたテストは次のようになります。
describe('MySaga', () => {
describe('*start()', () => {
subject(start())
it('calls `all()` with the correct functions', () => {
expect($subject.next().value).to.eql(all([
waitForSubmitExportRequest(),
waitForPollExportStatus(),
waitForCancelExportRequest(),
]))
})
})
})
出力はありません-ハングするだけです...そして「JavaScriptヒープがメモリ不足です」というエラーが表示されます。
私は、もしconsole.log($subject.next().value)
代わりに:
describe('MySaga', () => {
describe('*start()', () => {
subject(start())
it.only('foo', () => {
console.log($subject.next().value)
})
})
})
これは私が得るものです:
MySaga
*start()
{ '@@redux-saga/IO': true,
ALL:
[ GeneratorFunctionPrototype { _invoke: [Function: invoke] },
GeneratorFunctionPrototype { _invoke: [Function: invoke] },
GeneratorFunctionPrototype { _invoke: [Function: invoke] } ] }
✓ foo
だから私はここで何が起こっているのかわかりません。
数え切れないほどのグーグル検索は実際には何も役に立たなかったし、私が見つけた最も近いSO投稿(redux-sagaをjestを使ってすべての効果をテストする方法)も役に立たなかった。
あなたのstart
関数がジェネレーター関数ではないというのはタイプミスですか?
とにかく。次のstart
ように関数を書き直してみてください。
function* start() {
yield all([
call(waitForCreateRequest),
call(waitForPollStatus),
call(waitForCancelRequest),
])
}
これで、テストは次のようになります。
it('calls `all()` with the correct functions', () => {
expect($subject.next().value).to.eql(all([
call(waitForSubmitExportRequest),
call(waitForPollExportStatus),
call(waitForCancelExportRequest),
]))
})
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加