Skip to content

Commit

Permalink
refactor: always warn error on register controller
Browse files Browse the repository at this point in the history
  • Loading branch information
riflowth committed Aug 11, 2022
1 parent 440376a commit 9fe1311
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/controllers/ControllerRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ControllerRegistry {
const controllerPath = controller.getPath();
const routes = controller.getRouter();

if (process.env.NODE_ENV !== 'production' && this.#controllers.includes(controllerPath)) {
if (this.#controllers.includes(controllerPath)) {
const controllerName = controller.constructor.name;
console.warn(`Register duplicated controller: ${controllerPath} in ${controllerName}`);
}
Expand Down
10 changes: 1 addition & 9 deletions test/controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,16 @@ import { MockConditionalMiddleware, MockMiddleware, PingPongMiddleware } from '.

describe('Test the ControllerRegistry class implementation', () => {

const originalEnv = process.env;
let expressApp: Express;
let controllerRegistry: ControllerRegistry;

beforeEach(() => {
process.env = { ...originalEnv };

expressApp = express();
controllerRegistry = new ControllerRegistry(expressApp);
});

afterEach(() => {
jest.clearAllMocks();
process.env = originalEnv;
});

it('should registered the mock controllers correctly', () => {
Expand All @@ -30,16 +26,12 @@ describe('Test the ControllerRegistry class implementation', () => {
expect(findRouter(expressApp).length).toBe(2);
});

it('should warn an error correctly when register a duplicated controller', () => {
it('should warn an error when register a duplicated controller', () => {
jest.spyOn(console, 'warn').mockImplementation(jest.fn());

controllerRegistry.register(new MockIndexController());
controllerRegistry.register(new MockIndexController());

// It should not warn when NODE_ENV is production
process.env.NODE_ENV = 'production'
controllerRegistry.register(new MockIndexController());

expect(console.warn).toBeCalledTimes(1);
});

Expand Down

0 comments on commit 9fe1311

Please sign in to comment.