在node.js项目中的嘲笑测试中嘲笑(实际上,间谍)哨兵
我试图在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论