How to pass next as an argument on mocha test?

Member

by lily , in category: Javascript , 2 months ago

How to pass next as an argument on mocha test?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by elise_daugherty , 2 months ago

@lily 

In order to pass next as an argument in a Mocha test, you can simply declare it as a callback function in your test case.


For example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
describe('your test case', function() {
    it('should call next with no errors', function(done) {
        function next(err) {
            if (err) {
                done(err);
            } else {
                done(); // call done if everything is okay
            }
        }
        
        // Call your function that expects next as an argument
        yourFunction(next);
    });
});


In this example, next is declared within the test case and is passed as an argument to yourFunction. The done callback function is then used to complete the test once next has been called with no errors.