gpt4 book ai didi

javascript - 抛出错误时不调用 Jasmine afterAll

转载 作者:行者123 更新时间:2023-12-04 18:03:32 24 4
gpt4 key购买 nike

我正在使用 Jasmine 2.3.1。以下规范执行导致 afterAll 方法未被调用:

var http = require('http');

describe("simple server tests:", function() {

afterAll(function(done) {
console.log('after all');
});

it("throws an error because server is not running", function(done) {
http.get("http://127.0.0.1:8080", function(res) {
res.on('data', function(data) {
done();
});
});
});
});

控制台显示:

[23:25:24] Starting 'test'...
events.js:85
throw er; // Unhandled 'error' event
^
Error: connect ECONNREFUSED
at exports._errnoException (util.js:746:11)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1010:19)

我的期望是无论测试方法中抛出的错误如何,都会调用 afterAll。我真的不想在我的测试中试一试。如果这是 Jasmine 的问题或我对 Jasmine 的使用,请告诉我。

最佳答案

afterAll 在所有规范都已完成时调用,但如果您的情况服务器未运行,因此永远不会调用 done()。如果您正在测试服务器未运行,我建议为请求添加错误处理程序并在其中执行 done():

var http = require('http');

describe("simple server tests:", function() {

afterAll(function(done) {
console.log('after all');
done(); // do not forget to execute done if you use it
});

it("throws an error because server is not running", function(done) {

http.get("http://127.0.0.1:8080", function(res) {
res.on('data', function(data) {
// never called
})
}).on('error', function(e) {
console.log("Got error: " + e.message);
done(); // finish async test
});
});
});

关于javascript - 抛出错误时不调用 Jasmine afterAll,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31093704/

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