@mapbox/mock-aws-sdk-js

1.0.1 • Public • Published

@mapbox/mock-aws-sdk-js

Build Status

A library that provides sinon-style stubs for aws-sdk-js service methods for use in testing.

Goals

  • allow tests to make assertions about both service client configuration (e.g. region) and method arguments
  • enable tests for application logic with varied usage of AWS.Request object methods

Basic usage

Your application, app.js:

var AWS = require('aws-sdk');

module.exports = function(callback) {
  // It is important that aws-sdk clients be defined in a function, and not
  // as module-level variables.
  var s3 = new AWS.S3({ region: 'eu-west-1' });
  s3.getObject({ Bucket: 'bucket', Key: 'key' }, function(err, data) {
    if (err) return callback(err);
    callback(null, data.Body.toString());
  });
}

Your test script:

var test = require('tape');
var app = require('./app');
var AWS = require('@mapbox/mock-aws-sdk-js');

test('gets S3 object', function(assert) {
  var data = { Body: new Buffer('hello world') };
  var expected = { Bucket: 'bucket', Key: 'key' };

  AWS.stub('S3', 'getObject', function(params, callback) {
    assert.deepEqual(params, expected, 'called s3.getObject with expected params');
    callback(null, data);
  });

  app.useCallback(function(err, data) {
    assert.ifError(err, 'success');
    assert.equal(data, 'hello world');

    assert.equal(getObject.callCount, 1, 'called s3.getObject once');
    assert.equal(AWS.S3.callCount, 1, 'one s3 client created');
    assert.ok(AWS.S3.calledWithExactly({ region: 'eu-west-1' }), 's3 client created for the correct region');

    AWS.S3.restore();
    assert.end();
  });
});

Read all about how to use sinon stubs here.

More examples

test/test-app.js represents a module that makes the same basic S3.getObject request in several different ways. test/index.test.js then contains a number of examples for how you might choose to write tests for the module.

Readme

Keywords

Package Sidebar

Install

npm i @mapbox/mock-aws-sdk-js

Weekly Downloads

2,500

Version

1.0.1

License

ISC

Unpacked Size

17 kB

Total Files

9

Last publish

Collaborators

  • mbx-npm-ci-production
  • mbx-npm-ci-staging
  • mbx-npm-advanced-actions-production
  • mbx-npm-advanced-actions-staging
  • mbx-npm-09-production
  • mbx-npm-08-production
  • mbx-npm-07-production
  • mbx-npm-06-production
  • mbx-npm-05-production
  • mbx-npm-04-production
  • mbx-npm-03-production
  • mbx-npm-02-production
  • mbx-npm-01-production
  • mbx-npm-02-staging
  • mapbox-npm-01
  • mapbox-npm-02
  • mapbox-npm-07
  • mapbox-npm-03
  • mapbox-npm-04
  • mapbox-npm-09
  • mapbox-npm-05
  • mapbox-npm-06
  • mapbox-npm-08
  • mapbox-npm-advanced-actions
  • mapbox-npm-ci
  • mapbox-npm
  • mapbox-admin
  • mapbox-machine-user