Skip to content

Commit 339a2b9

Browse files
committed
port TestBackend to TestClient
1 parent 79e05ef commit 339a2b9

File tree

2 files changed

+69
-7
lines changed

2 files changed

+69
-7
lines changed

packages/core/test/mocks/backend.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
import { Session } from '@sentry/hub';
2-
import { Event, Options, Severity, Transport } from '@sentry/types';
2+
import { Event, Severity, Transport } from '@sentry/types';
33
import { resolvedSyncPromise } from '@sentry/utils';
44

55
import { BaseBackend } from '../../src/basebackend';
6+
import { TestOptions } from './client';
67

7-
export interface TestOptions extends Options {
8-
test?: boolean;
9-
mockInstallFailure?: boolean;
10-
enableSend?: boolean;
11-
}
8+
// TODO: Delete whole file (?)
129

1310
export class TestBackend extends BaseBackend<TestOptions> {
1411
public static instance?: TestBackend;

packages/core/test/mocks/client.ts

+66-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,79 @@
1+
import { Session } from '@sentry/hub';
2+
import { Options, Severity, Event, Transport } from '@sentry/types';
3+
import { resolvedSyncPromise } from '@sentry/utils';
14
import { BaseClient } from '../../src/baseclient';
25
import { initAndBind } from '../../src/sdk';
3-
import { TestBackend, TestOptions } from './backend';
6+
import { TestBackend } from './backend';
7+
export interface TestOptions extends Options {
8+
test?: boolean;
9+
mockInstallFailure?: boolean;
10+
enableSend?: boolean;
11+
}
412

13+
// TODO: remove TestBackend
514
export class TestClient extends BaseClient<TestBackend, TestOptions> {
615
public static instance?: TestClient;
16+
public static sendEventCalled?: (event: Event) => void;
17+
18+
public event?: Event;
19+
public session?: Session;
720

821
public constructor(options: TestOptions) {
22+
// TODO: remove TestBackend param
923
super(TestBackend, options);
1024
TestClient.instance = this;
1125
}
26+
27+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
28+
public eventFromException(exception: any): PromiseLike<Event> {
29+
return resolvedSyncPromise({
30+
exception: {
31+
values: [
32+
{
33+
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
34+
type: exception.name,
35+
value: exception.message,
36+
/* eslint-enable @typescript-eslint/no-unsafe-member-access */
37+
},
38+
],
39+
},
40+
});
41+
}
42+
43+
public eventFromMessage(message: string, level: Severity = Severity.Info): PromiseLike<Event> {
44+
return resolvedSyncPromise({ message, level });
45+
}
46+
47+
public sendEvent(event: Event): void {
48+
this.event = event;
49+
if (this._options.enableSend) {
50+
super.sendEvent(event);
51+
return;
52+
}
53+
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
54+
TestBackend.sendEventCalled && TestClient.sendEventCalled(event);
55+
}
56+
57+
public sendSession(session: Session): void {
58+
this.session = session;
59+
}
60+
61+
protected _setupTransport(): Transport {
62+
if (!this._options.dsn) {
63+
// We return the noop transport here in case there is no Dsn.
64+
return super._setupTransport();
65+
}
66+
67+
const transportOptions = this._options.transportOptions
68+
? this._options.transportOptions
69+
: { dsn: this._options.dsn };
70+
71+
if (this._options.transport) {
72+
return new this._options.transport(transportOptions);
73+
}
74+
75+
return super._setupTransport();
76+
}
1277
}
1378

1479
export function init(options: TestOptions): void {

0 commit comments

Comments
 (0)