Skip to content

Latest commit

 

History

History
203 lines (160 loc) · 5.46 KB

README.md

File metadata and controls

203 lines (160 loc) · 5.46 KB

Chai Setup

const awsTesting = require('aws-testing-library/lib/chai').default;
const chai = require('chai');
chai.use(awsTesting);

const { expect } = chai;

// write assertions using expect

Usage with TypeScript

import awsTesting from 'aws-testing-library/lib/chai';
import chai = require('chai');

chai.use(awsTesting);

const { expect } = chai;

// write assertions using expect

Assertions

Notes

  • The matchers use aws-sdk under the hood, thus they are all asynchronous and require using async/await

to.have.item()

Asserts existence/equality of a DynamoDb item

await expect({
  region: 'us-east-1',
  table: 'dynamo-db-table',
  timeout: 0 /* optional (defaults to 2500) */,
  pollEvery: 0 /* optional (defaults to 500) */,
}).to.have.item(
  {
    id: 'itemId',
  } /* dynamoDb key object (https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#getItem-property) */,
  {
    id: 'itemId',
    createdAt: new Date().getTime(),
    text: 'some content',
  } /* optional, if exists will check equality in addition to existence */,
  true /* optional, strict mode comparison, defaults to true */,
);

See complete example

to.have.object()

Asserts existence/equality of a S3 object

await expect({
  region: 'us-east-1',
  bucket: 's3-bucket',
  timeout: 0 /* optional (defaults to 2500) */,
  pollEvery: 0 /* optional (defaults to 500) */,
}).to.have.object(
  'someFileInTheBucket' /* a string representing the object key in the bucket */,
  Buffer.from(
    'a buffer of the file content',
  ) /* optional, if exists will check equality in addition to existence */,
);

See complete example

to.have.log()

Asserts existence of a cloudwatch log message

await expect({
  region: 'us-east-1',
  // use either an explicit log group
  logGroupName: 'logGroupName',
  // or a function name to match a lambda function logs
  function: 'functionName',
  startTime: 0 /* optional (milliseconds since epoch in UTC, defaults to now-1 hour) */,
  timeout: 0 /* optional (defaults to 2500) */,
  pollEvery: 0 /* optional (defaults to 500) */,
}).to.have.log(
  'some message written to log' /* a pattern to match against log messages */,
);

to.be.atState()

Asserts a state machine current state

await expect({
  pollEvery: 5000 /* optional (defaults to 500) */,
  region: 'us-east-1',
  stateMachineArn: 'stateMachineArn',
  timeout: 30 * 1000 /* optional (defaults to 2500) */,
}).to.be.atState('ExpectedState');

to.have.state()

Asserts that a state machine has been at a state

await expect({
  pollEvery: 5000 /* optional (defaults to 500) */,
  region: 'us-east-1',
  stateMachineArn: 'stateMachineArn',
  timeout: 30 * 1000 /* optional (defaults to 2500) */,
}).to.have.state('ExpectedState');

to.have.response()

Asserts that an api returns a specific response

await expect({
  url: 'https://api-id.execute-api.us-east-1.amazonaws.com/dev/api/private',
  method: 'POST',
  params: { urlParam: 'value' } /* optional URL parameters */,
  data: { bodyParam: 'value' } /* optional body parameters */,
  headers: { Authorization: 'Bearer token_value' } /* optional headers */,
}).to.have.response({
  data: {
    message: 'Hello World!',
  },
  statusCode: 200,
});

See complete example

to.have.record()

Asserts existence/equality of a Kinesis record

await expect({
  region: 'us-east-1',
  stream: 'kinesis-stream',
  timeout: 0 /* optional (defaults to 10000) */,
  pollEvery: 0 /* optional (defaults to 500) */,
}).to.have.record(
  (item) => item.id === 'someId' /* predicate to match with the stream data */,
);

See complete example

to.have.message()

Asserts existence/equality of a message in an SQS queue

const {
  subscribeToTopic,
  unsubscribeFromTopic,
} = require('aws-testing-library/lib/utils/sqs');

let [subscriptionArn, queueUrl] = ['', ''];
try {
  // create an SQS queue and subscribe to SNS topic
  ({ subscriptionArn, queueUrl } = await subscribeToTopic(region, topicArn));

  // run some code that will publish a message to the SNS topic
  someCodeThatResultsInPublishingAMessage();

  await expect({
    region,
    queueUrl,
    timeout: 10000 /* optional (defaults to 2500) */,
    pollEvery: 2500 /* optional (defaults to 500) */,
  }).to.have.message(
    /* predicate to match with the messages in the queue */
    (message) =>
      message.Subject === 'Some Subject' && message.Message === 'Some Message',
  );
} finally {
  // unsubscribe from SNS topic and delete SQS queue
  await unsubscribeFromTopic(region, subscriptionArn, queueUrl);
}

See complete example