• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Pruebas Online
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

185
Vistas
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())
  })
})
almost 3 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I have found two issues with the provided tests

  1. .finally(done()) finishes the test as successful immediately since .finally is expecting a callback and done is called in place instead, .finally(done) or .finally(() => done()) might work as you intended them to.

  2. intercepting the errors via try/catch is a really bad practice and your implementation will swallow potential errors while marking the test as passing, even though it failed, it is much better to use mocha`s test context for this is an afterEach hook, for example:

const errors = [];

afterEach(function () {
    if (this.currentTest.state === "failed") {
        errors.push(this.currentTest.title)
    }
});

It is also not required to call done when working with promises in mocha. A promise can be simply returned and mocha will pass the test if and when the promise resolves, fail it otherwise (if the error is uncaught).

it("...", () => {
  return request(...).get(...).then(...)
}

or using async/await, which returns a promise implicitly

it("...", async () => {
  const result = await request(...).get(...)

  // Do assertions on result
  ...

  // No need to return anything here
}
almost 3 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda