Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

136
Vistas
Does after block still excecute if before or it block fail in mocha test

If a mocha test looks like this

describe("create a user") {
    before("login to system") {}
    it("execute assertion") {}
    after("delete the user") {}
}

If either before or it block failed, does the after block still execute or no?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Yes, when test fails it will run after hook.

This code:

before("login to system", () => {
  console.log("BEFORE")
  throw new Error('error');
})

it('testing...', (done) => {
  console.log("IT")
  done();
});

after("delete the user", () => {
  console.log("AFTER")
})

Output this:

BEFORE
    1) "before all" hook: login to system for "testing..."
AFTER


  0 passing (14ms)
  1 failing

  1) States Get
       "before all" hook: login to system for "testing...":
     Error: error
      at Context.<anonymous> (test\index.js:22:11)
      at processImmediate (node:internal/timers:463:21)

Also you can check this github discussion: #1612 where here says:

Please see the example below, showing that after is correctly invoked after the beforeEach failure

The code

// example.js
before(function() {
  console.log('before');
});

after(function() {
  console.log('after');
});

describe('suite', function() {
  beforeEach(function() {
    throw new Error('In beforeEach');
  });

  afterEach(function() {
    console.log('afterEach');
  });

  it('test', function() {
    // example
  });
});

And the output.

// Test run
$ mocha example.js

  before

  ․afterEach
after


  0 passing (7ms)
  1 failing

  1) suite "before each" hook:
     Error: In beforeEach
      at Context.<anonymous> (/Users/dstjules/git/mocha/example.js:11:11)
      at callFn (/usr/local/lib/node_modules/mocha/lib/runnable.js:251:21)
      at Hook.Runnable.run (/usr/local/lib/node_modules/mocha/lib/runnable.js:244:7)
      at next (/usr/local/lib/node_modules/mocha/lib/runner.js:259:10)
      at Immediate._onImmediate (/usr/local/lib/node_modules/mocha/lib/runner.js:276:5)
      at processImmediate [as _immediateCallback] (timers.js:358:17)
about 4 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 Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda