CodeGenieApp / CodeGenieApp/serverless-express

How to test handler code

Aperta
#364 7 commenti 0 reazioni 1 assegnatario Rivendicata da @giorgio-zamparelli Vedi su GitHub
docs
Lingua principale
JavaScript
Stelle
5.3k
Fork
676
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

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)

})

})

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.