@mapbox/mock-aws-sdk-js

v1.0.0
Create stubbed aws-sdk-js clients for testing purposes
aws mock sinon testing

@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.

npm i @mapbox/[email protected]

Metadata

  • ISC
  • >4.0.0
  • Mapbox
  • released 3/28/2018

Downloads

Maintainers

1ec5, aaaandrea, aag6z, aarthykc, ajashton, akit, alianthes, aliceykuo, alinapaz, allierowan, alulsh, amishas157, ampetr89, amyleew, anandthakker, andygol, angela.navarro, ansis, aparlato, apavani, apendleton, arunasank, asheemmamoowala, atevans-mapbox, benjamintd, bhousel, bkowshik, boblannon, brendanmcfarland, brsbl, brunoabinader, bsrinivasa, bsudekum, camillacaros, camilo.sanin, cammace, captainbarbosa-mbx, caseymm, castledoor, charloyd, chaupow, chetangowda, chriscx, chrisloer, cknouss, cmtoomey, colleenmcginnis, d-prukop, danieljh, danmactough, danpat, danpaz-mb, danswick, dasulit, davidtheclark, deniskoronchik, dgearhart, dhcole, dianeschulze, dnesbitt, dnomadb, dputtick, drboyer, dthompson, edgar-marca, electrostat, elifitch, emilymcafee, emilymdubois, enf, ericdeveloper, eschow, fa7c0n, fabianguerra, flippmoke, franka, freenerd, friedbunny, geografa, ghoshkaj, gknisely, gretacb, guardiola31337, heyadam_mapbox, heystenson, hjudge, ian29, ianshward, ilyasaf, ingalls, isiyu, ivovandongen, jacquestardie, jayana, jc-clark, jcsg, jfirebaugh, jim-martin, jinnycho503, jjanczyszyn, jmkiley, jordanwebb, jothirnadh, jrpruit1, juliannebg, k-mahoney, kaibot3000, kalimar, karenell, karenzshea, katydecorah, kdiluca, kellyoung, kepta, kevinkreiser, kilatsat, kkaefer, l-r, langstonsmith, lbud, lily-chai, lingoded, lizziegooding, lobenichou, lshig, lucaswoj, luiswalter, lukasmartinelli, lukaspaczos, lxbarth, lyzidiamond, macro-shen, maikyon, mal-wood, maning, manoharuss, mapbox-admin, mapsam, markmisener, mateov, matt.gosline, mattficke, mcwhittemore, miccolis, mikelmaron, millzpaugh, mollymerp, morgane, morganherlocker, mourner, msirenko, mtirwin, nammala, natslaughter, nickcordella, nickidlugash, nickitaliano, nikhilprabhakar, nmargolis, norchard, normanb, oini, okoriep, oliikit, oormilavinod, otherwiseman, oxidase, paigemoody, pdgoodman, perrygeo, peterqliu, planemad, poornibadrinath, pratikyadav, pt-44, pveugen, rasagy, rclark, rcleedev, recursivefunk, rmrice, ryan-baumann, s-heisler, sabaelhilo, sabzo, sadiew, saikia.abhishek, samanbb, samely, samfader, sbma44, scottschultz, scottsfarley93, sgillies, springmeyer, srividyacb, tapasweni-pathak, taziam, tcql, themarex, tmpsantos, tony-cj, trevorspecht, tridip1931, tristen, upendrayadav, vakila, vincents, virginiayung, vladimir.kananovich, willwhite, xianny, yhahn, yuffster, yuletide, zhuwenlong, zmully, zugaldia, zurihunter92,