gpt4 book ai didi

javascript - Mocha 如何确定嵌套级别?

转载 作者:行者123 更新时间:2023-11-30 05:32:43 24 4
gpt4 key购买 nike

目前我正在尝试了解 BDD DSL 在 Mocha 中的工作原理,但我被卡住了。我喜欢这种方法并想应用它。

例如下面的测试:

describe('foo', function(){
describe('bar', function(){
it('should be something')
});
});

将产生输出:

foo
bar
- should be something


0 passing (4ms)
1 pending

问题:嵌套 block 中全局函数describe的调用如何判定为嵌套?我查看了源代码,但现在无法处理主要思想。

最佳答案

Mocha 在 Suites 中跟踪这些事情,您可以从 source 中看到

/**
* 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,Mocha 创建一个新套件。套件可以包含其他套件。

对于您的示例,Mocha 创建了 foo 套件,然后包含 bar 套件,其中包含 should be something 测试。

关于javascript - Mocha 如何确定嵌套级别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25923750/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com