|
| 1 | +import { Session } from '@sentry/hub'; |
| 2 | +import { Options, Severity, Event, Transport } from '@sentry/types'; |
| 3 | +import { resolvedSyncPromise } from '@sentry/utils'; |
1 | 4 | import { BaseClient } from '../../src/baseclient';
|
2 | 5 | 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 | +} |
4 | 12 |
|
| 13 | +// TODO: remove TestBackend |
5 | 14 | export class TestClient extends BaseClient<TestBackend, TestOptions> {
|
6 | 15 | public static instance?: TestClient;
|
| 16 | + public static sendEventCalled?: (event: Event) => void; |
| 17 | + |
| 18 | + public event?: Event; |
| 19 | + public session?: Session; |
7 | 20 |
|
8 | 21 | public constructor(options: TestOptions) {
|
| 22 | + // TODO: remove TestBackend param |
9 | 23 | super(TestBackend, options);
|
10 | 24 | TestClient.instance = this;
|
11 | 25 | }
|
| 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 | + } |
12 | 77 | }
|
13 | 78 |
|
14 | 79 | export function init(options: TestOptions): void {
|
|
0 commit comments