Is there a way to get Chai working with asynchronous Mocha tests?

后端 未结 13 2109
自闭症患者
自闭症患者 2020-11-29 21:06

I\'m running some asynchronous tests in Mocha using the Browser Runner and I\'m trying to use Chai\'s expect style assertions:

window.expect = chai.expect;
d         


        
相关标签:
13条回答
  • 2020-11-29 21:39

    I solved it extracting try/catch to a function.

    function asyncExpect(test, done){
        try{
            test();
            done();
        } catch(error){
            done(error);
        }
    }
    

    Then in it() I call:

    it('shall update a host', function (done) {
                testee.insertHost({_id: 'host_id'})
                    .then(response => {
                        asyncExpect(() => {
                            expect(response).to.have.property('ok', 1);
                            expect(response).to.have.property('nModified', 1);
                        }, done);
                    });
    
            });
    

    It's also debugable.

    0 讨论(0)
提交回复
热议问题