Skip to content

Commit

Permalink
feat(middleware): log unhandled requests
Browse files Browse the repository at this point in the history
  • Loading branch information
smackfu committed Nov 27, 2024
1 parent 6882800 commit 210f9fa
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
3 changes: 2 additions & 1 deletion packages/parrot-core/__tests__/Parrot.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/parrot-core/src/Parrot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 14 additions & 2 deletions packages/parrot-middleware/__tests__/ParrotMiddleware.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/parrot-middleware/src/ParrotMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class ParrotMiddleware extends Parrot {
return;
}
if (!response) {
this.logger.warn('No matching mock found for request', req.path);
next();
return;
}
Expand Down

0 comments on commit 210f9fa

Please sign in to comment.