使 return 的脚本在 Mocha 中失败

Making the script to return fail in Mocha

我试图通过 accessing/redirecting 到 2 个链接来使脚本失败,然后再到达错误案例。但是我仍然在 mocha 中得到“1 passing”,然后在到达断言时出现 return 错误。我如何让他们等待脚本完全完成只在 mocha 中抛出错误?

  describe("youtube", function(){
    it("test successful login",function(){
        var driver = new webdriver.Builder().forBrowser("chrome").build();
        driver.get("http://www.google.com");

        const pageLoad = By.id('lga')
        return driver.wait(until.elementLocated(pageLoad)).then(()=>{

            return setTimeout(function(){
                driver.get("http://www.facebook.com");

                const signedPageLoad = By.id('pagelet_bluebar')

                return driver.wait(until.elementLocated(signedPageLoad)).then(()=>{
                      //assert.strictEqual(, message);


                        assert.equal(50, 70); /*AssertionError: 50 == 70 */

                })
            },1000)
        })
    })
})

您需要告诉 mocha 测试何时结束。您可以传递 done 并且 mocha 将等待测试结果直到该函数被调用

describe("youtube", function(){
    it("test successful login",function(done){
        var driver = new webdriver.Builder().forBrowser("chrome").build();
        driver.get("http://www.google.com");

        const pageLoad = By.id('lga')
        return driver.wait(until.elementLocated(pageLoad)).then(()=>{

            return setTimeout(function(){
                driver.get("http://www.facebook.com");

                const signedPageLoad = By.id('pagelet_bluebar')

                return driver.wait(until.elementLocated(signedPageLoad)).then(()=>{
                      //assert.strictEqual(, message);


                        assert.equal(50, 70); /*AssertionError: 50 == 70 */
                        done(); // call this function to tell mocha that you are done. 
                })
            },1000)
        })
    })
})