【问题标题】:Jest / NodeJS, how to build a test for "unhandledRejection"Jest / NodeJS,如何为“unhandledRejection”构建测试
【发布时间】:2020-06-14 08:17:24
【问题描述】:

我正在尝试实现 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();
  });
});

但测试失败:thrown: 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;

这是在 unhandledRejection 情况下触发的错误中间件:

// 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.");
}

感谢任何帮助!

【问题讨论】:

标签: node.js express jestjs winston


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    在玩弄了代码之后,我使它的工作如下:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-29
      • 2018-04-28
      相关资源
      最近更新 更多