|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +import { expect, use } from 'chai'; |
| 19 | +import sinonChai from 'sinon-chai'; |
| 20 | +import chaiAsPromised from 'chai-as-promised'; |
| 21 | +import { restore, stub } from 'sinon'; |
| 22 | +import { onRequestError } from './index'; |
| 23 | +import * as app from '@firebase/app'; |
| 24 | +import * as telemetry from '../api'; |
| 25 | +import { FirebaseApp } from '@firebase/app'; |
| 26 | +import { Telemetry } from '../public-types'; |
| 27 | + |
| 28 | +use(sinonChai); |
| 29 | +use(chaiAsPromised); |
| 30 | + |
| 31 | +describe('onRequestError', () => { |
| 32 | + let getTelemetryStub: sinon.SinonStub; |
| 33 | + let captureErrorStub: sinon.SinonStub; |
| 34 | + let fakeApp: FirebaseApp; |
| 35 | + let fakeTelemetry: Telemetry; |
| 36 | + |
| 37 | + beforeEach(() => { |
| 38 | + fakeApp = {} as FirebaseApp; |
| 39 | + fakeTelemetry = {} as Telemetry; |
| 40 | + |
| 41 | + stub(app, 'getApp').returns(fakeApp); |
| 42 | + getTelemetryStub = stub(telemetry, 'getTelemetry').returns(fakeTelemetry); |
| 43 | + captureErrorStub = stub(telemetry, 'captureError'); |
| 44 | + }); |
| 45 | + |
| 46 | + afterEach(() => { |
| 47 | + restore(); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should capture errors with correct attributes', async () => { |
| 51 | + const error = new Error('test error'); |
| 52 | + const errorRequest = { |
| 53 | + path: '/test-path?some=param', |
| 54 | + method: 'GET', |
| 55 | + headers: {}, |
| 56 | + }; |
| 57 | + const errorContext: { |
| 58 | + routerKind: 'Pages Router'; |
| 59 | + routePath: string; |
| 60 | + routeType: 'render'; |
| 61 | + revalidateReason: undefined; |
| 62 | + } = { |
| 63 | + routerKind: 'Pages Router', |
| 64 | + routePath: '/test-path', |
| 65 | + routeType: 'render', |
| 66 | + revalidateReason: undefined, |
| 67 | + }; |
| 68 | + |
| 69 | + await onRequestError(error, errorRequest, errorContext); |
| 70 | + |
| 71 | + expect(getTelemetryStub).to.have.been.calledOnceWith(fakeApp); |
| 72 | + expect(captureErrorStub).to.have.been.calledOnceWith( |
| 73 | + fakeTelemetry, |
| 74 | + error, |
| 75 | + { |
| 76 | + 'nextjs_path': '/test-path?some=param', |
| 77 | + 'nextjs_method': 'GET', |
| 78 | + 'nextjs_router_kind': 'Pages Router', |
| 79 | + 'nextjs_route_path': '/test-path', |
| 80 | + 'nextjs_route_type': 'render' |
| 81 | + } |
| 82 | + ); |
| 83 | + }); |
| 84 | +}); |
0 commit comments