joystick
joystick copied to clipboard
Add support for test method to steps in actions
I want to avoid mocks and stubs when testing Joystick apps entirely. So far we're there, but in some situations (e.g., testing an action), it'd be nice to be able to just respond with test-specific calls/data. For example, if I'm testing an action that has a call to Stripe in it and I want to avoid calling Stripe (and having to write cleanup code in my test), I can just do this:
import joystick from "@joystick.js/node-canary";
const action_name = joystick.action(
"action_name",
{
input: {},
steps: {
step_name: {
run: () => {
return {};
},
},
another_step_name: {
test: () => {
// NOTE: Run test-specific code here. E.g., instead of mocking a third-party dep,
// just run this code only in test mode. Nice isolation from actual implementation.
},
run: () => {},
onError: (exception, action) => {
action.abort(exception.message);
},
},
},
run: async (input = {}, steps = {}, action = {}) => {
await steps.step_name();
return {
something: "123",
};
},
},
{
log_errors: process.env.NODE_ENV === 'development',
}
);
export default action_name;
Above, the another_step_name step has an additional test() method which receives the exact same args as the run() method, but it's only called when process.env.NODE_ENV === 'test' is true (and only if it exists). If test() is not defined, we'd just fallback to the run() method.