【发布时间】:2022-10-20 02:54:26
【问题描述】:
我正在使用浏览器版本的 Mocha 编写一些 Mocha/Chai 测试。如果我更改嵌套描述的任何字符串参数,Mocha 将失败而不会在控制台中引发任何错误。这是工作代码:
const fetchTestHTML = (path) => {
//fetchJSON is a function from my code. It fetch's a file and parses using the provided parseMethod ('text' in this case). I really need to rename this function since it no longer only fetches JSON files.
return fetchJSON(path,{},'text').then(text => $(text));
}
let context;
before(async ()=>{
context = await fetchTestHTML('tests/html/statblock.test.html');
});
describe('Testing extraction of child nodes',()=>{
describe('Getting self',() => {
it('Given a selector of `&`, it should return the passed JQuery Collection',()=>{
//findMatchingNodes is the function I am testing.
findMatchingNodes('&',context,context).should.deep.equal(context);
});
});
});
以上为我提供了一个正确的测试运行,并带有通过/失败输出(在这种情况下为成功)。但是,如果我更改任何有关传递给 describe() 调用的文本的任何内容,Mocha 就会失败(页面只是空的)。例如,只需将s 中的self 大写:
const fetchTestHTML = (path) => {
//fetchJSON is a function from my code. It fetch's a file and parses using the provided parseMethod ('text' in this case). I really need to rename this function since it no longer only fetches JSON files.
return fetchJSON(path,{},'text').then(text => $(text));
}
let context;
before(async ()=>{
context = await fetchTestHTML('tests/html/statblock.test.html');
});
describe('Testing extraction of child nodes',()=>{
describe('Getting Self',() => {
it('Given a selector of `&`, it should return the passed JQuery Collection',()=>{
findMatchingNodes('&',context,context).should.deep.equal(context);
});
});
});
这导致 mocha 测试 div 根本没有被填充。据我所知,没有运行任何测试。
如果我删除任一描述调用,Mocha 环境也会失败:
const fetchTestHTML = (path) => {
//fetchJSON is a function from my code. It fetch's a file and parses using the provided parseMethod ('text' in this case). I really need to rename this function since it no longer only fetches JSON files.
return fetchJSON(path,{},'text').then(text => $(text));
}
let context;
before(async ()=>{
context = await fetchTestHTML('tests/html/statblock.test.html');
});
describe('Getting self',() => {
it('Given a selector of `&`, it should return the passed JQuery Collection',()=>{
findMatchingNodes('&',context,context).should.deep.equal(context);
});
});
而且,如果我在第一个描述中添加任何带有不同文本的附加描述; Mocha 环境运行,但不运行附加测试:
const fetchTestHTML = (path) => {
return fetchJSON(path,{},'text').then(text => $(text));
}
let context;
before(async ()=>{
context = await fetchTestHTML('tests/html/statblock.test.html');
});
describe('Testing extraction of child nodes',()=>{
describe('Testing findNextNode',()=>{
it('Should return the adjacent element',()=>{
findNextNode($('#_idIndexMarker021'),context).should.deep.equal($('#_idTextAnchor003',context));
});
});
describe('Getting self',() => {
it('Given a selector of `&`, it should return the passed JQuery Collection',()=>{
findMatchingNodes('&',context,context).should.deep.equal(context);
});
});
});
导致Getting self 内的测试运行,但Testing findNextNode 内的测试没有运行。如果我将附加描述块的描述文本更改为Getting self,它将正确运行(在这种情况下测试失败):
const fetchTestHTML = (path) => {
return fetchJSON(path,{},'text').then(text => $(text));
}
let context;
before(async ()=>{
context = await fetchTestHTML('tests/html/statblock.test.html');
});
describe('Testing extraction of child nodes',()=>{
describe('Getting self',()=>{
it('Should return the adjacent element',()=>{
findNextNode($('#_idIndexMarker021'),context).should.deep.equal($('#_idTextAnchor003',context));
});
});
describe('Getting self',() => {
it('Given a selector of `&`, it should return the passed JQuery Collection',()=>{
findMatchingNodes('&',context,context).should.deep.equal(context);
});
});
});
我还有其他奇怪的测试用例,但是这篇文章有点长。我只想说,如果我做的不是我的描述格式(使用这些确切的字符串):
describe('Testing extraction of child nodes',()=>{
describe('Getting self',() => {
//Test(s) for `Getting self`
});
});
然后部分或全部测试无法运行。有人知道这里发生了什么吗?我在另一个项目中将 Mocha 用于 TDD,但从未有过类似的东西。
【问题讨论】:
标签: javascript browser mocha.js chai