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

后端 未结 13 2173
自闭症患者
自闭症患者 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:24

    Timers during tests and async sounds pretty rough. There is a way to do this with a promise based approach.

    const sendFormResp = async (obj) => {
        const result = await web.chat.postMessage({
            text: 'Hello world!',
        });
       return result
    }
    

    This async function uses a Web client (in this case it is Slacks SDK). The SDK takes care of the asynchronous nature of the API call and returns a payload. We can then test the payload within chai by running expect against the object returned in the async promise.

    describe("Slack Logic For Working Demo Environment", function (done) {
        it("Should return an object", () => {
            return sdkLogic.sendFormResp(testModels.workingModel).then(res => {
                expect(res).to.be.a("Object");
            })
        })
    });
    

提交回复
热议问题