Jasmine custom matchers and before all

时间:2015-07-28 16:30:51

标签: javascript jasmine

When using Jasmine, is there a difference between adding a matcher in a beforeAll block vs a beforeEach block?

As far as I can tell, it works both ways. However, every documentation/tutorial I find online uses beforeEach. Is this simply because Jasmine's built-in beforeAll block is a relatively new feature, or is it avoided due to a potential pitfall when running the tests?

Using beforeAll makes more sense to me (why add the same matcher more than once?), but I'd like to make sure that I'm not exposing my tests to any problems.

1 个答案:

答案 0 :(得分:1)

有两个区别 - 1)执行顺序,以及2)变更持久的范围。我在工作中花了很多时间在Jasmine上进行测试,而beforeAllbeforeEach之间的差异总是表现得如此(好吧,除了bug我帮助他们解决了这个问题)。我发现使用beforeEach代替beforeAll的唯一原因是为了避免测试污染。对于自定义匹配器来说,这不是一个问题,所以继续使用beforeAll应该没问题。

执行顺序 - 在任何给定的describe块中,顺序如下:

  • beforeAll
  • beforeEach
  • 第一个嵌套itdescribe块(包括其设置和拆卸)
  • afterEach
  • 对于每个额外的嵌套itdescribe块,重复最后三个步骤
  • afterAll

我通过测试和this等来源证实了这一点。

范围 - 任何添加的变量,this关键字,spiescustom matchers(如上所述)的属性都将是持久的,并且可以在只要添加了它们的代码已经执行,整个块(包括嵌套块)中的给定块。例如,如果我在this.foo的{​​{1}}中设置beforeAll,则可以在describe describe beforeEach afterEach中访问afterAll 1}},it以及describethis.foo块。离开描述块后,Unless specified otherwise by an extension, frames have no semantic meaning. An intermediary might coalesce and/or split frames, if no extensions were negotiated by the client and the server or if some extensions were negotiated, but the intermediary understood all the extensions negotiated and knows how to coalesce and/or split frames in the presence of these extensions. One implication of this is that in absence of extensions, senders and receivers must not depend on the presence of specific frame boundaries. 将无法再访问。