Let me start by saying I am quite new to node.js and mocha.It just breaks my head. I started using the tdd approach and I am trying to get the test that will start or has just finished from within the beforeEach and afterEach functions, but I've had no luck.(I am mostly interested in afterEach). At least I couldn't figure a neat way of doing it. The only thing I could think of was keeping the tests and the suite in a variable and then on afterEach() just do some matching to see which test has finished.
Ideally where it says 'test name' I want to have something like suite.test.name
suite('my test suite', function() {
beforeEach(function () {
console.log('test name');
});
test('first test', function (done) {
var testarray = ['1', '3', '5', '7'];
testarray.forEach(function(num, index) {
console.log('num: ' + num + ' index: ' + index);
},
done());
});
afterEach(){
console.log('test name');
}
}
The nice way to do this is to add a "test" npm script in package. json that calls mocha with the right arguments. This way your package. json also describes your test structure.
Retrying Mocha tests Mocha provides a this. retries() function that allows you specify the number of times a failed test can be retried. For each retry, Mocha reruns the beforeEach() and afterEach() Hooks but not the before() and after() Hooks.
A pending test in many test framework is test that the runner decided to not run. Sometime it's because the test is flagged to be skipped. Sometime because the test is a just a placeholder for a TODO. For Mocha, the documentation says that a pending test is a test without any callback.
You get the name of the current test with this.currentTest.title
afterEach(function(){
console.log(this.currentTest.title)
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With