From 210f9fa1edea4cd1adb25870adefb0fd1829f25a Mon Sep 17 00:00:00 2001 From: Scott McIntyre Date: Wed, 27 Nov 2024 15:22:39 -0500 Subject: [PATCH] feat(middleware): log unhandled requests --- packages/parrot-core/__tests__/Parrot.spec.js | 3 ++- packages/parrot-core/src/Parrot.js | 1 + .../__tests__/ParrotMiddleware.spec.js | 16 ++++++++++++++-- .../parrot-middleware/src/ParrotMiddleware.js | 1 + 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/packages/parrot-core/__tests__/Parrot.spec.js b/packages/parrot-core/__tests__/Parrot.spec.js index eafcaae..b79b967 100644 --- a/packages/parrot-core/__tests__/Parrot.spec.js +++ b/packages/parrot-core/__tests__/Parrot.spec.js @@ -35,9 +35,10 @@ class ParrotTest extends Parrot { describe('Parrot', () => { it('normalizes scenarios and sets logger', () => { - new ParrotTest(); // eslint-disable-line no-new + const parrotTest = new ParrotTest(); expect(normalizeScenarios).toHaveBeenCalledWith({}); expect(logger.setScenario).toHaveBeenCalledWith(undefined); + expect(parrotTest.logger).toBe(logger); }); it('should get the active scenario name', () => { diff --git a/packages/parrot-core/src/Parrot.js b/packages/parrot-core/src/Parrot.js index 2ed0d5f..47bfc5a 100644 --- a/packages/parrot-core/src/Parrot.js +++ b/packages/parrot-core/src/Parrot.js @@ -19,6 +19,7 @@ class Parrot { this.scenarios = normalizeScenarios(scenarios); [this.activeScenario] = Object.keys(scenarios); logger.setScenario(this.activeScenario); + this.logger = logger; } getActiveScenario = () => this.activeScenario; diff --git a/packages/parrot-middleware/__tests__/ParrotMiddleware.spec.js b/packages/parrot-middleware/__tests__/ParrotMiddleware.spec.js index 0f6f8a7..9cc2edb 100644 --- a/packages/parrot-middleware/__tests__/ParrotMiddleware.spec.js +++ b/packages/parrot-middleware/__tests__/ParrotMiddleware.spec.js @@ -14,7 +14,15 @@ import ParrotMiddleware from '../src/ParrotMiddleware'; -jest.mock('parrot-core', () => class {}); +jest.mock( + 'parrot-core', + () => + class { + constructor() { + this.logger = { warn: jest.fn() }; + } + } +); describe('ParrotMiddleware', () => { it('should normalize', () => { @@ -27,13 +35,17 @@ describe('ParrotMiddleware', () => { }); }); - it('should call next middleware', () => { + it('should call next middleware and log a warning', () => { const req = {}; const res = { headersSent: false }; const next = jest.fn(); const parrotMiddleware = new ParrotMiddleware(); parrotMiddleware.resolver(req, res, next)(); expect(next).toHaveBeenCalled(); + expect(parrotMiddleware.logger.warn).toHaveBeenCalledWith( + 'No matching mock found for request', + req.path + ); }); it('should not call next middleware if headers sent', () => { diff --git a/packages/parrot-middleware/src/ParrotMiddleware.js b/packages/parrot-middleware/src/ParrotMiddleware.js index 3501987..f48096e 100644 --- a/packages/parrot-middleware/src/ParrotMiddleware.js +++ b/packages/parrot-middleware/src/ParrotMiddleware.js @@ -22,6 +22,7 @@ class ParrotMiddleware extends Parrot { return; } if (!response) { + this.logger.warn('No matching mock found for request', req.path); next(); return; }