摩卡如何确定嵌套级别?
目前我正试图了解如何BDD DSL在摩卡工作,我卡住了。 我想要这个方法,并想要应用这个。
例如,下面的testing:
describe('foo', function(){ describe('bar', function(){ it('should be something') }); });
会产生输出:
foo bar - should be something 0 passing (4ms) 1 pending
问题:嵌套块中的全局函数describe
如何describe
确定为嵌套? 我查看了源代码,但现在无法处理主要的想法。
摩卡(Mocha)在套件中跟踪这些事情,就像你从源头上看到的一样
/** * Describe a "suite" with the given `title` * and callback `fn` containing nested suites * and/or tests. */ context.describe = context.context = function(title, fn){ var suite = Suite.create(suites[0], title); suite.file = file; suites.unshift(suite); fn.call(suite); suites.shift(); return suite; };
为了简化一点,对于每一个describe
,摩卡创build一个新的套件。 套房可以包含其他套房。
对于你的例子,摩卡创buildfoo
套件,然后包含bar
套件,其中包含should be something
testing的should be something
。
这可以通过全局树数据结构来实现。
当你调用describe时 ,mocha添加一个节点,当你调用它时 ,mocha添加一个叶子。
当根描述调用返回时,树完成,摩卡一个接一个地遍历执行叶的节点。