Saturday 7 August 2021

Mocha Chai basic GET request not recording passes and fails correctly

I'm quite new to mocha.js so trying to get something fairly basic and reliable working with a minimal example.

I'm trying to do a GET request to an example JSON API and run 3 tests on it, all should have different results but all are getting different results than expected.

Additionally I'm trying to catch and count/report on the errors that occur through the tests to output details after() all tests are run in order not to clutter up the UI.

For the 3 tests I've got only 1 should be passing. The problem is that if I include a .finally() call then all of them pass, if I remove the .finally() call and have only the .catch() call then all of them fail. Not sure what I'm missing.

let errors = []

// After All output error details
after((done) => { 
  console.log("Total Errors: " + errors.length)
  // console.log(errors)
  done()
})


// Example Test Suite
describe("Example Test Suite", () => {

  it("#1 Should Pass on expect 200", (done) => {
    request("https://jsonplaceholder.typicode.com/")
      .get("todos/1")
      .expect(200)
      .catch(function (err) {
        errors.push(err)
        done(err)
      })
      .finally(done())
  })

  it("#2 Should Fail on wrong content type", (done) => {
    request("https://jsonplaceholder.typicode.com/")
      .get("todos/1")
      .expect("Content-Type", "FAIL")
      .catch(function (err) {
        errors.push(err)
        done(err)
      })
      .finally(done())
  })

  it("#3 Should Fail on contain.string()", (done) => {
    request("https://jsonplaceholder.typicode.com/")
      .get("todos/1")
      .then(function (err, res) {
        if (err) {done(err)} else {
          try {
            expect(res.text).to.contain.string("ThisShouldFail");
          } catch(e) {
            errors.push({error: e, response: res})
            done(err)
          } 
          finally {
            done()
          }
        }
      }).catch(function (err) {
        errors.push(err)
        done(err)
      })
      .finally(done())
  })
})


from Mocha Chai basic GET request not recording passes and fails correctly

No comments:

Post a Comment