Skip to content

Latest commit

 

History

History
213 lines (168 loc) · 6.1 KB

README.md

File metadata and controls

213 lines (168 loc) · 6.1 KB

Jest Setup

The simplest setup is to use jest's setupFilesAfterEnv config.

Make sure your package.json includes the following:

// package.json
"jest": {
  "setupFilesAfterEnv": ["./node_modules/aws-testing-library/lib/jest/index.js"],
},

Usage with TypeScript

When using aws-testing-library with TypeScript and ts-jest, you'll need to add a setupFrameworks.ts file to your app that explicitly imports aws-testing-library, and point the setupFilesAfterEnv field in your package.json file towards it:

// src/setupFrameworks.ts
import 'aws-testing-library/lib/jest';
// package.json
"jest": {
 "setupFilesAfterEnv": ["./src/setupFrameworks.ts"],
},

Assertions

Notes

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

toHaveItem()

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) */,
}).toHaveItem(
  {
    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

toHaveObject()

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) */,
}).toHaveObject(
  '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

toHaveLog()

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) */,
}).toHaveLog(
  'some message written to log' /* a pattern to match against log messages */,
);

See complete example

toBeAtState()

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) */,
}).toBeAtState('ExpectedState');

See complete example

toHaveState()

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) */,
}).toHaveState('ExpectedState');

See complete example

toReturnResponse()

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 */,
}).toReturnResponse({
  data: {
    message: 'Unauthorized',
  },
  statusCode: 401,
});

See complete example

toHaveRecord()

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) */,
}).toHaveRecord(
  (item) => item.id === 'someId' /* predicate to match with the stream data */,
);

See complete example

toHaveMessage()

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) */,
  }).toHaveMessage(
    /* 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