Skip to content

Commit 905b121

Browse files
committed
Initial commit
0 parents  commit 905b121

23 files changed

+747
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# stepfunctions-testing
2+

dist/index.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export { MockedResponse } from './response';
2+
export { StateMachineTestDefinition } from './state-machine-test-definition';
3+
export { StateMachineTestCase } from './state-machine-test-case';
4+
export { StepFunctionsMockConfig } from './step-functions-mock-config';
5+
export interface JsonSerializable {
6+
toJson: () => any;
7+
}
8+
export declare const isObjectEmpty: (obj: any) => any;

dist/index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.isObjectEmpty = exports.StepFunctionsMockConfig = exports.StateMachineTestCase = exports.StateMachineTestDefinition = exports.MockedResponse = void 0;
4+
var response_1 = require("./response");
5+
Object.defineProperty(exports, "MockedResponse", { enumerable: true, get: function () { return response_1.MockedResponse; } });
6+
var state_machine_test_definition_1 = require("./state-machine-test-definition");
7+
Object.defineProperty(exports, "StateMachineTestDefinition", { enumerable: true, get: function () { return state_machine_test_definition_1.StateMachineTestDefinition; } });
8+
var state_machine_test_case_1 = require("./state-machine-test-case");
9+
Object.defineProperty(exports, "StateMachineTestCase", { enumerable: true, get: function () { return state_machine_test_case_1.StateMachineTestCase; } });
10+
var step_functions_mock_config_1 = require("./step-functions-mock-config");
11+
Object.defineProperty(exports, "StepFunctionsMockConfig", { enumerable: true, get: function () { return step_functions_mock_config_1.StepFunctionsMockConfig; } });
12+
const isObjectEmpty = (obj) => {
13+
return (obj &&
14+
Object.keys(obj).length === 0 &&
15+
Object.getPrototypeOf(obj) === Object.prototype);
16+
};
17+
exports.isObjectEmpty = isObjectEmpty;

dist/response.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { JsonSerializable } from '.';
2+
export declare class MockedResponse implements JsonSerializable {
3+
private responses;
4+
private _name;
5+
get name(): string;
6+
constructor(name: string);
7+
return<T = any>(response: T, repeat?: number): this;
8+
throw(error: string, cause: string, repeat?: number): this;
9+
toJson(): {
10+
[x: string]: {
11+
[index: string]: any;
12+
};
13+
};
14+
}

dist/response.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.MockedResponse = void 0;
4+
const statements_1 = require("./statements");
5+
class MockedResponseCollection {
6+
collection = [];
7+
add(statement, repeat) {
8+
this.collection = [...this.collection, { repeat, statement }];
9+
}
10+
isEmpty() {
11+
return this.collection.length === 0;
12+
}
13+
toJson() {
14+
let result = {};
15+
let currentResponseIndex = 0;
16+
for (let i = 0; i < this.collection.length; i++) {
17+
const item = this.collection[i];
18+
let indexStr = currentResponseIndex.toString();
19+
if (item.repeat > 0) {
20+
currentResponseIndex = currentResponseIndex + item.repeat;
21+
indexStr = `${indexStr}-${currentResponseIndex}`;
22+
}
23+
result[indexStr] = item.statement.toJson();
24+
currentResponseIndex++;
25+
}
26+
return result;
27+
}
28+
}
29+
class MockedResponse {
30+
responses = new MockedResponseCollection();
31+
_name;
32+
get name() {
33+
return this._name;
34+
}
35+
constructor(name) {
36+
this._name = name;
37+
}
38+
return(response, repeat = 0) {
39+
this.responses.add(new statements_1.ReturnStatement(response), repeat);
40+
return this;
41+
}
42+
throw(error, cause, repeat = 0) {
43+
this.responses.add(new statements_1.ThrowStatement(error, cause), repeat);
44+
return this;
45+
}
46+
toJson() {
47+
if (this.responses.isEmpty()) {
48+
throw new Error('At least 1 return or throw response is required.');
49+
}
50+
return {
51+
[this._name]: this.responses.toJson(),
52+
};
53+
}
54+
}
55+
exports.MockedResponse = MockedResponse;

dist/state-machine-test-case.d.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { JsonSerializable } from '.';
2+
import { MockedResponse } from './response';
3+
export declare class StateMachineTestCase<StateName extends string = string> implements JsonSerializable {
4+
private mockedStates;
5+
private _testCaseName;
6+
get name(): string;
7+
private _input;
8+
get input(): any;
9+
constructor(testCaseName: string);
10+
withInput(input: any): this;
11+
addMockedState(stateName: StateName, response: MockedResponse): this;
12+
collectMockedResponses(): MockedResponse[];
13+
toJson(): {
14+
[x: string]: {
15+
[key: string]: any;
16+
};
17+
};
18+
}

dist/state-machine-test-case.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.StateMachineTestCase = void 0;
4+
const _1 = require(".");
5+
class StateMachineTestCase {
6+
mockedStates = {};
7+
_testCaseName;
8+
get name() {
9+
return this._testCaseName;
10+
}
11+
_input;
12+
get input() {
13+
return this._input;
14+
}
15+
constructor(testCaseName) {
16+
this._testCaseName = testCaseName;
17+
}
18+
withInput(input) {
19+
this._input = input;
20+
return this;
21+
}
22+
addMockedState(stateName, response) {
23+
if (this.mockedStates.hasOwnProperty(stateName)) {
24+
throw new Error(`The State ${stateName} is already defined`);
25+
}
26+
this.mockedStates[stateName] = response;
27+
return this;
28+
}
29+
collectMockedResponses() {
30+
return Object.keys(this.mockedStates).map((key) => this.mockedStates[key]); // TODO: better way to Type?
31+
}
32+
toJson() {
33+
if ((0, _1.isObjectEmpty)(this.mockedStates)) {
34+
throw new Error('At least 1 state needs to be defined');
35+
}
36+
const serializedMockedStates = Object.keys(this.mockedStates).reduce((acc, key) => {
37+
acc[key] = this.mockedStates[key].name;
38+
return acc;
39+
}, {});
40+
return {
41+
[this._testCaseName]: serializedMockedStates,
42+
};
43+
}
44+
}
45+
exports.StateMachineTestCase = StateMachineTestCase;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { JsonSerializable } from '.';
2+
import { MockedResponse } from './response';
3+
import { StateMachineTestCase } from './state-machine-test-case';
4+
declare type TestCaseDefinitions = {
5+
[key: string]: StateMachineTestCase;
6+
};
7+
export declare class StateMachineTestDefinition implements JsonSerializable {
8+
private _testCaseDefinitions;
9+
get testCaseDefinitions(): TestCaseDefinitions;
10+
private _stateMachineName;
11+
get stateMachineName(): string;
12+
constructor(stateMachineName: string);
13+
addTestCase(testCase: StateMachineTestCase): this;
14+
collectTestCase(): StateMachineTestCase[];
15+
collectMockedResponses(): MockedResponse[];
16+
toJson(): {
17+
[x: string]: {
18+
TestCases: {
19+
[key: string]: any;
20+
};
21+
};
22+
};
23+
}
24+
export {};

dist/state-machine-test-definition.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.StateMachineTestDefinition = void 0;
4+
const _1 = require(".");
5+
class StateMachineTestDefinition {
6+
_testCaseDefinitions = {};
7+
get testCaseDefinitions() {
8+
return this._testCaseDefinitions;
9+
}
10+
_stateMachineName;
11+
get stateMachineName() {
12+
return this._stateMachineName;
13+
}
14+
constructor(stateMachineName) {
15+
this._stateMachineName = stateMachineName;
16+
}
17+
addTestCase(testCase) {
18+
if (this._testCaseDefinitions.hasOwnProperty(testCase.name)) {
19+
throw new Error(`TestCase ${testCase.name} is already defined`);
20+
}
21+
this._testCaseDefinitions[testCase.name] = testCase;
22+
return this;
23+
}
24+
collectTestCase() {
25+
return Object.values(this._testCaseDefinitions);
26+
}
27+
collectMockedResponses() {
28+
return Object.keys(this._testCaseDefinitions).reduce((acc, key) => {
29+
return acc.concat(this._testCaseDefinitions[key].collectMockedResponses());
30+
}, []);
31+
}
32+
toJson() {
33+
if ((0, _1.isObjectEmpty)(this._testCaseDefinitions)) {
34+
throw new Error('At least 1 test case needs to be defined');
35+
}
36+
const serializedTestCaseDefinitions = Object.keys(this._testCaseDefinitions).reduce((acc, key) => {
37+
return { ...acc, ...this._testCaseDefinitions[key].toJson() };
38+
}, {});
39+
return {
40+
[this._stateMachineName]: {
41+
TestCases: serializedTestCaseDefinitions,
42+
},
43+
};
44+
}
45+
}
46+
exports.StateMachineTestDefinition = StateMachineTestDefinition;

0 commit comments

Comments
 (0)