Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Jest/NodeJS,如何为“构建测试”;未经处理的弹射“;_Node.js_Express_Jestjs_Winston - Fatal编程技术网

Node.js Jest/NodeJS,如何为“构建测试”;未经处理的弹射“;

Node.js Jest/NodeJS,如何为“构建测试”;未经处理的弹射“;,node.js,express,jestjs,winston,Node.js,Express,Jestjs,Winston,我正在努力争取100%的保险 我一直在为代码的这一部分创建测试: // Manually throwing the exception will let winston handle the logging process.on("unhandledRejection", (ex) => { throw ex; }); 我尝试使用以下方法: const logger = require("../../../startup/logging"); describe("startup /

我正在努力争取100%的保险

我一直在为代码的这一部分创建测试:

// Manually throwing the exception will let winston handle the logging
process.on("unhandledRejection", (ex) => {
  throw ex;
});
我尝试使用以下方法:

const logger = require("../../../startup/logging");

describe("startup / logging", () => {
  it("should log the error in case of unhandledRejection", () => {
    process.emit("unhandledRejection");
    const loggerError = jest.spyOn(logger, "error");
    expect(loggerError).toHaveBeenCalled();
  });
});
但是测试失败,抛出:
undefined

这是logging.js的完整代码:

const { createLogger, format, transports } = require("winston");
require("winston-mongodb");
// This will forward the error in the pipeline to our error handler
require("express-async-errors");

// Manually throwing the exception will let winston handle the logging
process.on("unhandledRejection", (ex) => {
  throw ex;
});

// Log to files
const logger = createLogger({
  format: format.combine(
    format.timestamp({
      format: 'YYYY-MM-DD HH:mm:ss'
    }),
    format.errors({ stack: true }),
    format.splat(),
    format.json()
  ),
  transports: [
    new transports.File({filename: "./logs/combined.log", level: "verbose"}),
  ],
  transports: [
    new transports.File({filename: "./logs/error.log", level: "error"}),
    new transports.File({filename: "./logs/combined.log"}),
  ],
  exceptionHandlers: [
    new transports.File({ filename: "./logs/exceptions.log" }),
    new transports.File({ filename: "./logs/combined.log" }),
  ],
  handleExceptions: true,
});

// Log to database
logger.add(new transports.MongoDB({
  level: "error",
  db: "mongodb://localhost:27017/rest-api-mongodb",
  options: {
    useUnifiedTopology: true,
    useNewUrlParser: true,
  },
  metaKey: "stack",
  handleExceptions: true,
}));

module.exports = logger;
这是在出现未处理的弹出时触发的错误中间件:

// Winston is a logger, it allows to store errors in a log and mongoDB
const logger = require("../startup/logging");

// This function will handle all errors in the router
// It works thanks to require("express-async-errors"); that forwards the error in the pipeline

// It does not work outside of the context of express
module.exports = function (err, req, res, next) {
  logger.error(err.message, err);
  // error, warn, info, berbose, debug, silly
  res.status(500).send("Something on the server failed.");
}

感谢您的帮助

尝试使用真正的未处理拒绝触发
未处理的拒绝
。例如,在不附加处理程序的情况下调用
Promise.reject()

在对代码进行处理后,我使其工作如下:

require("../../../startup/logging");

describe("startup / logging", () => {
  it("should throw an error if there is an unhandledRejection", () => {
    const emitUnhandledRejection = () => process.emit("unhandledRejection");
    expect(emitUnhandledRejection).toThrow();
  });
});