0

My end goal is that I want to be able to create a test that satisfies the following statement:

verify that requests to valid URLs return a 200 HTTP status code

A valid URL for example would be /about-page or /jobs, basically any directory that I add in my content folder that contains a file with the extension /index.md.

This is my code so far:

app.js

const readFilePromise = util.promisify(fs.readFile)

app.get('/*', (req, res) => {
  readFilePromise(path.join(__dirname, 'content', req.url) + '/index.md', 'utf8')
    .then(data => {
      convertData(data, res)
    })
    .catch(err => {
      res.status(404).send('Page doesn\'t exist!')
    })
})

const convertData = (data, res) => {
  const convertedData = md.render(data)
  readFilePromise(path.join(__dirname, '/template.html'), 'utf8')
    .then(data => {
      data = data.replace(/\{\{content\}\}/, convertedData)
      res.send(data)
    })
    .catch(err => {
      console.log(err)
    })
}
app.listen(3000) 
module.exports = app

After reading this article, it mentions that

Requests are asynchronous, which means you must be able to conduct asynchronous tests.

So I wrote the following test:

app.test.js

const app = require('./app.js')
const request = supertest(app)
const supertest = require('supertest')


it('Gets the test endpoint and returns a 200 status', async done => {
  const res = await request.get('/*')
  expect(res.status).toBe(200)
  done()
})

When I run the test, it fails with a 404 status, rather than returning a 200 status. I thought this might be due to my app.js not being in the async/await style, so I changed app.js to:

const readFilePromise = util.promisify(fs.readFile)

app.get('/*', async (req, res) => {
  try {
    await readFilePromise(path.join(__dirname, 'content', req.url) + '/index.md', 'utf8')
  } catch (err) {
      res.status(404).send('Page doesn\'t exist!')
  }
  try {
    const convertedData = md.render(data)
    await readFilePromise(path.join(__dirname, '/template.html'), 'utf8')
    data = data.replace(/\{\{content\}\}/, convertedData)
    res.send(data)
  } catch (err) {
      console.log(err)
  } 
})
app.listen(3000) 
module.exports = app

I tried running the test again, but it still fails with a 404. I think my set up within app.test.js is wrong, but I'm not sure exactly what, as I've tried using the various set ups as the article. How would I fix this?

Separately, when I try going to a URL using the async/await style in app.js, I get a ReferenceError: data is not defined error, but I'm not sure how to define data in the async/await format.

randhm
  • 55
  • 2
  • 9

1 Answers1

0

I explained here how to set up app for the test environment: supertest not found error testing express endpoint

You did not mention how you set the database environment, make sure your database is not empty. Then make your get request. but just checking status for get request is not enough because if your db is empty you will still get 200.

const response = await request(app).get("/route").send().expect(200);

expect(response.body.length).toBeGreaterThan(0)

Better approach would be connect to a different database, post your data first and then check the response

  const response = await request(app).get("/api/tickets").send().expect(200);

  expect(response.body.length).toEqual(2); // if you post two items

Also before you every test make sure you start with empty database inside beforeEach()

Yilmaz
  • 35,338
  • 10
  • 157
  • 202