Skip to content

Commit 1f3fc98

Browse files
authored
fix(core): Check that functions have been called in beforeSend and beforeSendTransaction tests (#6150)
Per @Lms24's suggestion[1], this adds a check to make sure the relevant function is being called in our `beforeSend` and `beforeSendTransaction` tests. [1] #6121 (comment)
1 parent 1e23e90 commit 1f3fc98

File tree

1 file changed

+45
-24
lines changed

1 file changed

+45
-24
lines changed

packages/core/test/lib/base.test.ts

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -888,31 +888,33 @@ describe('BaseClient', () => {
888888
});
889889

890890
test('calls `beforeSend` and uses original event without any changes', () => {
891-
expect.assertions(1);
891+
expect.assertions(2);
892892

893893
const beforeSend = jest.fn(event => event);
894894
const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN, beforeSend });
895895
const client = new TestClient(options);
896896

897897
client.captureEvent({ message: 'hello' });
898898

899+
expect(beforeSend).toHaveBeenCalled();
899900
expect(TestClient.instance!.event!.message).toEqual('hello');
900901
});
901902

902903
test('calls `beforeSendTransaction` and uses original event without any changes', () => {
903-
expect.assertions(1);
904+
expect.assertions(2);
904905

905906
const beforeSendTransaction = jest.fn(event => event);
906907
const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN, beforeSendTransaction });
907908
const client = new TestClient(options);
908909

909910
client.captureEvent({ transaction: '/dogs/are/great', type: 'transaction' });
910911

912+
expect(beforeSendTransaction).toHaveBeenCalled();
911913
expect(TestClient.instance!.event!.transaction).toBe('/dogs/are/great');
912914
});
913915

