Skip to content

Commit

Permalink
test: remove e2e test for onLog and add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
russellwheatley committed Jan 22, 2025
1 parent cf9bfa3 commit 4d049d4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
20 changes: 19 additions & 1 deletion packages/app/__tests__/app.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, it } from '@jest/globals';
import { jest, describe, expect, it } from '@jest/globals';

import {
deleteApp,
Expand All @@ -9,6 +9,7 @@ import {
getApp,
setLogLevel,
} from '../lib';
import { Logger } from '../lib/internal/logger';

describe('App', function () {
describe('modular', function () {
Expand Down Expand Up @@ -39,5 +40,22 @@ describe('App', function () {
it('`setLogLevel` function is properly exposed to end user', function () {
expect(setLogLevel).toBeDefined();
});

it('`onLog()` is called when using Logger (currently only VertexAI uses `onLog()`)', function () {
const logger = new Logger('@firebase/vertexai');
const spy2 = jest.fn();

onLog(spy2);
logger.info('test');

expect(spy2).toHaveBeenCalledWith(
expect.objectContaining({
args: ['test'],
level: 'info',
message: 'test',
type: '@firebase/vertexai',
}),
);
});
});
});
13 changes: 12 additions & 1 deletion packages/app/lib/internal/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ const LogLevel = {
SILENT: 5,
};

// mimic the LogLevel in firebase-js-sdk TS
const reverseLogLevel = obj => {
const reversed = {};
for (const [key, value] of Object.entries(obj)) {
reversed[value] = key;
}
return reversed;
};

const LogLevelReversed = reverseLogLevel(LogLevel);

/**
* By default, `console.debug` is not displayed in the developer console (in
* chrome). To avoid forcing users to have to opt-in to these logs twice
Expand Down Expand Up @@ -208,7 +219,7 @@ export function setUserLogHandler(logCallback, options) {
.join(' ');
if (level >= (customLogLevel ?? instance.logLevel)) {
logCallback({
level: LogLevel[level].toLowerCase(),
level: LogLevelReversed[level].toLowerCase(),
message,
args,
type: instance.name,
Expand Down
18 changes: 14 additions & 4 deletions packages/app/lib/modular/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,23 @@ export function registerVersion(
): Promise<void>;

/**
* Sets log handler for all Firebase SDKs.
* Sets log handler for all Firebase SDKs. Currently only supported on VertexAI.
* @param logCallback - The callback function to handle logs.
* @param options - Optional settings for log handling.
* @returns Promise<void>
* @throws Error - onLog is only supported on Web
* @returns <void>
*/
export function onLog(logCallback: (logData: any) => void, options?: any): Promise<void>;

interface LogCallbackParams {
level: LogLevelString;
message: string;
args: unknown[];
type: string;
}

export function onLog(
logCallback: (callbackParams: LogCallbackParams) => void,
options?: any,
): void;

/**
* Gets the list of all initialized apps.
Expand Down

0 comments on commit 4d049d4

Please sign in to comment.