本文介绍了Jasmine的beforeEach同步吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您有多个 beforeEach ,那么它们会一个接一个地运行吗?

If you have multiple beforeEach's, will they always run one after another?

beforeEach(function() {});
beforeEach(function() {});
beforeEach(function() {});
beforeEach(function() {});
beforeEach(function() {});

似乎他们会。我尝试用我的代码测试它:

It seems that they will. I tried testing it with my code:

describe('Directive: Statement', function() {
  var el, scope, statements;

  beforeEach(module('simulatedSelves'));
  beforeEach(module('templates'));
  beforeEach(inject(function($compile, $rootScope) {
    console.log(1);
    scope = $rootScope.$new();

    statements = [];
    scope.statement = {
      text: 'test',
      arr: []
    };
    scope.statement.parent = statements;
    statements.push(scope.statement);

    el = angular.element('<statement statement=statement></statement>');
    $compile(el)(scope);
    scope.$digest();
  }));
  beforeEach(function() {
    var counter = 0;
    console.log(2);
    for (var i = 0; i < 1000000; i++) {
      counter++;
    }
    console.log(counter);
  });
  beforeEach(function() {
    console.log(3);
  });

  it('test statement has correct properties', function() {
    // stuff
  });
});

它记录:

1
2
1000000
3

由于 beforeEach 与的长循环在记录之前记录其内容3 ,我认为 beforeEach 同步运行。这是真的吗?

Since the beforeEach with the long for loop logs its stuff out before the one that logs 3, I'm thinking that the beforeEach's run synchronously. Is that true?

推荐答案

是的,所有 beforeEach s将在你定义的顺序。

Yes, all beforeEachs will execute in the order you define.

如果你钻进Jasmine,你最终会得到:

If you drill into Jasmine, you eventually get to this definition:

Suite.prototype.beforeEach = function(fn) {
  this.beforeFns.unshift(fn);
};

当您添加 describe s, Suite s生成并嵌套。每个套件都会使用 this.beforeFns = [] 进行初始化,如您所见,它会被添加到。请注意, unshift 会添加到数组的左侧,因此您希望以后定义的 beforeEach s先跑。那是当Jasmine走向儿童套房的父母时,收集所有 beforeEach 列表,然后将其反转以按照您想要的顺序运行。

As you add describes, Suites get generated and nested. Each Suite is initialized with this.beforeFns = [], which is added to as you can see. Note that unshift adds to the left side of the array, so you'd expect later-defined beforeEachs to be run first. That's fixed later when Jasmine walks up the child suite's parents, gathers all beforeEach lists, and then reverses them to run in the order you want.

var beforeAndAfterFns = function(suite) {
  return function() {
    var befores = [],
      afters = [];

    while(suite) {
      befores = befores.concat(suite.beforeFns);
      afters = afters.concat(suite.afterFns);

      suite = suite.parentSuite;
    }

    return {
      befores: befores.reverse(),
      afters: afters
    };
  };
};






as ,我们到目前为止假设所有 beforeEach 都是同步的。从Jasmine 2开始,您可以设置异步 beforeEach ,如下所示:


As Dan points out, we've so far assumed that all of your beforeEachs are synchronous. As of Jasmine 2, you can set up asynchronous beforeEachs like so:

beforeEach(function(done) {
  setTimeout(function() {
    console.log('Async');
    done();
  }, 1000)
});

在运行时,Jasmine发送已完成函数并异步执行。 (返回号码函数期望的参数。

At runtime, Jasmine sends you the done function and executes asynchronously if your function takes an argument. (Function.length returns the number of arguments the function expects).

for(iterativeIndex = recursiveIndex; iterativeIndex < length; iterativeIndex++) {
  var queueableFn = queueableFns[iterativeIndex];
  if (queueableFn.fn.length > 0) {
    attemptAsync(queueableFn);
    return;
  } else {
    attemptSync(queueableFn);
  }
}

attemptAsync 等待你在 QueueRunner 移动到下一个 done() > beforeEach hook,所以订购仍然有效!非常整洁。

attemptAsync waits for you to call done() before the QueueRunner moves on to the next beforeEach hook, so ordering still works! Pretty neat.

describe('beforeEach', function() {
  var data = null;

  beforeEach(function() { data = []; });
  beforeEach(function() { data.push(1); });
  beforeEach(function(done) {
    setTimeout(function() {
      data.push('Async');
      done();
    }, 1000);
  });
  beforeEach(function() { data.push(2); });
  beforeEach(function() { data.push(3); });

  it('runs in order', function(){
    expect(data).toEqual([1, 'Async', 2, 3]);
  });
});

这篇关于Jasmine的beforeEach同步吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 23:47