914916
test('calls `beforeSend` and uses the modified event', () => {
915-
expect.assertions(1);
917+
expect.assertions(2);
916918

917919
const beforeSend = jest.fn(event => {
918920
event.message = 'changed1';
@@ -923,11 +925,12 @@ describe('BaseClient', () => {
923925

924926
client.captureEvent({ message: 'hello' });
925927

928+
expect(beforeSend).toHaveBeenCalled();
926929
expect(TestClient.instance!.event!.message).toEqual('changed1');
927930
});
928931

929932
test('calls `beforeSendTransaction` and uses the modified event', () => {
930-
expect.assertions(1);
933+
expect.assertions(2);
931934

932935
const beforeSendTransaction = jest.fn(event => {
933936
event.transaction = '/adopt/dont/shop';
@@ -938,11 +941,12 @@ describe('BaseClient', () => {
938941

939942
client.captureEvent({ transaction: '/dogs/are/great', type: 'transaction' });
940943

944+
expect(beforeSendTransaction).toHaveBeenCalled();
941945
expect(TestClient.instance!.event!.transaction).toBe('/adopt/dont/shop');
942946
});
943947

944948
test('calls `beforeSend` and discards the event', () => {
945-
expect.assertions(3);
949+
expect.assertions(4);
946950

947951
const beforeSend = jest.fn(() => null);
948952
const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN, beforeSend });
@@ -952,6 +956,7 @@ describe('BaseClient', () => {
952956

953957
client.captureEvent({ message: 'hello' });
954958

959+
expect(beforeSend).toHaveBeenCalled();
955960
expect(TestClient.instance!.event).toBeUndefined();
956961
// This proves that the reason the event didn't send/didn't get set on the test client is not because there was an
957962
// error, but because `beforeSend` returned `null`
@@ -960,7 +965,7 @@ describe('BaseClient', () => {
960965
});
961966

962967
test('calls `beforeSendTransaction` and discards the event', () => {
963-
expect.assertions(3);
968+
expect.assertions(4);
964969

965970
const beforeSendTransaction = jest.fn(() => null);
966971
const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN, beforeSendTransaction });
@@ -970,6 +975,7 @@ describe('BaseClient', () => {
970975

971976
client.captureEvent({ transaction: '/dogs/are/great', type: 'transaction' });
972977

978+
expect(beforeSendTransaction).toHaveBeenCalled();
973979
expect(TestClient.instance!.event).toBeUndefined();
974980
// This proves that the reason the event didn't send/didn't get set on the test client is not because there was an
975981
// error, but because `beforeSendTransaction` returned `null`
@@ -979,7 +985,7 @@ describe('BaseClient', () => {
979985

980986
test('calls `beforeSend` and logs info about invalid return value', () => {
981987
const invalidValues = [undefined, false, true, [], 1];
982-
expect.assertions(invalidValues.length * 2);
988+
expect.assertions(invalidValues.length * 3);
983989

984990
for (const val of invalidValues) {
985991
const beforeSend = jest.fn(() => val);
@@ -990,14 +996,15 @@ describe('BaseClient', () => {
990996

991997
client.captureEvent({ message: 'hello' });
992998

999+
expect(beforeSend).toHaveBeenCalled();
9931000
expect(TestClient.instance!.event).toBeUndefined();
9941001
expect(loggerWarnSpy).toBeCalledWith(new SentryError('`beforeSend` must return `null` or a valid event.'));
9951002
}
9961003
});
9971004

9981005
test('calls `beforeSendTransaction` and logs info about invalid return value', () => {
9991006
const invalidValues = [undefined, false, true, [], 1];
1000-
expect.assertions(invalidValues.length * 2);
1007+
expect.assertions(invalidValues.length * 3);
10011008

10021009
for (const val of invalidValues) {
10031010
const beforeSendTransaction = jest.fn(() => val);
@@ -1008,6 +1015,7 @@ describe('BaseClient', () => {
10081015

10091016
client.captureEvent({ transaction: '/dogs/are/great', type: 'transaction' });
10101017

1018+
expect(beforeSendTransaction).toHaveBeenCalled();
10111019
expect(TestClient.instance!.event).toBeUndefined();
10121020
expect(loggerWarnSpy).toBeCalledWith(
10131021
new SentryError('`beforeSendTransaction` must return `null` or a valid event.'),
@@ -1017,7 +1025,7 @@ describe('BaseClient', () => {
10171025

10181026
test('calls async `beforeSend` and uses original event without any changes', done => {
10191027
jest.useFakeTimers();
1020-
expect.assertions(1);
1028+
expect.assertions(2);
10211029

10221030
const beforeSend = jest.fn(
10231031
async event =>
@@ -1034,6 +1042,7 @@ describe('BaseClient', () => {
10341042
jest.runOnlyPendingTimers();
10351043

10361044
TestClient.sendEventCalled = (event: Event) => {
1045+
expect(beforeSend).toHaveBeenCalled();
10371046
expect(event.message).toEqual('hello');
10381047
};
10391048

@@ -1046,7 +1055,7 @@ describe('BaseClient', () => {
10461055

10471056
test('calls async `beforeSendTransaction` and uses original event without any changes', done => {
10481057
jest.useFakeTimers();
1049-
expect.assertions(1);
1058+
expect.assertions(2);
10501059

10511060
const beforeSendTransaction = jest.fn(
10521061
async event =>
@@ -1063,6 +1072,7 @@ describe('BaseClient', () => {
10631072
jest.runOnlyPendingTimers();
10641073

10651074
TestClient.sendEventCalled = (event: Event) => {
1075+
expect(beforeSendTransaction).toHaveBeenCalled();
10661076
expect(event.transaction).toBe('/dogs/are/great');
10671077
};
10681078

@@ -1075,7 +1085,7 @@ describe('BaseClient', () => {
10751085

10761086
test('calls async `beforeSend` and uses the modified event', done => {
10771087
jest.useFakeTimers();
1078-
expect.assertions(1);
1088+
expect.assertions(2);
10791089

10801090
const beforeSend = jest.fn(async event => {
10811091
event.message = 'changed2';
@@ -1092,6 +1102,7 @@ describe('BaseClient', () => {
10921102
jest.runOnlyPendingTimers();
10931103

10941104
TestClient.sendEventCalled = (event: Event) => {
1105+
expect(beforeSend).toHaveBeenCalled();
10951106
expect(event.message).toEqual('changed2');
10961107
};
10971108

@@ -1104,7 +1115,7 @@ describe('BaseClient', () => {
11041115

11051116
test('calls async `beforeSendTransaction` and uses the modified event', done => {
11061117
jest.useFakeTimers();
1107-
expect.assertions(1);
1118+
expect.assertions(2);
11081119

11091120
const beforeSendTransaction = jest.fn(async event => {
11101121
event.transaction = '/adopt/dont/shop';
@@ -1121,6 +1132,7 @@ describe('BaseClient', () => {
11211132
jest.runOnlyPendingTimers();
11221133

11231134
TestClient.sendEventCalled = (event: Event) => {
1135+
expect(beforeSendTransaction).toHaveBeenCalled();
11241136
expect(event.transaction).toBe('/adopt/dont/shop');
11251137
};
11261138

@@ -1133,7 +1145,7 @@ describe('BaseClient', () => {
11331145

11341146
test('calls async `beforeSend` and discards the event', () => {
11351147
jest.useFakeTimers();
1136-
expect.assertions(1);
1148+
expect.assertions(2);
11371149

11381150
const beforeSend = jest.fn(
11391151
async () =>
@@ -1149,12 +1161,13 @@ describe('BaseClient', () => {
11491161
client.captureEvent({ message: 'hello' });
11501162
jest.runAllTimers();
11511163

1164+
expect(beforeSend).toHaveBeenCalled();
11521165
expect(TestClient.instance!.event).toBeUndefined();
11531166
});
11541167

11551168
test('calls async `beforeSendTransaction` and discards the event', () => {
11561169
jest.useFakeTimers();
1157-
expect.assertions(1);
1170+
expect.assertions(2);
11581171

11591172
const beforeSendTransaction = jest.fn(
11601173
async () =>
@@ -1170,24 +1183,26 @@ describe('BaseClient', () => {
11701183
client.captureEvent({ transaction: '/dogs/are/great', type: 'transaction' });
11711184
jest.runAllTimers();
11721185

1186+
expect(beforeSendTransaction).toHaveBeenCalled();
11731187
expect(TestClient.instance!.event).toBeUndefined();
11741188
});
11751189

11761190
test('`beforeSend` gets access to a hint as a second argument', () => {
1177-
expect.assertions(2);
1191+
expect.assertions(3);
11781192

11791193
const beforeSend = jest.fn((event, hint) => ({ ...event, data: hint.data }));
11801194
const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN, beforeSend });
11811195
const client = new TestClient(options);
11821196

11831197
client.captureEvent({ message: 'hello' }, { data: 'someRandomThing' });
11841198

1199+
expect(beforeSend).toHaveBeenCalledWith(expect.any(Object), expect.objectContaining({ data: 'someRandomThing' }));
11851200
expect(TestClient.instance!.event!.message).toEqual('hello');
11861201
expect((TestClient.instance!.event! as any).data).toEqual('someRandomThing');
11871202
});
11881203

11891204
test('`beforeSendTransaction` gets access to a hint as a second argument', () => {
1190-
expect.assertions(2);
1205+
expect.assertions(3);
11911206

11921207
const beforeSendTransaction = jest.fn((event, hint) => ({ ...event, data: hint.data }));
11931208
const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN, beforeSendTransaction });
@@ -1198,45 +1213,50 @@ describe('BaseClient', () => {
11981213
{ data: { dogs: 'yes', cats: 'maybe' } },
11991214
);
12001215

1216+
expect(beforeSendTransaction).toHaveBeenCalledWith(
1217+
expect.any(Object),
1218+
expect.objectContaining({ data: { dogs: 'yes', cats: 'maybe' } }),
1219+
);
12011220
expect(TestClient.instance!.event!.transaction).toBe('/dogs/are/great');
12021221
expect((TestClient.instance!.event! as any).data).toEqual({ dogs: 'yes', cats: 'maybe' });
12031222
});
12041223

12051224
test('`beforeSend` records dropped events', () => {
1206-
expect.assertions(1);
1225+
expect.assertions(2);
12071226

1227+
const beforeSend = jest.fn(() => null);
12081228
const client = new TestClient(
12091229
getDefaultTestClientOptions({
12101230
dsn: PUBLIC_DSN,
1211-
beforeSend() {
1212-
return null;
1213-
},
1231+
beforeSend,
12141232
}),
12151233
);
12161234

12171235
const recordLostEventSpy = jest.spyOn(client, 'recordDroppedEvent');
12181236

12191237
client.captureEvent({ message: 'hello' }, {});
12201238

1239+
expect(beforeSend).toHaveBeenCalled();
12211240
expect(recordLostEventSpy).toHaveBeenCalledWith('before_send', 'error');
12221241
});
12231242

12241243
test('`beforeSendTransaction` records dropped events', () => {
1225-
expect.assertions(1);
1244+
expect.assertions(2);
1245+
1246+
const beforeSendTransaction = jest.fn(() => null);
12261247

12271248
const client = new TestClient(
12281249
getDefaultTestClientOptions({
12291250
dsn: PUBLIC_DSN,
1230-
beforeSendTransaction() {
1231-
return null;
1232-
},
1251+
beforeSendTransaction,
12331252
}),
12341253
);
12351254

12361255
const recordLostEventSpy = jest.spyOn(client, 'recordDroppedEvent');
12371256

12381257
client.captureEvent({ transaction: '/dogs/are/great', type: 'transaction' });
12391258

1259+
expect(beforeSendTransaction).toHaveBeenCalled();
12401260
expect(recordLostEventSpy).toHaveBeenCalledWith('before_send', 'transaction');
12411261
});
12421262

@@ -1361,6 +1381,7 @@ describe('BaseClient', () => {
13611381
},
13621382
});
13631383

1384+
expect(beforeSendTransaction).toHaveBeenCalled();
13641385
expect(TestClient.instance!.event!.transaction).toBe('/adopt/dont/shop');
13651386
expect(TestClient.instance!.event!.transaction_info).toEqual({
13661387
source: 'custom',

0 commit comments

Comments
 (0)