我的 JavaScript 断言库 return 如何通过?
How can my JavaScript assert library return a pass?
我正在开发自定义 Javascript 断言库,主要用于像 Mocha 这样的测试运行程序。
当测试失败时,库通过抛出错误正常工作。但是,当没有错误可抛出时,我不知道如何使库 return 成为 "pass"。
我曾尝试阅读其他库(如 Should 和 Chai)的源代码,但未能找到这方面的信息。
这是我的断言库,my-assert-lib.js
:
module.exports = function(input){
var methods = {
compare: function(comparison){
if(input != comparison){
throw new Error("The comparison string didn't match")
}
else{
return
}
}
}
return methods
}
这是我用 Mocha 执行的 test.js
:
var myAssertLib = require("./my-assert-lib.js")("correct");
describe("The comparison string", function(){
it('should match the library input', function(doc) {
myAssertLib.compare("incorrect")
});
it('should match the library input', function(doc) {
myAssertLib.compare("correct")
});
})
我得到这些结果:
0 passing (2s)
2 failing
1) The comparison string
should match the library input:
Error: The comparison string didn't match
at Object.compare (my-assert-lib.js:5:23)
at Context.<anonymous> (test.js:10:21)
2) The comparison string
should match the library input:
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
第一个测试立即退出,这是应该的。但是,第二次测试超时。当没有什么可抛出且测试运行器应该继续时,my-assert-lib.js
需要做什么?
我不能说为什么,但是 mocha 希望你调用 done
函数。
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
这样做会产生一次通过和一次失败,正如预期的那样。
describe("The comparison string", function(){
it('should match the library input', function(done) {
myAssertLib.compare("incorrect")
done()
})
it('should match the library input', function(done) {
myAssertLib.compare("correct")
done()
})
})
> The comparison string
> 1) should match the library input
> ✓ should match the library input
>
>
> 1 passing (14ms) 1 failing
>
> 1) The comparison string
> should match the library input:
> Error: The comparison string didn't match
> at Object.compare (assertLib.js:7:23)
> at Context.<anonymous> (test.js:7:21)
我正在开发自定义 Javascript 断言库,主要用于像 Mocha 这样的测试运行程序。
当测试失败时,库通过抛出错误正常工作。但是,当没有错误可抛出时,我不知道如何使库 return 成为 "pass"。
我曾尝试阅读其他库(如 Should 和 Chai)的源代码,但未能找到这方面的信息。
这是我的断言库,my-assert-lib.js
:
module.exports = function(input){
var methods = {
compare: function(comparison){
if(input != comparison){
throw new Error("The comparison string didn't match")
}
else{
return
}
}
}
return methods
}
这是我用 Mocha 执行的 test.js
:
var myAssertLib = require("./my-assert-lib.js")("correct");
describe("The comparison string", function(){
it('should match the library input', function(doc) {
myAssertLib.compare("incorrect")
});
it('should match the library input', function(doc) {
myAssertLib.compare("correct")
});
})
我得到这些结果:
0 passing (2s)
2 failing
1) The comparison string
should match the library input:
Error: The comparison string didn't match
at Object.compare (my-assert-lib.js:5:23)
at Context.<anonymous> (test.js:10:21)
2) The comparison string
should match the library input:
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
第一个测试立即退出,这是应该的。但是,第二次测试超时。当没有什么可抛出且测试运行器应该继续时,my-assert-lib.js
需要做什么?
我不能说为什么,但是 mocha 希望你调用 done
函数。
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
这样做会产生一次通过和一次失败,正如预期的那样。
describe("The comparison string", function(){
it('should match the library input', function(done) {
myAssertLib.compare("incorrect")
done()
})
it('should match the library input', function(done) {
myAssertLib.compare("correct")
done()
})
})
> The comparison string
> 1) should match the library input
> ✓ should match the library input
>
>
> 1 passing (14ms) 1 failing
>
> 1) The comparison string
> should match the library input:
> Error: The comparison string didn't match
> at Object.compare (assertLib.js:7:23)
> at Context.<anonymous> (test.js:7:21)