CodeGenieApp / CodeGenieApp/serverless-express

How to test handler code

Abierto
#364 7 comentarios 0 reacciones 1 asignado Reclamado por @giorgio-zamparelli Ver en GitHub
docs
Lenguaje dominante
JavaScript
Estrellas
5.3k
Forks
674
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

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)

})

})

Guía de contribución

Abrir la guía de contribución

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.