在 Node.js 中使用 jest 和sentry-testkit 进行单元测试。 运行测试时出现错误 TypeError: Cannot readproperties of undefined (reading 'sentry')
import { describe, expect, it } from '@jest/globals';
import * as Sentry from '@sentry/node';
import sentryTestKit from 'sentry-testkit';
const { testkit, sentryTransport } = sentryTestKit();
Sentry.init({
dsn: 'my-dsa',
transport: sentryTransport,
});
describe('Sentry Test', () => {
describe('Error Capture', () => {
it('should be able to catch the error', () => {
expect(testkit.reports()).toHaveLength(1);
});
});
});
我这样修复了错误:
import { init} from "@sentry/node";
init({
dsn: 'my-dsa',
transport: sentryTransport,
});