Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

take* should handle thrown errors #48

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 43 additions & 4 deletions test/take-helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,7 @@ it(testEvery, "should receive all actions", async () => {

function* root() {
const task = yield* spawn(() =>
takeEvery(
"ACTION",
(action) => worker("a1", "a2", action),
)
takeEvery("ACTION", (action) => worker("a1", "a2", action))
);
yield* take("CANCEL_WATCHER");
yield* task.halt();
Expand Down Expand Up @@ -113,3 +110,45 @@ it(testEvery, "should receive all actions", async () => {
["a1", "a2", 5],
]);
});

it(testEvery, "recover from throw", async () => {
const loop = 10;
const actual: string[][] = [];

function* root() {
const task = yield* spawn(() =>
takeEvery("ACTION", (action) => worker("a1", "a2", action))
);
yield* take("CANCEL_WATCHER");
yield* task.halt();
}

// deno-lint-ignore require-yield
function* worker(arg1: string, arg2: string, action: AnyAction) {
if (action.payload === 3) throw new Error("random error!");
actual.push([arg1, arg2, action.payload]);
}

const store = createStore({ initialState: {} });
const task = store.run(root);

for (let i = 1; i <= loop / 2; i += 1) {
store.dispatch({
type: "ACTION",
payload: i,
});
}

await task;

expect(actual).toEqual([
["a1", "a2", 1],
["a1", "a2", 2],
// this throws so we should miss this one
// ["a1", "a2", 3],
// TODO can we observe the throw in a test?
// takeEvery should continue and still handle follow events
["a1", "a2", 4],
["a1", "a2", 5],
]);
});
Loading