【问题标题】:jasmine IT block executes before 'beforeAll' blockjasmine IT 块在“beforeAll”块之前执行
【发布时间】:2018-06-28 21:44:05
【问题描述】:

在我的 VSCode 中的 node.js 项目中,我试图在运行规范之前读取配置信息。但是我的规范总是在我的“beforeAll”块之前首先执行。

beforeAll(() => {
    console.log('Step0………..: ');
    return new Promise(resolve => {
        console.log('Step1………..: ');

        browser.getProcessedConfig().then((config) => {            
            console.log('environment 12: ' );
            resolve(true);
        });    
    });
});

describe('*************************Executing TestSuite************************', function () {
    console.log('Step2………..: ');
    it("should support async execution of test preparation and expectations", function() {
        expect(3).toBeGreaterThan(0);
    });   
});//describe

我尝试简化代码以仅保留一个期望语句,但仍保持不变。

我得到的当前输出是 Step 2 , Step 0 , Step 1

我期待的是第 0 步、第 1 步、第 2 步

【问题讨论】:

    标签: javascript node.js jasmine protractor


    【解决方案1】:

    问题在于您的 beforeEach 函数中包含异步代码:您的 Promise 在开始运行您的第一个测试之前不会解析。

    Jasmine has a utility to handle asynchronous behaviour

    如果您将done 参数传递给您的 beforeEach 调用,则可以在您的承诺解决方案中调用此done() 函数。 在这里举个例子:

    beforeAll( (done) => {
                console.log('Step0………..: ');
                return new Promise(resolve => {
                    console.log('Step1………..: ');
    
                    browser.getProcessedConfig().then((config) => {
    
                        console.log('environment 12: ' );
                        resolve(true);
                        done()
                    });    
                });
    
            });
    
            describe('*************************Executing TestSuite************************', function () {
    
                console.log('Step2………..: ');
    
                it("should support async execution of test preparation and expectations", function() {
                    expect(3).toBeGreaterThan(0);
                });
    
            });//describe
    

    【讨论】:

    • 我运行了上面的代码。它对我不起作用。它打印旧的结果。
    • 尝试不使用嵌套的Promise(只需使用browser.getProcessedConfig),您不需要从beforeEach返回Promise
    • 还是不行。也许你可以用工作代码更新你的答案。谢谢。
    【解决方案2】:

    根据 jasmine 文档,生成的输出是正确的。 IT 块中的代码在 beforeAll 完成之前不会被执行。但是如果 beforeAll 有任何 promise,那么我们需要传递 done 关键字来让执行等待 beforeAll 异步操作完成,然后再执行 IT 块。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-01
      • 2019-03-28
      • 1970-01-01
      • 1970-01-01
      • 2021-05-13
      • 1970-01-01
      • 2020-06-08
      相关资源
      最近更新 更多