|
| 1 | +import { Options, EventHint, Event, Session, SeverityLevel, Transport } from '@sentry/types'; |
| 2 | + |
1 | 3 | import { BaseClient } from '../../src/baseclient'; |
2 | 4 | import { initAndBind } from '../../src/sdk'; |
3 | | -import { TestBackend, TestOptions } from './backend'; |
| 5 | +import { resolvedSyncPromise } from '@sentry/utils'; |
| 6 | + |
| 7 | +interface TestOptions extends Options { |
| 8 | + test?: boolean; |
| 9 | + mockInstallFailure?: boolean; |
| 10 | + enableSend?: boolean; |
| 11 | +} |
4 | 12 |
|
5 | | -export class TestClient extends BaseClient<TestBackend, TestOptions> { |
| 13 | +export class TestClient extends BaseClient<TestOptions> { |
6 | 14 | public static instance?: TestClient; |
| 15 | + public static sendEventCalled?: (event: Event) => void; |
| 16 | + |
| 17 | + public event?: Event; |
| 18 | + public session?: Session; |
7 | 19 |
|
8 | 20 | public constructor(options: TestOptions) { |
9 | | - super(TestBackend, options); |
| 21 | + super(options); |
10 | 22 | TestClient.instance = this; |
11 | 23 | } |
| 24 | + |
| 25 | + public sendEvent(event: Event): void { |
| 26 | + this.event = event; |
| 27 | + if (this._options.enableSend) { |
| 28 | + super.sendEvent(event); |
| 29 | + return; |
| 30 | + } |
| 31 | + // eslint-disable-next-line @typescript-eslint/no-unused-expressions |
| 32 | + TestClient.sendEventCalled && TestClient.sendEventCalled(event); |
| 33 | + } |
| 34 | + |
| 35 | + public sendSession(session: Session): void { |
| 36 | + this.session = session; |
| 37 | + } |
| 38 | + |
| 39 | + protected _setupTransport(): Transport { |
| 40 | + if (!this._options.dsn) { |
| 41 | + // We return the noop transport here in case there is no Dsn. |
| 42 | + return super._setupTransport(); |
| 43 | + } |
| 44 | + |
| 45 | + const transportOptions = this._options.transportOptions |
| 46 | + ? this._options.transportOptions |
| 47 | + : { dsn: this._options.dsn }; |
| 48 | + |
| 49 | + if (this._options.transport) { |
| 50 | + return new this._options.transport(transportOptions); |
| 51 | + } |
| 52 | + |
| 53 | + return super._setupTransport(); |
| 54 | + } |
| 55 | + |
| 56 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types |
| 57 | + protected _eventFromException(exception: any, _hint?: EventHint): PromiseLike<Event> { |
| 58 | + return resolvedSyncPromise({ |
| 59 | + exception: { |
| 60 | + values: [ |
| 61 | + { |
| 62 | + /* eslint-disable @typescript-eslint/no-unsafe-member-access */ |
| 63 | + type: exception.name, |
| 64 | + value: exception.message, |
| 65 | + /* eslint-enable @typescript-eslint/no-unsafe-member-access */ |
| 66 | + }, |
| 67 | + ], |
| 68 | + }, |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + protected _eventFromMessage(message: string, level: SeverityLevel = 'info'): PromiseLike<Event> { |
| 73 | + return resolvedSyncPromise({ message, level }); |
| 74 | + } |
12 | 75 | } |
13 | 76 |
|
14 | 77 | export function init(options: TestOptions): void { |
|
0 commit comments