Aug 25, 2023.
By default, Mocha’s BDD user interface includes a worldwide afterEach()
feature
You can call afterEach()
with a feature, as well as Mocha will certainly perform that feature after every examination in the collection.
You need to make use of afterEach()
to tidy up any type of relentless state your examination produced, like access conserved in your data source or calling sinon.restore()
afterEach( feature() {
console log(' Running afterEach!');.
} );.
it(' test1', feature() {
console log(' test1');.
} );.
it(' test2', feature() {
console log(' test2');.
} );
afterEach()
resembles beforeEach()
, yet follows every examination instead of in the past.
With explain()
explain()
allows you extent afterEach()
hooks.
If you specify a afterEach()
in a explain()
, Mocha will certainly not run that afterEach()
on any type of examinations beyond that explain()
afterEach( feature() {
console log(' Running international afterEach!');.
} );.
explain(' my examination collection', feature() {
afterEach( feature() {
console log(' Running my examination collection afterEach!');.
} );.
it(' test1', feature() {} );.
it(' test2', feature() {} );.
} );.
it(' test3', feature() {} );
So a worldwide afterEach()
will certainly pursue every examination, also examinations within a explain()
Yet a afterEach()
hook within a explain()
will certainly not work on any type of examination beyond that explain()
Avoided Examinations
Mocha will certainly not run afterEach()
if there are no examinations.
As an example, if you run a solitary Mocha examination utilizing just()
, Mocha will certainly not perform the afterEach()
incorporate the complying with examination data.
explain(' my examination collection', feature() {
afterEach( feature() {
console log(' Running my examination collection afterEach!');.
} );.
it(' test1', feature() {} );.
it(' test2', feature() {} );.
} );.
it.only(' test3', feature() {} );