在node.js项目中的嘲笑测试中嘲笑(实际上,间谍)哨兵

发布于 2025-02-07 07:45:21 字数 2369 浏览 2 评论 0原文

我试图在Node.js项目中与Jest嘲笑(真的是Spyon)哨兵,但它无法正常工作。

/debug-sentry路由只是抛出异常,并与之正确地将异常发送给Sentry。但是,当我尝试这样模拟时,期望... tohavebeencalded失败。我不是嘲笑这个权利吗?建议?

import * as Sentry from '@sentry/node';
...
describe('failure modes', () => {
  it('send an exception to Sentry if exception thrown', async () => {
    const mock = jest.spyOn(Sentry, 'captureException');
    const res = await request(server).get('/debug-sentry').expect('Content-Type', /text/).expect(500);
    expect(mock).toHaveBeenCalled(); // This does not succeed, not sure why...
...

正在测试的代码(我刚刚添加了/debug-sentry临时用于测试服务器的路由)

import express from 'express';
import { Express } from 'express-serve-static-core';

import { requestLogger } from '@/utils/request_logger';
import {
  initialize_exception_reporting,
  initialize_error_reporting,
  send_exception_message
  } from '@/utils/exception_reporting';

export async function createServer(): Promise<Express> {
  const server = express();

  initialize_exception_reporting(server);

  server.use(requestLogger);

  // error customization, if request is invalid
  // eslint-disable-next-line @typescript-eslint/no-explicit-any,@typescript-eslint/no-unused-vars
  server.use((err: any, req: express.Request, res: express.Response, next: express.NextFunction) => {
    res.status(err.status).json({
      error: {
        type: 'request_validation',
        message: err.message,
        errors: err.errors,
      },
    });
  });

  server.get("/debug-sentry", (req, res) => {
    throw new Error("My first Sentry error!");
  });

  initialize_error_reporting(server);

  return server;
}

和设置SENTRY的代码:

import Express from 'express';
import * as Sentry from "@sentry/node";
import * as Tracing from "@sentry/tracing";

export function initialize_exception_reporting(server: Express.Express) {
  Sentry.init({
    integrations: [],
  });
  server.use(Sentry.Handlers.requestHandler());
}

export function initialize_error_reporting(server: Express.Express) {
  server.use(Sentry.Handlers.errorHandler());
}

export function send_exception_message(message: string) {
  Sentry.captureMessage(message);
}

I'm trying to mock (well really, spyOn) Sentry with jest in a Node.js project and it is not working.

The /debug-sentry route just throws an exception, and interactively it correctly sends an exception to Sentry. When I try to mock it like this, though, the expect...toHaveBeenCalled fails. Am I not mocking this right? Suggestions?

import * as Sentry from '@sentry/node';
...
describe('failure modes', () => {
  it('send an exception to Sentry if exception thrown', async () => {
    const mock = jest.spyOn(Sentry, 'captureException');
    const res = await request(server).get('/debug-sentry').expect('Content-Type', /text/).expect(500);
    expect(mock).toHaveBeenCalled(); // This does not succeed, not sure why...
...

The code under test (I just added the /debug-sentry route to my server temporarily for testing):

import express from 'express';
import { Express } from 'express-serve-static-core';

import { requestLogger } from '@/utils/request_logger';
import {
  initialize_exception_reporting,
  initialize_error_reporting,
  send_exception_message
  } from '@/utils/exception_reporting';

export async function createServer(): Promise<Express> {
  const server = express();

  initialize_exception_reporting(server);

  server.use(requestLogger);

  // error customization, if request is invalid
  // eslint-disable-next-line @typescript-eslint/no-explicit-any,@typescript-eslint/no-unused-vars
  server.use((err: any, req: express.Request, res: express.Response, next: express.NextFunction) => {
    res.status(err.status).json({
      error: {
        type: 'request_validation',
        message: err.message,
        errors: err.errors,
      },
    });
  });

  server.get("/debug-sentry", (req, res) => {
    throw new Error("My first Sentry error!");
  });

  initialize_error_reporting(server);

  return server;
}

and the code setting up Sentry:

import Express from 'express';
import * as Sentry from "@sentry/node";
import * as Tracing from "@sentry/tracing";

export function initialize_exception_reporting(server: Express.Express) {
  Sentry.init({
    integrations: [],
  });
  server.use(Sentry.Handlers.requestHandler());
}

export function initialize_error_reporting(server: Express.Express) {
  server.use(Sentry.Handlers.errorHandler());
}

export function send_exception_message(message: string) {
  Sentry.captureMessage(message);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文