confuse about the execution order of nested testing suite and specs

北慕城南 提交于 2019-12-10 23:13:27

问题


All:

I just start the second day study about Jasmine, there is one question about exe order I want to figure out:

This example is from Jasmine 2.0 introduction: Jasmine 2.0 Introduction

describe("Asynchronous specs", function() {
  var value;
  beforeEach(function(done) {
    setTimeout(function() {
      value = 0;
      done();
    }, 1);
  });
  it("should support async execution of test preparation and expectations", function(done) {
    value++;
    expect(value).toBeGreaterThan(0);
    done();
  });
  describe("long asynchronous specs", function() {
    var originalTimeout;
    beforeEach(function() {
      originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
      jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
    });
    it("takes a long time", function(done) {
      setTimeout(function() {
        done();
      }, 9000);
    });
    afterEach(function() {
      jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout;
    });
  });
});

I tried reading the source, but it only makes me more confused that I even confuse which part I do not understand....ToT

Could anyone just brief explain what does jasmine do when it meet describe beforeEach and it, and how it runs t?

Thanks


回答1:


describe is like a "test-scope" and it's used to determine on which its beforeAll, afterAll, beforeEach and afterEach has to execute, it can also be used simply to categorize your tests because it's a bit like a "chapter", with a title.

The first beforeEach is executed before every it in the first describe (and the describe children).

The first it could be written without the done parameter since it contains only synchronous operations.

Then beforeEach in the "child" describe is executed before each test case inside, then the it inside, and finally the afterEach, is only executed after each it inside the child describe.

To sum up, In your exemple the functions are executed in this order :

beforeEach1 > it1 > beforeEach1 > beforeEach2 > it2 > afterEach

When you pass the done parameter, jasmine "waits" it's execution to proceed to the next test case (it). As you can see in the last case, jasmine has to wait 9000ms to execute done(): by default, jasmine timeouts after 5000ms, that's why we change it to 10000.

I hope it was clear, if it wasn't enough, feel free to ask for details :)



来源:https://stackoverflow.com/questions/32596649/confuse-about-the-execution-order-of-nested-testing-suite-and-specs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!