[Unit Testing] Set the timeout of a Test in Mocha

Mocha uses a default timeout of 2000 ms. However, if for some reason that does not work for your use case, you can increase the timeout for a particular test. In this lesson, you will learn how to increase the timeout of the test using this.timeout() method.

 

const bankAccounts = [
    {
        name: 'John',
        id: 1,
        account: '347496844548723216',
        balance: 100
    },
    {
        name: 'Jane',
        id: 2,
        account: '447496844568723219',
        balance: 200
    }
]
module.exports = {
    getBalance(id, cb) {
        setTimeout(() => {
            const { balance } = bankAccounts.find(a => a.id === id);
            if (!balance) cb('could not find balance');
            cb(null, balance)
        }, 3200)
    }
}
const { expect } = require('chai');
const bankAccount = require('../modules/bank-account');

describe('BankAccount',function(){
    this.timeout(3500);
    it('should get the balance', function(done){
        bankAccount.getBalance(1,(err, balance) => {
            if(err){
                throw err;
            }
            expect(balance).to.equals(100);
            done();
        })
    })
})

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324843405&siteId=291194637