How to use StartExecution method in redwood

Best JavaScript code snippet using redwood

stepfunctions.test.js

Source:stepfunctions.test.js Github

copy

Full Screen

1const Sfn = require('../lib/stepfunctions');2describe('Stepfunctions', () => {3 it('validates states definition via asl-validator', async () => {4 const message = 'data should NOT have additional properties';5 expect(() => new Sfn({ StateMachine: { willThrow: true } })).toThrow(6 message,7 );8 });9 it('can run a simple task with a mock', async () => {10 const sm = new Sfn({ StateMachine: require('./steps/simple.json') });11 const mockfn = jest.fn((input) => input.test === 1);12 sm.bindTaskResource('Test', mockfn);13 await sm.startExecution({ test: 1 });14 expect(mockfn).toHaveBeenCalled();15 expect(sm.getExecutionResult()).toBe(true);16 });17 it('can run a simple task with a spy', async () => {18 const sm = new Sfn({ StateMachine: require('./steps/simple.json') });19 const spy = jest.spyOn(sm, 'step');20 await sm.startExecution({ test: 1 });21 expect(spy).toHaveBeenCalled();22 });23 it('can run a simple task and get the result', async () => {24 const sm = new Sfn({ StateMachine: require('./steps/simple.json') });25 const mockfn = jest.fn((input) => input.test !== 1);26 sm.bindTaskResource('Test', mockfn);27 await sm.startExecution({ test: 1 });28 expect(sm.getExecutionResult()).toBe(false);29 });30 it('can modify input with InputPath and Parameters', async () => {31 const sm = new Sfn({ StateMachine: require('./steps/input.json') });32 const mockfn = jest.fn((input) => ({33 comment: 'Example for Parameters.',34 product: {35 details: {36 color: 'blue',37 size: 'small',38 material: 'cotton',39 },40 availability: 'in stock',41 sku: '2317',42 cost: input,43 },44 }));45 const mock2fn = jest.fn((input) => input);46 sm.bindTaskResource('Test', mockfn);47 sm.bindTaskResource('Test2', mock2fn);48 await sm.startExecution({ value: '$23' });49 expect(mockfn).toHaveBeenCalled();50 expect(sm.getExecutionResult()).toEqual(51 expect.objectContaining({52 MyDetails: {53 size: 'small',54 exists: 'in stock',55 StaticValue: 'foo',56 },57 }),58 );59 });60 it('can modify input with Context', async () => {61 const sm = new Sfn({ StateMachine: require('./steps/context.json') });62 const mockfn = jest.fn((input) => input);63 sm.bindTaskResource('Test', mockfn);64 await sm.startExecution({ value: 'test' });65 expect(mockfn).toHaveBeenCalled();66 expect(sm.getExecutionResult()).toEqual(67 expect.objectContaining({68 MyDetails: {69 Execution: expect.any(String),70 Retries: expect.any(Number),71 Name: expect.any(String),72 },73 }),74 );75 });76 it('can modify output with ResultPath and OutputPath', async () => {77 const sm = new Sfn({ StateMachine: require('./steps/output.json') });78 const mockfn = jest.fn((input) => 'Hello, ' + input.who + '!');79 sm.bindTaskResource('Test', mockfn);80 await sm.startExecution({81 comment: 'An input comment.',82 data: {83 val1: 23,84 val2: 17,85 },86 extra: 'foo',87 lambda: {88 who: 'AWS Step Functions',89 },90 });91 expect(mockfn).toHaveBeenCalled();92 expect(sm.getExecutionResult()).toEqual(93 expect.objectContaining({94 val1: 23,95 val2: 17,96 lambdaresult: 'Hello, AWS Step Functions!',97 }),98 );99 });100 describe('Task', () => {101 it('can run a bound task', async () => {102 const sm = new Sfn({ StateMachine: require('./steps/task.json') });103 const mockfn = jest.fn((input) => input.test === 1);104 sm.bindTaskResource('Test', mockfn);105 await sm.startExecution({ test: 1 });106 expect(mockfn).toHaveBeenCalled();107 expect(sm.getExecutionResult()).toBe(true);108 });109 it('can run if there are no bound task', async () => {110 const sm = new Sfn({ StateMachine: require('./steps/task.json') });111 await sm.startExecution({ test: 1 });112 expect(sm.getExecutionResult()).toEqual(113 expect.objectContaining({ test: 1 }),114 );115 });116 it('can bind multiple tasks', async () => {117 const sm = new Sfn({ StateMachine: require('./steps/tasks.json') });118 const mockfn = jest.fn((input) => ({ test: input.test + 1 }));119 const mockfn1 = jest.fn((input) => ({ test: input.test + 2 }));120 const mockfn2 = jest.fn((input) => input.test + 3);121 sm.bindTaskResource('Test', mockfn);122 sm.bindTaskResource('Test1', mockfn1);123 sm.bindTaskResource('Test2', mockfn2);124 await sm.startExecution({ test: 1 });125 expect(mockfn).toHaveBeenCalled();126 expect(sm.getExecutionResult()).toBe(7);127 });128 });129 describe('Map', () => {130 it('starts with a simple Map', async () => {131 const sm = new Sfn({ StateMachine: require('./steps/map.json') });132 const mockfn = jest.fn((input) => input);133 const mockMapfn = jest.fn((input) => ({ test: input.test + 1 }));134 sm.bindTaskResource('Test', mockfn);135 sm.bindTaskResource('Mapper', mockMapfn);136 await sm.startExecution([{ test: 1 }, { test: 2 }]);137 expect(mockfn).toHaveBeenCalled();138 expect(sm.getExecutionResult()).toEqual(139 expect.arrayContaining([{ test: 2 }]),140 );141 expect(sm.getExecutionResult()).toEqual(142 expect.arrayContaining([{ test: 3 }]),143 );144 });145 it('aggregates from a Task to a Map', async () => {146 const sm = new Sfn({ StateMachine: require('./steps/map-task.json') });147 const mockfn = jest.fn((input) => input);148 const mockMapfn = jest.fn((input) => ({ test: input.test + 1 }));149 sm.bindTaskResource('Mapper', mockMapfn);150 sm.bindTaskResource('Test', mockfn);151 await sm.startExecution([{ test: 1 }, { test: 2 }]);152 expect(mockfn).toHaveBeenCalled();153 expect(sm.getExecutionResult()).toEqual(154 expect.arrayContaining([{ test: 2 }]),155 );156 expect(sm.getExecutionResult()).toEqual(157 expect.arrayContaining([{ test: 3 }]),158 );159 });160 it('supports nested Map', async () => {161 const sm = new Sfn({ StateMachine: require('./steps/map-nested.json') });162 const mockfn = jest.fn((input) => input);163 const mockMapfn = jest.fn((input) => [164 { test: input.test + 1 },165 { test: input.test + 2 },166 ]);167 const mockMapdfn = jest.fn((input) => ({ test: input.test + 1 }));168 const mockLastfn = jest.fn((input) => input);169 sm.bindTaskResource('Test', mockfn);170 sm.bindTaskResource('Mapper', mockMapfn);171 sm.bindTaskResource('Mapped', mockMapdfn);172 sm.bindTaskResource('Last', mockLastfn);173 await sm.startExecution([{ test: 1 }, { test: 2 }]);174 expect(mockfn).toHaveBeenCalled();175 expect(sm.getExecutionResult()).toEqual(176 expect.arrayContaining([[{ test: 3 }, { test: 4 }]]),177 );178 expect(sm.getExecutionResult()).toEqual(179 expect.arrayContaining([[{ test: 4 }, { test: 5 }]]),180 );181 });182 it('supports ItemsPath', async () => {183 const definition = require('./steps/map.json');184 definition.States.Map.ItemsPath = '$.items';185 const sm = new Sfn({ StateMachine: definition });186 const mockfn = jest.fn((input) => input);187 const mockMapfn = jest.fn((input) => ({ test: input.test + 1 }));188 sm.bindTaskResource('Test', mockfn);189 sm.bindTaskResource('Mapper', mockMapfn);190 await sm.startExecution({ items: [{ test: 1 }, { test: 2 }] });191 expect(mockfn).toHaveBeenCalled();192 expect(sm.getExecutionResult()).toEqual(193 expect.arrayContaining([{ test: 2 }]),194 );195 expect(sm.getExecutionResult()).toEqual(196 expect.arrayContaining([{ test: 3 }]),197 );198 });199 it('can modify input with Context within Map', async () => {200 const definition = require('./steps/map-context.json');201 const sm = new Sfn({ StateMachine: definition });202 const mockfn = jest.fn((input) => input);203 const mockMapfn = jest.fn((input) => input);204 sm.bindTaskResource('Test', mockfn);205 sm.bindTaskResource('Mapper', mockMapfn);206 await sm.startExecution([{ who: 'bob' }, { who: 'meg' }, { who: 'joe' }]);207 expect(mockfn).toHaveBeenCalled();208 expect(sm.getExecutionResult()).toEqual(209 expect.arrayContaining([210 { ContextIndex: 1, ContextValue: { who: 'meg' } },211 ]),212 );213 });214 it.skip('can run sequential tasks while limiting concurrency using MaxConcurrency', async () => {});215 });216 describe('Parallel', () => {217 it('can run multiple tasks at once', async () => {218 const sm = new Sfn({ StateMachine: require('./steps/parallel.json') });219 const mock1Fn = jest.fn((input) => input[0] + input[1]);220 const mock2Fn = jest.fn((input) => input[0] - input[1]);221 sm.bindTaskResource('ParallelTask1', mock1Fn);222 sm.bindTaskResource('ParallelTask2', mock2Fn);223 await sm.startExecution([1, 1]);224 expect(mock1Fn).toHaveBeenCalled();225 expect(mock2Fn).toHaveBeenCalled();226 expect(sm.getExecutionResult()).toEqual(expect.arrayContaining([2, 0]));227 });228 it('can run nested parallels', async () => {229 const sm = new Sfn({230 StateMachine: require('./steps/parallel-nested.json'),231 });232 const mock1Fn = jest.fn((input) => input[0] + input[1]);233 const mock2Fn = jest.fn((input) => input[0] + input[1]);234 const mock3Fn = jest.fn((input) => input + 1);235 const mock4Fn = jest.fn((input) => input + 2);236 sm.bindTaskResource('ParallelTask1', mock1Fn);237 sm.bindTaskResource('ParallelTask2', mock2Fn);238 sm.bindTaskResource('ParallelTask3', mock3Fn);239 sm.bindTaskResource('ParallelTask4', mock4Fn);240 await sm.startExecution([1, 1]);241 expect(mock1Fn).toHaveBeenCalled();242 expect(mock2Fn).toHaveBeenCalled();243 expect(mock3Fn).toHaveBeenCalled();244 expect(mock4Fn).toHaveBeenCalled();245 expect(sm.getExecutionResult()).toEqual(246 expect.arrayContaining([[3, 4], 2]),247 );248 });249 it.skip('can run multiple tasks and aggregate the results into ResultPath', async () => {});250 it.skip('can run multiple tasks that respects maxConcurrency', async () => {});251 });252 describe('Pass', () => {253 it('can continue to another task via Pass', async () => {254 const sm = new Sfn({ StateMachine: require('./steps/pass.json') });255 const mockFn = jest.fn();256 sm.on('PassStateEntered', mockFn);257 await sm.startExecution();258 expect(mockFn).toHaveBeenCalled();259 });260 });261 describe('Fail', () => {262 it('throws a full stop and an error when a fail state is encountered', async () => {263 const sm = new Sfn({ StateMachine: require('./steps/fail.json') });264 const mockFn = jest.fn();265 sm.on('FailStateEntered', mockFn);266 await expect(sm.startExecution()).rejects.toThrowError(/TaskFailed/);267 expect(mockFn).toHaveBeenCalled();268 });269 });270 describe('Succeed', () => {271 it('stops the statemachine after receiving a succeeding option', async () => {272 const sm = new Sfn({ StateMachine: require('./steps/succeed.json') });273 const succeedingFn = jest.fn(() => ({ value: 0 }));274 const mockFn = jest.fn();275 sm.bindTaskResource('Test', succeedingFn);276 sm.on('SucceedStateEntered', mockFn);277 await sm.startExecution();278 expect(mockFn).toHaveBeenCalled();279 expect(succeedingFn).toHaveBeenCalled();280 });281 it('fails the statemachine after receiving a failing option', async () => {282 const sm = new Sfn({ StateMachine: require('./steps/succeed.json') });283 const succeedingFn = jest.fn(() => ({ value: 1 }));284 const mockFn = jest.fn();285 sm.bindTaskResource('Test', succeedingFn);286 sm.on('FailStateEntered', mockFn);287 await expect(sm.startExecution()).rejects.toThrowError(/TaskFailed/);288 expect(mockFn).toHaveBeenCalled();289 expect(succeedingFn).toHaveBeenCalled();290 });291 });292 // TODO: maybe use jest.fakeTimers293 describe('Wait', () => {294 it('can wait for 1 second', async () => {295 const sm = new Sfn({ StateMachine: require('./steps/wait.json') });296 const mockFn = jest.fn();297 sm.bindTaskResource('Final', mockFn);298 await sm.startExecution({ value: 'Wait' });299 expect(mockFn).toHaveBeenCalled();300 }, 1500);301 it('can wait for 1 second using SecondsPath', async () => {302 const sm = new Sfn({ StateMachine: require('./steps/wait.json') });303 const mockFn = jest.fn();304 sm.bindTaskResource('Final', mockFn);305 await sm.startExecution({ value: 'WaitPath', until: 1 });306 expect(mockFn).toHaveBeenCalled();307 }, 1500);308 it('can wait until a specified time', async () => {309 const sm = new Sfn({ StateMachine: require('./steps/wait.json') });310 const mockFn = jest.fn();311 sm.bindTaskResource('Final', mockFn);312 await sm.startExecution({ value: 'WaitUntil' });313 expect(mockFn).toHaveBeenCalled();314 });315 it('can wait until a specified time using TimestampPath', async () => {316 const sm = new Sfn({ StateMachine: require('./steps/wait.json') });317 const mockFn = jest.fn();318 sm.bindTaskResource('Final', mockFn);319 // 1 second in the future320 const date = new Date();321 await sm.startExecution({322 value: 'WaitUntilPath',323 until: date.setSeconds(date.getSeconds() + 1),324 });325 expect(mockFn).toHaveBeenCalled();326 }, 2000);327 it.skip('can abort a running statemachine', () => {});328 it.skip('can expect a timeout when a wait step is running for a long time', () => {});329 });330 describe('Choice', () => {331 it('can test against boolean', async () => {332 const sm = new Sfn({ StateMachine: require('./steps/choice.json') });333 const mockFn = jest.fn((input) => input);334 sm.bindTaskResource('Test', mockFn);335 await sm.startExecution({ param1: true });336 expect(mockFn).toHaveBeenCalled();337 expect(sm.getExecutionResult()).toEqual(338 expect.objectContaining({ param1: true }),339 );340 });341 it('can test equality against numbers', async () => {342 const sm = new Sfn({ StateMachine: require('./steps/choice.json') });343 const mockFn = jest.fn((input) => input);344 sm.bindTaskResource('Test', mockFn);345 await sm.startExecution({ param1: 0 });346 expect(mockFn).toHaveBeenCalled();347 expect(sm.getExecutionResult()).toEqual(348 expect.objectContaining({ param1: 0 }),349 );350 });351 it('can test equality against strings', async () => {352 const sm = new Sfn({ StateMachine: require('./steps/choice.json') });353 const mockFn = jest.fn((input) => input);354 sm.bindTaskResource('Test', mockFn);355 await sm.startExecution({ param1: 'test' });356 expect(mockFn).toHaveBeenCalled();357 expect(sm.getExecutionResult()).toEqual(358 expect.objectContaining({ param1: 'test' }),359 );360 });361 it('can test equality against timestamps', async () => {362 const sm = new Sfn({ StateMachine: require('./steps/choice.json') });363 const mockFn = jest.fn((input) => input);364 sm.bindTaskResource('Test', mockFn);365 await sm.startExecution({ param1: '2001-01-01T12:00:00Z' });366 expect(mockFn).toHaveBeenCalled();367 expect(sm.getExecutionResult()).toEqual(368 expect.objectContaining({ param1: '2001-01-01T12:00:00Z' }),369 );370 });371 it('can test greater than against numbers', async () => {372 const sm = new Sfn({ StateMachine: require('./steps/choice.json') });373 const mockFn = jest.fn((input) => input);374 sm.bindTaskResource('Test', mockFn);375 await sm.startExecution({ param1: 1 });376 expect(mockFn).toHaveBeenCalled();377 expect(sm.getExecutionResult()).toEqual(378 expect.objectContaining({ param1: 1 }),379 );380 });381 it('can test greater than against strings', async () => {382 const sm = new Sfn({ StateMachine: require('./steps/choice.json') });383 const mockFn = jest.fn((input) => input);384 sm.bindTaskResource('Test', mockFn);385 await sm.startExecution({ param4: 'tester' });386 expect(mockFn).toHaveBeenCalled();387 expect(sm.getExecutionResult()).toEqual(388 expect.objectContaining({ param4: 'tester' }),389 );390 });391 it('can test greater than against timestamps', async () => {392 const sm = new Sfn({ StateMachine: require('./steps/choice.json') });393 const mockFn = jest.fn((input) => input);394 sm.bindTaskResource('Test', mockFn);395 await sm.startExecution({ param1: '2001-02-01T12:00:00Z' });396 expect(mockFn).toHaveBeenCalled();397 expect(sm.getExecutionResult()).toEqual(398 expect.objectContaining({ param1: '2001-02-01T12:00:00Z' }),399 );400 });401 it('can test greater than equals against numbers', async () => {402 const sm = new Sfn({ StateMachine: require('./steps/choice.json') });403 const mockFn = jest.fn((input) => input);404 sm.bindTaskResource('Test', mockFn);405 await sm.startExecution({ param1: 10 });406 expect(mockFn).toHaveBeenCalled();407 expect(sm.getExecutionResult()).toEqual(408 expect.objectContaining({ param1: 10 }),409 );410 await sm.startExecution({ param1: 11 });411 expect(mockFn).toHaveBeenCalled();412 expect(sm.getExecutionResult()).toEqual(413 expect.objectContaining({ param1: 11 }),414 );415 });416 it('can test greater than equals against strings', async () => {417 const sm = new Sfn({ StateMachine: require('./steps/choice.json') });418 const mockFn = jest.fn((input) => input);419 sm.bindTaskResource('Test', mockFn);420 await sm.startExecution({ param1: 'tester' });421 expect(mockFn).toHaveBeenCalled();422 expect(sm.getExecutionResult()).toEqual(423 expect.objectContaining({ param1: 'tester' }),424 );425 await sm.startExecution({ param1: 'testers' });426 expect(mockFn).toHaveBeenCalled();427 expect(sm.getExecutionResult()).toEqual(428 expect.objectContaining({ param1: 'testers' }),429 );430 });431 it('can test greater than equals against timestamps', async () => {432 const sm = new Sfn({ StateMachine: require('./steps/choice.json') });433 const mockFn = jest.fn((input) => input);434 sm.bindTaskResource('Test', mockFn);435 await sm.startExecution({ param1: '2001-02-01T12:00:00Z' });436 expect(mockFn).toHaveBeenCalled();437 expect(sm.getExecutionResult()).toEqual(438 expect.objectContaining({ param1: '2001-02-01T12:00:00Z' }),439 );440 await sm.startExecution({ param1: '2001-02-02T12:00:00Z' });441 expect(mockFn).toHaveBeenCalled();442 expect(sm.getExecutionResult()).toEqual(443 expect.objectContaining({ param1: '2001-02-02T12:00:00Z' }),444 );445 });446 it('can test less than against numbers', async () => {447 const sm = new Sfn({ StateMachine: require('./steps/choice.json') });448 const mockFn = jest.fn((input) => input);449 sm.bindTaskResource('Test', mockFn);450 await sm.startExecution({ param3: -1 });451 expect(mockFn).toHaveBeenCalled();452 expect(sm.getExecutionResult()).toEqual(453 expect.objectContaining({ param3: -1 }),454 );455 });456 it('can test less than against strings', async () => {457 const sm = new Sfn({ StateMachine: require('./steps/choice.json') });458 const mockFn = jest.fn((input) => input);459 sm.bindTaskResource('Test', mockFn);460 await sm.startExecution({ param3: 'tes' });461 expect(mockFn).toHaveBeenCalled();462 expect(sm.getExecutionResult()).toEqual(463 expect.objectContaining({ param3: 'tes' }),464 );465 });466 it('can test less than against timestamps', async () => {467 const sm = new Sfn({ StateMachine: require('./steps/choice.json') });468 const mockFn = jest.fn((input) => input);469 sm.bindTaskResource('Test', mockFn);470 await sm.startExecution({ param3: '2001-01-01T11:00:00Z' });471 expect(mockFn).toHaveBeenCalled();472 expect(sm.getExecutionResult()).toEqual(473 expect.objectContaining({ param3: '2001-01-01T11:00:00Z' }),474 );475 });476 it('can test less than equals against numbers', async () => {477 const sm = new Sfn({ StateMachine: require('./steps/choice.json') });478 const mockFn = jest.fn((input) => input);479 sm.bindTaskResource('Test', mockFn);480 await sm.startExecution({ param2: 9 });481 expect(mockFn).toHaveBeenCalled();482 expect(sm.getExecutionResult()).toEqual(483 expect.objectContaining({ param2: 9 }),484 );485 await sm.startExecution({ param2: 10 });486 expect(mockFn).toHaveBeenCalled();487 expect(sm.getExecutionResult()).toEqual(488 expect.objectContaining({ param2: 10 }),489 );490 });491 it('can test less than equals against strings', async () => {492 const sm = new Sfn({ StateMachine: require('./steps/choice.json') });493 const mockFn = jest.fn((input) => input);494 sm.bindTaskResource('Test', mockFn);495 await sm.startExecution({ param2: 'tes' });496 expect(mockFn).toHaveBeenCalled();497 expect(sm.getExecutionResult()).toEqual(498 expect.objectContaining({ param2: 'tes' }),499 );500 await sm.startExecution({ param2: 'test' });501 expect(mockFn).toHaveBeenCalled();502 expect(sm.getExecutionResult()).toEqual(503 expect.objectContaining({ param2: 'test' }),504 );505 });506 it('can test less than equals against timestamps', async () => {507 const sm = new Sfn({ StateMachine: require('./steps/choice.json') });508 const mockFn = jest.fn((input) => input);509 sm.bindTaskResource('Test', mockFn);510 await sm.startExecution({ param2: '2001-02-01T12:00:00Z' });511 expect(mockFn).toHaveBeenCalled();512 expect(sm.getExecutionResult()).toEqual(513 expect.objectContaining({ param2: '2001-02-01T12:00:00Z' }),514 );515 await sm.startExecution({ param2: '2001-02-01T11:00:00Z' });516 expect(mockFn).toHaveBeenCalled();517 expect(sm.getExecutionResult()).toEqual(518 expect.objectContaining({ param2: '2001-02-01T11:00:00Z' }),519 );520 });521 it('can test a simple AND comparison', async () => {522 const sm = new Sfn({ StateMachine: require('./steps/choice.json') });523 const mockFn = jest.fn((input) => input);524 sm.bindTaskResource('Test', mockFn);525 await sm.startExecution({ param1: 1, param2: 'test' });526 expect(mockFn).toHaveBeenCalled();527 expect(sm.getExecutionResult()).toEqual(528 expect.objectContaining({ param1: 1, param2: 'test' }),529 );530 });531 it('can test a simple OR comparison', async () => {532 const sm = new Sfn({ StateMachine: require('./steps/choice.json') });533 const mockFn = jest.fn((input) => input);534 sm.bindTaskResource('Test', mockFn);535 await sm.startExecution({ param2: 1, param3: 'test' });536 expect(mockFn).toHaveBeenCalled();537 expect(sm.getExecutionResult()).toEqual(538 expect.objectContaining({ param2: 1, param3: 'test' }),539 );540 });541 it('can test a simple NOT comparison', async () => {542 const sm = new Sfn({ StateMachine: require('./steps/choice.json') });543 const mockFn = jest.fn((input) => input);544 sm.bindTaskResource('Test', mockFn);545 await sm.startExecution({ param4: 1 });546 expect(mockFn).toHaveBeenCalled();547 expect(sm.getExecutionResult()).toEqual(548 expect.objectContaining({ param4: 1 }),549 );550 });551 it('can test a complex AND/OR/NOT comparison', async () => {552 const sm = new Sfn({ StateMachine: require('./steps/choice.json') });553 const mockFn = jest.fn((input) => input);554 sm.bindTaskResource('Test', mockFn);555 await sm.startExecution({556 param5: 15,557 param6: 'test',558 param7: 0,559 param8: '2001-01-01T12:00:00Z',560 });561 expect(mockFn).toHaveBeenCalled();562 expect(sm.getExecutionResult()).toEqual(563 expect.objectContaining({564 param5: 15,565 param6: 'test',566 param7: 0,567 param8: '2001-01-01T12:00:00Z',568 }),569 );570 });571 });572 it('can catch custom errors', async () => {573 const sm = new Sfn({ StateMachine: require('./steps/catch.json') });574 const firstFn = jest.fn(() => {575 class CustomError extends Error {576 // empty577 }578 throw new CustomError('something happened');579 });580 const errorFn = jest.fn((input) => input);581 const lastFn = jest.fn((input) => {582 return input;583 });584 sm.bindTaskResource('First', firstFn);585 sm.bindTaskResource('All', errorFn);586 sm.bindTaskResource('Last', lastFn);587 await sm.startExecution({});588 expect(errorFn).toHaveBeenCalled();589 expect(lastFn).toHaveBeenCalled();590 expect(sm.getExecutionResult()).toEqual(591 expect.objectContaining({592 error: expect.objectContaining({593 Cause: expect.objectContaining({ errorType: 'CustomError' }),594 }),595 }),596 );597 });598 it('can catch custom States.ALL', async () => {599 const sm = new Sfn({ StateMachine: require('./steps/catch-all.json') });600 const firstFn = jest.fn(() => {601 class Custom2Error extends Error {602 // empty603 }604 throw new Custom2Error('something happened');605 });606 const errorFn = jest.fn((input) => input);607 const lastFn = jest.fn((input) => {608 return input;609 });610 sm.bindTaskResource('First', firstFn);611 sm.bindTaskResource('All', errorFn);612 sm.bindTaskResource('Last', lastFn);613 await sm.startExecution({});614 expect(errorFn).toHaveBeenCalled();615 expect(lastFn).toHaveBeenCalled();616 expect(sm.getExecutionResult()).toEqual(617 expect.objectContaining({618 error: expect.objectContaining({619 Cause: expect.objectContaining({ errorType: 'States.ALL' }),620 }),621 }),622 );623 });624 it('can catch custom States.TaskFailed', async () => {625 const sm = new Sfn({ StateMachine: require('./steps/catch.json') });626 const firstFn = jest.fn(() => {627 throw new Error('fail the task');628 });629 const errorFn = jest.fn((input) => input);630 const lastFn = jest.fn((input) => {631 return input;632 });633 sm.bindTaskResource('First', firstFn);634 sm.bindTaskResource('All', errorFn);635 sm.bindTaskResource('Last', lastFn);636 await sm.startExecution({});637 expect(errorFn).toHaveBeenCalled();638 expect(lastFn).toHaveBeenCalled();639 expect(sm.getExecutionResult()).toEqual(640 expect.objectContaining({641 error: expect.objectContaining({642 Cause: expect.objectContaining({ errorType: 'States.TaskFailed' }),643 }),644 }),645 );646 });647 it('can retry failing tasks', async () => {648 const sm = new Sfn({ StateMachine: require('./steps/retry.json') });649 const firstFn = jest.fn((input) => {650 if (input.retries === 2) {651 return input;652 }653 class CustomError extends Error {654 // empty655 }656 throw new CustomError('something happened');657 });658 const errorFn = jest.fn((input) => input);659 const lastFn = jest.fn((input) => {660 return input;661 });662 sm.bindTaskResource('First', firstFn);663 sm.bindTaskResource('All', errorFn);664 sm.bindTaskResource('Last', lastFn);665 await sm.startExecution({});666 expect(firstFn).toHaveBeenCalled();667 expect(errorFn).not.toHaveBeenCalled();668 expect(lastFn).toHaveBeenCalled();669 expect(sm.getExecutionResult()).toEqual(670 expect.objectContaining({ retries: 2 }),671 );672 }, 8000);673 it('can retry failing tasks and finally catch', async () => {674 const sm = new Sfn({ StateMachine: require('./steps/retry.json') });675 const firstFn = jest.fn(() => {676 class CustomError extends Error {677 // empty678 }679 throw new CustomError('something happened');680 });681 const errorFn = jest.fn((input) => input);682 const lastFn = jest.fn((input) => {683 return input;684 });685 sm.bindTaskResource('First', firstFn);686 sm.bindTaskResource('All', errorFn);687 sm.bindTaskResource('Last', lastFn);688 await sm.startExecution({});689 expect(firstFn).toHaveBeenCalled();690 expect(errorFn).toHaveBeenCalled();691 expect(lastFn).toHaveBeenCalled();692 expect(sm.getExecutionResult()).toEqual(693 expect.objectContaining({694 error: expect.objectContaining({695 Cause: expect.objectContaining({ errorType: 'CustomError' }),696 }),697 }),698 );699 });...

Full Screen

Full Screen

startValidationStateMachine.spec.ts

Source:startValidationStateMachine.spec.ts Github

copy

Full Screen

1import { DynamoDBStreamEvent } from 'aws-lambda';2import { MockWorkflowRegister } from '@moe-tech/orchestrator/__mock__/dals';3import { MockStepFunctions } from '@moe-tech/orchestrator/__mock__/aws';4import { WorkflowRegister } from '@moe-tech/orchestrator';5import { StepFunctions } from 'aws-sdk';6process.env.OrchestratorConfig = JSON.stringify({ statusTable: 'StatusTable' });7process.env.WorkflowRegistry = 'workflowRegistry';8const mockRegister = new MockWorkflowRegister(WorkflowRegister);9const mockStepFunction = new MockStepFunctions(StepFunctions);10describe('handler', () => {11 const handler = require('./startValidationStateMachine').handler;12 process.env.environment = 'unit-test';13 beforeEach(() => {14 mockStepFunction.reset();15 mockRegister.reset();16 });17 test('Null event', async () => {18 await handler(null, null, null);19 expect(mockStepFunction.startExecution).toBeCalledTimes(0);20 expect(mockRegister.registerInput).toBe(null);21 });22 test('Empty event', async () => {23 await handler({}, null, null);24 expect(mockStepFunction.startExecution).toBeCalledTimes(0);25 expect(mockRegister.registerInput).toBe(null);26 });27 test('Records null', async () => {28 await handler({ Records: null }, null, null);29 expect(mockStepFunction.startExecution).toBeCalledTimes(0);30 expect(mockRegister.registerInput).toBe(null);31 });32 test('Records empty', async () => {33 await handler({ Records: [] }, null, null);34 expect(mockStepFunction.startExecution).toBeCalledTimes(0);35 expect(mockRegister.registerInput).toBe(null);36 });37 test('dynamodb not set', async () => {38 await handler({ Records: [{}] }, null, null);39 expect(mockStepFunction.startExecution).toBeCalledTimes(0);40 expect(mockRegister.registerInput).toBe(null);41 });42 test('NewImage not set', async () => {43 await handler({ Records: [{ dynamodb: {} }] }, null, null);44 expect(mockStepFunction.startExecution).toBeCalledTimes(0);45 expect(mockRegister.registerInput).toBe(null);46 });47 test('Valid', async () => {48 const event = createDefaultDynamoEvent();49 await handler(event, null, null);50 expect(mockStepFunction.startExecution).toBeCalledTimes(1);51 expect(mockRegister.registerInput).toBe('workflow');52 });53 test('No uid', async () => {54 const event = createDefaultDynamoEvent();55 event.Records[0].dynamodb.NewImage.uid.S = '';56 await handler(event, null, null);57 expect(mockStepFunction.startExecution).toBeCalledTimes(0);58 expect(mockRegister.registerInput).toBe(null);59 });60 test('No workflow', async () => {61 const event = createDefaultDynamoEvent();62 event.Records[0].dynamodb.NewImage.workflow.S = '';63 await handler(event, null, null);64 expect(mockStepFunction.startExecution).toBeCalledTimes(0);65 expect(mockRegister.registerInput).toBe(null);66 });67 test('Old image exists', async () => {68 const event = createDefaultDynamoEvent();69 event.Records[0].dynamodb.OldImage = event.Records[0].dynamodb.NewImage;70 await handler(event, null, null);71 expect(mockStepFunction.startExecution).toBeCalledTimes(0);72 expect(mockRegister.registerInput).toBe(null);73 });74});75function createDefaultDynamoEvent () : DynamoDBStreamEvent {76 const retval: DynamoDBStreamEvent = {77 Records: [78 {79 dynamodb: {80 NewImage: {81 uid: { S: 'uid' },82 workflow: { S: 'workflow' }83 },84 OldImage: null85 }86 }87 ]88 } as any;89 return retval;...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var AWS = require('aws-sdk');2var redwood = new AWS.StepFunctions();3exports.handler = (event, context, callback) => {4 console.log("event: " + JSON.stringify(event));5 console.log("context: " + JSON.stringify(context));6 var params = {7 input: JSON.stringify(event)8 };9 redwood.startExecution(params, function(err, data) {10 if (err) {11 console.log(err, err.stack);12 callback(err);13 } else {14 console.log(data);15 callback(null, data);16 }17 });18};

Full Screen

Using AI Code Generation

copy

Full Screen

1var redwood = require('redwood');2var options = {3};4redwood.startExecution(options, function(err, data) {5});6### redwood(options, callback)7##### callback(err, data)8MIT © [Piyush Gupta](

Full Screen

Using AI Code Generation

copy

Full Screen

1var redwood = require('redwood');2var workflowId = "workflowId";3var input = "input";4redwood.startExecution(workflowId, input, function(err, data) {5 if (err) {6 console.log(err, err.stack);7 } else {8 console.log(data);9 }10});11 [MIT](LICENSE)

Full Screen

Using AI Code Generation

copy

Full Screen

1import { useAuth } from '@redwoodjs/auth'2import { navigate, routes } from '@redwoodjs/router'3import { useMutation } from '@redwoodjs/web'4import { Form, FieldError, Label, Submit, TextAreaField } from '@redwoodjs/forms'5import { Flash } from '@redwoodjs/web'6import { useFlash } from '@redwoodjs/web'7import { Link, routes } from '@redwoodjs/router'8import { Toaster } from '@redwoodjs/web/toast'9import { useAuth } from '@redwoodjs/auth'10import { useMutation } from '@redwoodjs/web'11import { navigate, routes } from '@redwoodjs/router'12import { Form, FieldError, Label, Submit, TextAreaField } from '@redwoodjs/forms'13import { Flash } from '@redwoodjs/web'14import { useFlash } from '@redwoodjs/web'15import { Link, routes } from '@redwoodjs/router'16import { Toaster } from '@redwoodjs/web/toast'17import { useAuth } from '@redwoodjs/auth'18import { useMutation } from '@redwoodjs/web'19import { navigate, routes } from '@redwoodjs/router'20import { Form, FieldError, Label, Submit, TextAreaField } from '@redwoodjs/forms'21import { Flash } from '@redwoodjs/web'22import { useFlash } from '@redwoodjs/web'23import { Link, routes } from '@redwoodjs/router'24import { Toaster } from '@redwoodjs/web/toast'25import { useAuth } from '@redwoodjs/auth'26import { useMutation } from '@redwoodjs/web'27import { navigate, routes } from '@redwoodjs/router'28import { Form, FieldError, Label, Submit, TextAreaField } from '@redwoodjs/forms'29import { Flash } from '@redwoodjs/web'30import { useFlash } from '@redwoodjs/web'31import { Link, routes } from '@redwoodjs/router'32import { Toaster } from '@redwoodjs/web/toast'33import { useAuth } from '@redwoodjs/auth'34import

Full Screen

Using AI Code Generation

copy

Full Screen

1const { StartExecution } = require('redwood-saga');2const { v4: uuidv4 } = require('uuid');3const startExecution = new StartExecution({4 input: {5 },6 correlationId: uuidv4(),7 executionId: uuidv4(),8});9startExecution.execute();10const { StartWorkflowExecution } = require('redwood-saga');11const { v4: uuidv4 } = require('uuid');12const startWorkflowExecution = new StartWorkflowExecution({13 input: {14 },15 correlationId: uuidv4(),16 executionId: uuidv4(),17});18startWorkflowExecution.execute();19const { SignalExecution } = require('redwood-saga');20const { v4: uuidv4 } = require('uuid');21const signalExecution = new SignalExecution({22 input: {23 },24 correlationId: uuidv4(),25 executionId: uuidv4(),26});27signalExecution.execute();28const { TerminateExecution } = require('redwood-saga');29const { v4: uuidv4 } = require('uuid');

Full Screen

Using AI Code Generation

copy

Full Screen

1'use strict';2const redwood = require('redwood');3const startExecution = redwood.startExecution;4const input = {5 "input": {6 },7 "taskToDomain": {8 }9};10const callback = function (err, data) {11 if (err) {12 console.error(err);13 } else {14 console.log(data);15 }16};17startExecution(input, callback);

Full Screen

Using AI Code Generation

copy

Full Screen

1var redwood = require('redwood');2var execution = new redwood.Execution();3var params = {4};5execution.start(params, function(err, data) {6 if (err) {7 } else {8 }9});

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run redwood automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful