Skip to content

Commit

Permalink
Reduce 'module not found' console logging
Browse files Browse the repository at this point in the history
Many users will only have the ability to resolve module 'firebase' or 'firebase-admin' but not necessarily both. 

Impacted users see repeated logging of 'Module not found, mocking skipped' messages which can cause concern, and also pollute the logs of larger test suites with many such lines.

This change only logs to console in the more severe case where neither module can be found.
  • Loading branch information
partridgeworks authored Nov 12, 2024
1 parent 6249eab commit 358f017
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/mocks/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,22 @@ const firebaseStub = (overrides, options = defaultOptions) => {
};

const mockFirebase = (overrides = {}, options = defaultOptions) => {
mockModuleIfFound('firebase', overrides, options);
mockModuleIfFound('firebase-admin', overrides, options);
const moduleFound =
mockModuleIfFound('firebase', overrides, options) |
mockModuleIfFound('firebase-admin', overrides, options);

if (!moduleFound) {
console.info(`Neither 'firebase' nor 'firebase-admin' modules found, mocking skipped.`);
}
};

function mockModuleIfFound(moduleName, overrides, options) {
try {
require.resolve(moduleName);
jest.doMock(moduleName, () => firebaseStub(overrides, options));
return true;
} catch (e) {
// eslint-disable-next-line no-console
console.info(`Module ${moduleName} not found, mocking skipped.`);
return false;
}
}

Expand Down

0 comments on commit 358f017

Please sign in to comment.