Testing with supertest
Now, it is time to make sure that our REST API is working as expected. We will learn in this section how to build solid tests while using Express.
Adding stores to the server
We will refactor each route to use the store functions. Let’s start with the GET /
api/v1/whisper
route:
app.get('/api/v1/whisper', async (req, res) => { const whispers = await getAll() res.json(whispers) })
Basically, we are using the getAll
function to get all the whispers and we are returning them in the response. Now, let’s refactor the GET /
api/v1/whisper/:id
route:
app.get('/api/v1/whisper/:id', async (req, res) => { const id = parseInt(req.params.id) const whisper = await getById(id) if (!whisper) { res.sendStatus(404) } else { ...