Last active
May 5, 2017 19:01
-
-
Save richardhyatt/721bdffa65ad0eeb055915caf243116b to your computer and use it in GitHub Desktop.
Unit Testing AWS Lambda Functions in Node.js #2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| var expect = require( 'chai' ).expect; | |
| var myLambda = require( '../index' ); | |
| describe( 'myLambda', function() { | |
| [ | |
| "Richard", | |
| "rhyatt" | |
| ].forEach( function( validName ) { | |
| it( `successful invocation: name=${validName}`, function( done ) { | |
| var context = { | |
| succeed: function( result ) { | |
| expect( result.valid ).to.be.true; | |
| done(); | |
| }, | |
| fail: function() { | |
| done( new Error( 'never context.fail' ) ); | |
| } | |
| } | |
| myLambda.handler( { name: validName }, { /* context */ }, (err, result) => { | |
| try { | |
| expect( err ).to.not.exist; | |
| expect( result ).to.exist; | |
| expect( result.valid ).to.be.true; | |
| done(); | |
| } | |
| catch( error ) { | |
| done( error ); | |
| } | |
| }); | |
| }); | |
| }); | |
| [ | |
| "Fred", | |
| undefined | |
| ].forEach( function( invalidName ) { | |
| it( `fail: when name is invalid: name=${invalidName}`, function( done ) { | |
| myLambda.handler( { name: invalidName }, { /* context */ }, (err,result) => { | |
| try { | |
| expect( err ).to.exist; | |
| expect( err.message ).to.equal( 'unknown name' ); | |
| expect( result ).to.not.exist; | |
| done(); | |
| } | |
| catch( error ) { | |
| done( eror ); | |
| } | |
| }); | |
| }); | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment