CodeGenieApp / CodeGenieApp/serverless-express
How to test handler code
- Vorherrschende Sprache
- JavaScript
- Sterne
- 5.3k
- Forks
- 676
- PR-Merge-Kennzahlen
- Keine gemergten PRs in 30 T.
Beschreibung
First of all thank you so much for your great work and effort in maintaining this library.
I'm very happy you have added an integration and unit test to the repository.
How can I test our handler code?
Our handler looks this:
//handler.js
import serverlessExpress from '@vendia/serverless-express'
import app from './app'
export default serverlessExpress({app}).handler
using express
//app.js
...
app.get('/health', (req, res, next) => {
response.setHeader('isBase64Encoded', true)
response.header('content-type', 'application/json')
res.status(200).send({
healthy: true,
status:200,
})
})
I would like to have a jest test handler.test.js in our code repo.
This is a smoke test to see if things are more or less okay.
The handler might still not run on AWS but at least we'll catch more gross errors in Jest on dev machine rather than waiting for the code to be pushed to staging to do an E2E test with Cypress.
I've tried passing an event but at best I'm getting an empty object response {}
//handler.test.js
import handler from '../handler'
describe('handler.js', () => {
it('GET /takeaway/health should return 200', async () => {
const event = {
headers: {},
httpMethod: 'GET',
path: '/health',
requestContext: {
stage: 'prod',
},
}
const context = {}
const response = handler(event, context)
console.log('response', JSON.stringify(response, null, 2))
expect(response.statusCode).toEqual(200)
const body = JSON.parse(atob(response.body))
// console.log('body', JSON.stringify(body, null, 2))
expect(body.status).toEqual(200)
expect(body.healthy).toEqual(true)
})
})
Beitragsleitfaden
Bewertung
Dieses Issue wurde noch nicht bewertet.