Javascript 是茉莉花';在每次同步之前是什么?

Javascript 是茉莉花';在每次同步之前是什么?,javascript,jasmine,Javascript,Jasmine,如果在每个之前有多个,它们是否总是一个接一个地运行 beforeEach(function() {}); beforeEach(function() {}); beforeEach(function() {}); beforeEach(function() {}); beforeEach(function() {}); 看来他们会的。我试着用我的代码测试它: describe('Directive: Statement', function() { var el, scope, state

如果在每个之前有多个,它们是否总是一个接一个地运行

beforeEach(function() {});
beforeEach(function() {});
beforeEach(function() {});
beforeEach(function() {});
beforeEach(function() {});
看来他们会的。我试着用我的代码测试它:

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
  });
});

由于带有长
for
循环的
beforeach
在记录
3
的循环之前将其内容注销,因此我认为
beforeach
是同步运行的。是这样吗?

是的,每个之前的所有都将按照您定义的顺序执行

如果你深入了解Jasmine,你最终会:

添加
descripe
s时,
Suite
s将生成并嵌套。每个
套件
都用
this.beforeFns=[]
初始化,如您所见,该套件已添加到。请注意,
unshift
会添加到数组的左侧,因此您希望以后在每个s之前定义的
会首先运行。这时,Jasmine走到子套件的父级,在每个子套件之前收集所有的
列表,然后反转它们以按您想要的顺序运行

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
    };
  };
};

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

beforeEach(function(done) {
  setTimeout(function() {
    console.log('Async');
    done();
  }, 1000)
});
在运行时,Jasmine向您发送
done
函数并异步执行。(返回函数期望的参数数)


Jasmine还通过承诺和回调支持异步
beforeach
,所以我不认为这是一个非常好的观点,现在我正在深入研究如何工作。@DanPantry用异步行为更新了,我学到了很多。谢谢
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
    };
  };
};
beforeEach(function(done) {
  setTimeout(function() {
    console.log('Async');
    done();
  }, 1000)
});
for(iterativeIndex = recursiveIndex; iterativeIndex < length; iterativeIndex++) {
  var queueableFn = queueableFns[iterativeIndex];
  if (queueableFn.fn.length > 0) {
    attemptAsync(queueableFn);
    return;
  } else {
    attemptSync(queueableFn);
  }
}
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]);
  });
});