JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 30966
  • Score
    100M100P100Q237998F
  • License MIT

This library returns a mock context object that can be used to test lambda functions.

Package Exports

  • aws-lambda-mock-context

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (aws-lambda-mock-context) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

aws-lambda-mock-context

This library returns a mock context object that can be used to test lambda functions

Installation

npm install --save aws-lambda-mock-context

Usage

The library can be used in combination with SinonJS to test if the correct methods are called.

// module dependencies
var chai = require('chai'),
    sinon = require('sinon'),
    sinonChai = require('sinonChai'),
    Q = require('q'),
    ctx = require('aws-lambda-mock-context');;

// Use the should flavour
chai.should();
chai.use(sinonChai);

// Start the test
describe('Lambda Test', function() {
    
    // Create a new context
    var ctx = context();
    
    beforeEach(function() {
        // Spy the succeed function
        sinon.spy(ctx, 'succeed');
    });
    
    afterEach(function() {
        // Restore it back to normal
        ctx.succeed.restore();
    });
    
    it('Should call the succeed method', function(done) {
        Q.fcall(function() {
            // Call the handler method
            return index.handler({hello: 'world'}, ctx);
        }).then(function() {
            // Test if the succeed method is called once
            ctx.succeed.should.be.calledOnce;
            
            done();
        });
    });
});

If the handler method is asynchronous, make sure it returns a promise. If you don't return a promise, the test will fail. This is because the handler returns but at that moment, the succeed method is not yet called.

Contributors

License

MIT © Sam Verschueren