简体   繁体   中英

Unit Testing using mocha, node

I'm new to unit testing in the Node world and am struggling with this: I've setup a after cb to delete the records I've added during my tests, however I keep getting an error Error: done() called multiple times every time I delete the record on the db. Here's my code:

after((done) => {
    User.deleteOne({email: user_email}, function(err, result) {
        if(err) console.log(err);
        console.log(result);
        done();
    });
});

If I do anything else (like just console something within the after block, I get no error at all.

What am I doing wrong?

Try with async/await style.

after(async () => {
   const deleteResult = await User.deleteOne({email: user_email});

   console.log(deleteResult);
});

With async/await you don't need to execute done, because mocha automatically handle promises.
More here and here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM