问题描述
我在 express 代码库中使用 Jest 作为测试运行程序。我在运行一个测试用例时收到一个错误。它在 PHPUnserialize 行抛出错误。我不明白如何解决这个问题,因为我尝试修改超时但无济于事。
测试用例
it("Returns data from cache",async () => {
//perform the request to the api which will now be intercepted by nock
const res = await request(app).get(
"/drupal/en/v1/api/article/2?_format=json"
);
//assert that the mocked response is returned
expect(res.status).toEqual(200);
});
错误
at Console.log (node_modules/winston/lib/winston/transports/console.js:79:23)
console.error
Error: Uncaught [TypeError: PHPUnserialize.unserialize is not a function]
at reportException (/Users/anandabhishe/gitlab/wellness/wellbeing-proxy/node_modules/jsdom/lib/jsdom/living/helpers/runtime-script-errors.js:62:24)
at Timeout.task [as _onTimeout] (/Users/anandabhishe/gitlab/wellness/wellbeing-proxy/node_modules/jsdom/lib/jsdom/browser/Window.js:396:9)
at listOnTimeout (internal/timers.js:554:17)
at processTimers (internal/timers.js:497:7) TypeError: PHPUnserialize.unserialize is not a function
at apply (/Users/anandabhishe/gitlab/wellness/wellbeing-proxy/routes/drupal.js:39:37)
at tryCatcher (/Users/anandabhishe/gitlab/wellness/wellbeing-proxy/node_modules/standard-as-callback/built/utils.js:11:23)
at /Users/anandabhishe/gitlab/wellness/wellbeing-proxy/node_modules/standard-as-callback/built/index.js:19:23
at processTicksAndRejections (internal/process/task_queues.js:93:5)
测试中的一段代码
function cache(req,res,next) {
const url = req.url;
var pos = url.indexOf("?");
var type = req.private == true ? "?type=private" : "?type=public";
var cachekey = cacheKeyPrefix.concat(url.slice(4,pos)).concat(type);
drupalLogger.info("<<<< Cache Key >>>>",cachekey);
client.hgetall(cachekey,(err,data) => {
if (err) {
if (err instanceof Redis.ReplyError) {
drupalLogger.info("<<<< Redis Reply Error >>>>",err.message);
res.status(500).json({
error: err.name,error_description: err.message,});
return;
} else {
drupalLogger.info("<<<< Redis Connection Error >>>>",err.message);
req.redisErr = true;
return next(err);
}
}
if (Object.keys(data).length !== 0 && data !== null) {
drupalLogger.info("<<<< Found in Cache >>>>",typeof data);
var datasend = PHPUnserialize.unserialize('a:0:{}');
res.send(datasend);
} else {
drupalLogger.info("<<<< Not Found in Cache >>>>");
next();
}
});
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)