You can patch the standard describe() function, which will add the hook transparently.
const originalDescribe = global.describe
const describe = (title, callback) => {
originalDescribe(title, () => {
before(() => {
console.log('describe', title)
});
callback();
});
}
describe('Main', ()=> {
describe('Sub', () => {
it('some tests', ()=> {
console.log('it', 1)
})
it('some tests2', ()=> {
console.log('it', 2)
})
})
describe('Sub 2', () => {
it('some tests', ()=> {
console.log('it', 3)
})
})
})
Or another (simpler) approach is just to call you beforeDescribe() hook at the start of each describe() block.
const beforeDescribeHook = () => {
const title = Cypress.mocha.getRunner().suite.title;
console.log('describe', title)
}
describe('Main', ()=> {
before(beforeDescribeHook)
describe('Sub', () => {
before(beforeDescribeHook)
it('some tests', ()=> {
console.log('it', 1)
})
it('some tests2', ()=> {
console.log('it', 2)
})
})
describe('Sub 2', () => {
before(beforeDescribeHook)
it('some tests', ()=> {
console.log('it', 3)
})
})
})