How to call a test case from another test case in mocha?


Mocha does not sup­port call­ing test case inside another test case. So you can not do some­thing as shown below.


it('testA', () => {
    return doSomethingAsync(objA).then(() => {
        expect();
    });
});

it('testB', () => callTestA );


But in JavaScript world we can abstract the test­ing func­tion out­side the test case and call it from inside the test case as shown below.

function testAsync(obj) {
    return doSomethingAsync(obj).then(() => {
        expect();
    });
}

it('testA', () => testAsync(objA) );

it('testB', () => testAsync(objB) );


You may also like...