Last active
October 31, 2018 00:42
-
-
Save chris-burkhardt/354802a68ab3264b738bb32ee2a2ccd1 to your computer and use it in GitHub Desktop.
JEST unit testing example for fs.readFile on Lambda handler
This file contains 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
module.exports = (data) => { | |
const { | |
message, path, reason, requestId, code, | |
} = data; | |
return { | |
message, | |
error: { | |
path, | |
reason, | |
requestId, | |
code, | |
}, | |
}; | |
}; |
This file contains 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
{ | |
"version": 1, | |
"triggerSource": "CustomMessage_ForgotPassword", | |
"region": "us-west-2", | |
"userPoolId": "us-west-2_Rdf128test23r4", | |
"userName": "testUser", | |
"callerContext": { | |
"awsSdk": "1", | |
"clientId": "123456789" | |
}, | |
"request": { | |
"userAttributes": { | |
"phone_number_verified": false, | |
"email_verified": true | |
}, | |
"codeParameter": "test" | |
}, | |
"response": { | |
"smsMessage": "<custom message to be sent in the message with code parameter>", | |
"emailMessage": "<custom message to be sent in the message with code parameter>", | |
"emailSubject": "<custom email subject>" | |
} | |
} |
This file contains 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
const fs = require('fs'); | |
const createErrorBody = require('./utils/createErrorBody'); | |
async function readCustomForgotPasswordEmail() { | |
return new Promise((resolve, reject) => { | |
fs.readFile('./emails/forgotpassword-email.html', 'utf-8', (err, data) => { | |
if (err) { | |
console.log(JSON.stringify(createErrorBody({ | |
message: 'Not able to read the forgotpassword custom email', | |
reason: 'Not able to read the forgotpassword custom email', | |
code: '4006', | |
}))); | |
reject(err); | |
} else { | |
console.log('Forgot Password Custom Email Read Successfully'); | |
resolve(data); | |
} | |
}); | |
}); | |
} | |
exports.handler = async (event) => { | |
const newEvent = { ...event }; | |
// Identify why was this function invoked | |
if (event.triggerSource === 'CustomMessage_ForgotPassword') { | |
// Ensure that your message contains event.request.codeParameter | |
let emailMessage; | |
try { | |
emailMessage = await readCustomForgotPasswordEmail(); | |
} catch (e) { | |
return event; | |
} | |
newEvent.response.emailSubject = 'Forgot Your Password Security Confirmation'; | |
newEvent.response.emailMessage = emailMessage; | |
} | |
// Create custom message for other events | |
return newEvent; | |
}; |
This file contains 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
/* global jest describe beforeEach afterAll it expect */ | |
const fs = require('fs'); | |
const mod = require('../../../src/sendCustomMessage'); | |
const lambdaWrapper = require('lambda-wrapper'); | |
const wrapped = lambdaWrapper.wrap(mod, { | |
handler: 'handler', | |
}); | |
describe('sendCustomMessage', () => { | |
let event = {}; | |
const forgotPasswordEmailFile = './emails/forgotpassword-email.html'; | |
const wrongForgotPasswordEmailFile = './emails/forgotpasswordemail.html'; | |
beforeEach(() => { | |
event = JSON.parse(fs.readFileSync('./test/testData/customMessageForgotPasswordTrigger.json')); | |
}); | |
afterAll(() => { | |
fs.renameSync(wrongForgotPasswordEmailFile, forgotPasswordEmailFile); | |
}); | |
it('should return with email content when having successfully read forgotpassword email template', () => { | |
return wrapped.run(event).then((response) => { | |
expect(response.response.emailSubject).toEqual('Forgot Your Password Security Confirmation'); | |
}); | |
}); | |
it('should mock console.log and equal "Forgot Password Custom Email Read Successfully"', () => { | |
// tests the console.log in the resolve else branch | |
console.log = jest.fn(); | |
return wrapped.run(event).then(() => { | |
expect(console.log.mock.calls[0][0]).toEqual('Forgot Password Custom Email Read Successfully'); | |
}); | |
}); | |
it('should return without email content when forgotpassword email failed to be read', () => { | |
fs.renameSync(forgotPasswordEmailFile, wrongForgotPasswordEmailFile); | |
return wrapped.run(event).then((response) => { | |
expect(response.response.emailSubject).toEqual('<custom email subject>'); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment