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

add step function matchers and utils #11

Merged
merged 2 commits into from
Oct 15, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
add step function matchers and utils
  • Loading branch information
erezrokah committed Oct 15, 2018
commit 5ced157fac5e44051c6e949dc87857a95dd3eac7
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import 'jest-e2e-serverless';
- [toHaveItem()](#tohaveitem)
- [toHaveObject()](#tohaveobject)
- [toHaveLog()](#tohavelog)
- [toBeAtState()](#tobeatstate)

#### `toHaveItem()`

Expand Down Expand Up @@ -133,12 +134,29 @@ await expect({

[See complete example](https://github.com/erezrokah/hello-retail/blob/master/e2eTests/src/sendUserLogin.test.ts)

#### `toBeAtState()`

Asserts a state machine state

```js
expect.assertions(1); // makes sure the assertion was called
await expect({
pollEvery: 5000,
region: 'us-east-1',
stateMachineArn: 'stateMachineArn',
timeout: 30 * 1000,
}).toBeAtState('ExpectedState');
```

[See complete example](https://github.com/erezrokah/hello-retail/blob/master/e2eTests/src/newProduct.test.ts)

### Utils

- [invoke()](#invoke)
- [clearAllItems()](#clearallitems)
- [clearAllObjects()](#clearallobjects)
- [deleteAllLogs()](#deletealllogs)
- [stopRunningExecutions()](#stoprunningexecutions)
- [deploy()](#deploy)

#### `invoke()`
Expand Down Expand Up @@ -187,6 +205,18 @@ const { deleteAllLogs } = require('jest-e2e-serverless/lib/utils/cloudwatch');
await deleteAllLogs('us-east-1', 'lambda-function-name');
```

#### `stopRunningExecutions()`

Stop all running executions for a state machine

```typescript
const {
stopRunningExecutions,
} = require('jest-e2e-serverless/lib/utils/stepFunctions');

await stopRunningExecutions('us-east-1', 'state-machine-arn');
```

#### `deploy()`

Deploys the current service using [Serverless framework](https://serverless.com/)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jest-e2e-serverless",
"version": "0.0.10",
"version": "0.0.11",
"description": "Jest matchers for Serverless frameworks",
"main": "lib/index.js",
"scripts": {
Expand Down
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,26 @@ import { toHaveLog } from './matchers/cloudwatch';
import { wrapWithRetries } from './matchers/common';
import { toHaveItem } from './matchers/dynamoDb';
import { toHaveObject } from './matchers/s3';
import { toBeAtState } from './matchers/stepFunctions';

declare global {
namespace jest {
// tslint:disable-next-line:interface-name
interface Matchers<R> {
toBeAtState: (state: string) => R;
toHaveItem: (
key: AWS.DynamoDB.DocumentClient.Key,
expectedItem?: AWS.DynamoDB.DocumentClient.AttributeMap,
strict?: boolean,
) => R;
toHaveObject: (key: string, expectedItem?: Buffer) => R;
toHaveLog: (pattern: string) => R;
toHaveObject: (key: string, expectedItem?: Buffer) => R;
}
}
}

expect.extend({
toBeAtState: wrapWithRetries(toBeAtState) as typeof toBeAtState,
toHaveItem: wrapWithRetries(toHaveItem) as typeof toHaveItem,
toHaveLog: wrapWithRetries(toHaveLog) as typeof toHaveLog,
toHaveObject: wrapWithRetries(toHaveObject) as typeof toHaveObject,
Expand Down
1 change: 0 additions & 1 deletion src/matchers/cloudwatch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { toHaveLog } from './cloudwatch';
jest.mock('./common');
jest.mock('../utils/cloudwatch');
jest.spyOn(console, 'error');
jest.mock('jest-diff');

describe('cloudwatch matchers', () => {
describe('toHaveLog', () => {
Expand Down
141 changes: 141 additions & 0 deletions src/matchers/stepFunctions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import { EOL } from 'os';
import { toBeAtState } from './stepFunctions';

jest.mock('./common');
jest.mock('../utils/stepFunctions');
jest.spyOn(console, 'error');
jest.mock('jest-diff');

describe('stepFunctions matchers', () => {
describe('toBeAtState', () => {
const matcherUtils = {
equals: jest.fn(),
utils: {
matcherHint: jest.fn(i => i),
printExpected: jest.fn(i => i),
printReceived: jest.fn(i => i),
},
};
const region = 'region';
const stateMachineArn = 'stateMachineArn';
const props = { region, stateMachineArn };
const expectedState = 'expectedState';

beforeEach(() => {
jest.clearAllMocks();
});

test('should throw error on getCurrentState error', async () => {
const { verifyProps } = require('./common');
const { getCurrentState } = require('../utils/stepFunctions');

const error = new Error('Unknown error');
getCurrentState.mockReturnValue(Promise.reject(error));

expect.assertions(7);
await expect(
toBeAtState.bind(matcherUtils)(props, expectedState),
).rejects.toBe(error);
expect(getCurrentState).toHaveBeenCalledTimes(1);
expect(getCurrentState).toHaveBeenCalledWith(
props.region,
props.stateMachineArn,
);
expect(console.error).toHaveBeenCalledTimes(1);
expect(console.error).toHaveBeenCalledWith(
`Unknown error getting state machine state: ${error.message}`,
);
expect(verifyProps).toHaveBeenCalledTimes(1);
expect(verifyProps).toHaveBeenCalledWith(
{ ...props, state: expectedState },
['region', 'stateMachineArn', 'state'],
);
});

test('should not pass when wrong state', async () => {
const diff = require('jest-diff');
const diffString = 'diffString';
diff.mockReturnValue(diffString);

matcherUtils.equals.mockReturnValue(false);

const { getCurrentState } = require('../utils/stepFunctions');

const receivedState = 'receivedState';
getCurrentState.mockReturnValue(Promise.resolve(receivedState));

const { message, pass } = await toBeAtState.bind(matcherUtils)(
props,
expectedState,
);

expect(matcherUtils.equals).toHaveBeenCalledTimes(1);
expect(matcherUtils.equals).toHaveBeenCalledWith(
expectedState,
receivedState,
);
expect(diff).toHaveBeenCalledTimes(1);
expect(diff).toHaveBeenCalledWith(expectedState, receivedState, {
expand: true,
});

expect(pass).toBeFalsy();
expect(message).toEqual(expect.any(Function));
expect(message()).toEqual(
`.toBeAtState${EOL}${EOL}Expected ${stateMachineArn} at region ${region} to be at state ${expectedState}${EOL}` +
`Difference:${EOL}${EOL}${diffString}`,
);
});

test('should not pass when wrong state empty diffString', async () => {
const diff = require('jest-diff');
const diffString = '';
diff.mockReturnValue(diffString);

matcherUtils.equals.mockReturnValue(false);

const { getCurrentState } = require('../utils/stepFunctions');

const receivedState = 'receivedState';
getCurrentState.mockReturnValue(Promise.resolve(receivedState));

const { message } = await toBeAtState.bind(matcherUtils)(
props,
expectedState,
);

expect(message()).toEqual(
`.toBeAtState${EOL}${EOL}Expected ${stateMachineArn} at region ${region} to be at state ${expectedState}${EOL}Difference:`,
);
});

test('should pass when correct state', async () => {
const diff = require('jest-diff');

matcherUtils.equals.mockReturnValue(true);

const { getCurrentState } = require('../utils/stepFunctions');

const receivedState = expectedState;
getCurrentState.mockReturnValue(Promise.resolve(receivedState));

const { message, pass } = await toBeAtState.bind(matcherUtils)(
props,
expectedState,
);

expect(matcherUtils.equals).toHaveBeenCalledTimes(1);
expect(matcherUtils.equals).toHaveBeenCalledWith(
expectedState,
receivedState,
);
expect(diff).toHaveBeenCalledTimes(0);

expect(pass).toBeTruthy();
expect(message).toEqual(expect.any(Function));
expect(message()).toEqual(
`.not.toBeAtState${EOL}${EOL}Expected ${stateMachineArn} at region ${region} not to be at state ${expectedState}${EOL}`,
);
});
});
});
52 changes: 52 additions & 0 deletions src/matchers/stepFunctions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import diff = require('jest-diff');
import { EOL } from 'os';
import { getCurrentState } from '../utils/stepFunctions';
import { ICommonProps, verifyProps } from './common';

interface IStepFunctionsProps extends ICommonProps {
stateMachineArn: string;
}

const expectedProps = ['region', 'stateMachineArn', 'state'];

export const toBeAtState = async function(
this: jest.MatcherUtils,
props: IStepFunctionsProps,
expected: string,
) {
verifyProps({ ...props, state: expected }, expectedProps);

const { region, stateMachineArn } = props;

try {
const printStateMachineArn = this.utils.printExpected(stateMachineArn);
const printRegion = this.utils.printExpected(region);
const printExpected = this.utils.printExpected(expected) + EOL;

const notHint = this.utils.matcherHint('.not.toBeAtState') + EOL + EOL;
const hint = this.utils.matcherHint('.toBeAtState') + EOL + EOL;

const received = await getCurrentState(region, stateMachineArn);
const pass = this.equals(expected, received);
if (pass) {
return {
message: () =>
`${notHint}Expected ${printStateMachineArn} at region ${printRegion} not to be at state ${printExpected}`,
pass: true,
};
} else {
const diffString = diff(expected, received, {
expand: true,
});
return {
message: () =>
`${hint}Expected ${printStateMachineArn} at region ${printRegion} to be at state ${printExpected}` +
`Difference:${diffString ? `${EOL}${EOL}${diffString}` : ''}`,
pass: false,
};
}
} catch (e) {
console.error(`Unknown error getting state machine state: ${e.message}`);
throw e;
}
};
Loading