Skip to content

Commit

Permalink
fix: handle failed commands in tests correctly
Browse files Browse the repository at this point in the history
BREAKING CHANGE: The commands created by `ofSuccess` and `perform` do not throw anymore when the task fails. Maybe your tests need to be updated.
  • Loading branch information
atheck committed Jul 29, 2024
1 parent 1f33123 commit febb4c0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 26 deletions.
13 changes: 6 additions & 7 deletions src/Testing/execCmd.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { execCmd } from ".";
import type { Nullable } from "../Types";
import { cmd } from "../cmd";

type Message = { name: "Msg1" } | { name: "Msg2" } | { name: "Error" };
Expand Down Expand Up @@ -155,7 +154,7 @@ describe("execCmd", () => {
expect(messages).toStrictEqual([{ name: "Msg1" }]);
});

it("rejects for async perform, fail", async () => {
it("does not throw when async perform fails", async () => {
// arrange
const asyncFunc = async (): Promise<void> => {
throw new Error("fail");
Expand All @@ -164,10 +163,10 @@ describe("execCmd", () => {
const commands = cmd.ofPromise.perform(asyncFunc, (): Message => ({ name: "Msg1" }));

// act
const fail = async (): Promise<Nullable<Message>[]> => await execCmd(commands);
const messages = await execCmd(commands);

// assert
await expect(fail()).rejects.toThrow("fail");
expect(messages).toStrictEqual([null]);
});

it("resolves for attempt", async () => {
Expand Down Expand Up @@ -215,7 +214,7 @@ describe("execCmd", () => {
expect(messages).toStrictEqual([{ name: "Msg1" }]);
});

it("rejects for sync perform, fail", async () => {
it("does not throw if sync perform fails", async () => {
// arrange
const func = (): void => {
throw new Error("fail");
Expand All @@ -224,9 +223,9 @@ describe("execCmd", () => {
const commands = cmd.ofFunc.perform(func, (): Message => ({ name: "Msg1" }));

// act
const fail = async (): Promise<Nullable<Message>[]> => await execCmd(commands);
const messages = await execCmd(commands);

// assert
await expect(fail()).rejects.toThrow("fail");
expect(messages).toStrictEqual([null]);
});
});
4 changes: 2 additions & 2 deletions src/Testing/execCmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { Cmd, Nullable } from "../Types";
* @returns The array of processed messages.
*/
async function execCmd<TMessage>(...commands: (Cmd<TMessage> | undefined)[]): Promise<Nullable<TMessage>[]> {
const definedCommands = commands.filter((cmd) => cmd !== undefined) as Cmd<TMessage>[];
const definedCommands = commands.filter((cmd) => cmd !== undefined);
const callers = definedCommands.flatMap((cmd) =>
cmd.map(
async (currentCmd) =>
Expand All @@ -31,7 +31,7 @@ async function execCmd<TMessage>(...commands: (Cmd<TMessage> | undefined)[]): Pr
}

function execCmdWithDispatch<TMessage>(dispatch: Dispatch<TMessage>, ...commands: (Cmd<TMessage> | undefined)[]): void {
const definedCommands = commands.filter((cmd) => cmd !== undefined) as Cmd<TMessage>[];
const definedCommands = commands.filter((cmd) => cmd !== undefined);

for (const cmd of definedCommands) {
for (const sub of cmd) {
Expand Down
28 changes: 11 additions & 17 deletions src/cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const cmd = {

Promise.resolve(taskResult)
.then((result) => dispatch(ofSuccess(result)))
.catch((ex: Error) => dispatch(ofError(ex)));
.catch((ex: unknown) => dispatch(ofError(ex as Error)));
} catch (ex: unknown) {
dispatch(ofError(ex as Error));
}
Expand All @@ -70,9 +70,9 @@ const cmd = {

Promise.resolve(taskResult)
.then((result) => dispatch(ofSuccess(result)))
.catch(fallback);
} catch (ex: unknown) {
fallback(ex as Error);
.catch(() => fallback());
} catch {
fallback();
}
};

Expand All @@ -96,7 +96,7 @@ const cmd = {

Promise.resolve(taskResult)
.then(() => fallback?.())
.catch((ex: Error) => dispatch(ofError(ex)));
.catch((ex: unknown) => dispatch(ofError(ex as Error)));
} catch (ex: unknown) {
dispatch(ofError(ex as Error));
}
Expand Down Expand Up @@ -151,10 +151,8 @@ const cmd = {
const result = task(...args);

dispatch(ofSuccess(result));
} catch (ex: unknown) {
if (fallback) {
fallback(ex as Error);
}
} catch {
fallback?.();
}
};

Expand Down Expand Up @@ -208,7 +206,7 @@ const cmd = {
const bind = (dispatch: Dispatch<TSuccessMessage | TErrorMessage>): void => {
task(...args)
.then((result) => dispatch(ofSuccess(result)))
.catch((ex: Error) => dispatch(ofError(ex)));
.catch((ex: unknown) => dispatch(ofError(ex as Error)));
};

return [bind];
Expand All @@ -228,7 +226,7 @@ const cmd = {
const bind = (dispatch: Dispatch<TSuccessMessage>, fallback: FallbackHandler = defaultFallbackHandler): void => {
task(...args)
.then((result) => dispatch(ofSuccess(result)))
.catch(fallback);
.catch(() => fallback());
};

return [bind];
Expand All @@ -247,12 +245,8 @@ const cmd = {
): Cmd<TErrorMessage> {
const bind = (dispatch: Dispatch<TErrorMessage>, fallback?: FallbackHandler): void => {
task(...args)
.then(() => {
if (fallback) {
fallback();
}
})
.catch((ex: Error) => dispatch(ofError(ex)));
.then(() => fallback?.())
.catch((ex: unknown) => dispatch(ofError(ex as Error)));
};

return [bind];
Expand Down

0 comments on commit febb4c0

Please sign in to comment.