Skip to content

Instantly share code, notes, and snippets.

@tarasowski
Last active August 7, 2018 06:01
Show Gist options
  • Save tarasowski/7db9dc3121e727a7059e24afb10dcd8b to your computer and use it in GitHub Desktop.
Save tarasowski/7db9dc3121e727a7059e24afb10dcd8b to your computer and use it in GitHub Desktop.
#Lambda - Boilerplate Lambda Async Testing
////********************************Async Version**********************************************////
// lambda function
let response
module.exports.handler = async(event) => {
try {
response = await Promise.resolve('Works')
} catch (err) {
console.log(err)
throw new Error('Something went wrong')
}
return response
}
// unit test
const test = require('tape')
test('should be true', async(assert) => {
assert.plan(1)
try {
const lambda = require('./lambda.js')
await lambda.handler()
assert.pass('returns a resolved promise..')
} catch (err) {
console.log(err)
assert.fail('promise was rejected...')
}
})
////********************************Promise Version**********************************************////
// lambda function
module.exports.handler = async(event) => {
const response = Promise.resolve('Works')
return response
.then(data => data)
.catch(err => {
throw new Error(err)
})
}
// unit test
const test = require('tape')
test('should be true', async(assert) => {
assert.plan(1)
const lambda = require('./lambda.js')
lambda.handler()
.then(data => {
assert.pass('returns a resolved promise...')
})
.catch(err => {
assert.fail('promise was rejected...')
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment