Skip to content

Commit cda64c9

Browse files
committed
move decorator tests to own describe block
1 parent f388c9f commit cda64c9

File tree

1 file changed

+150
-150
lines changed

1 file changed

+150
-150
lines changed

packages/event-handler/tests/unit/rest/BaseRouter.test.ts

Lines changed: 150 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -593,181 +593,181 @@ describe('Class: BaseRouter', () => {
593593
// Assess
594594
expect(result.headers.get('Content-Type')).toBe('application/json');
595595
});
596+
});
596597

597-
describe('decorators', () => {
598-
it('works with errorHandler decorator', async () => {
599-
// Prepare
600-
const app = new TestResolver();
601-
602-
class Lambda {
603-
@app.errorHandler(BadRequestError)
604-
public async handleBadRequest(error: BadRequestError) {
605-
return {
606-
statusCode: HttpErrorCodes.BAD_REQUEST,
607-
error: 'Bad Request',
608-
message: `Decorated: ${error.message}`,
609-
};
610-
}
611-
612-
@app.get('/test')
613-
public async getTest() {
614-
throw new BadRequestError('test error');
615-
}
616-
617-
public async handler(event: unknown, context: Context) {
618-
return app.resolve(event, context);
619-
}
620-
}
621-
622-
const lambda = new Lambda();
623-
624-
// Act
625-
const result = (await lambda.handler(
626-
{ path: '/test', method: 'GET' },
627-
context
628-
)) as Response;
598+
describe('decorators error handling', () => {
599+
it('works with errorHandler decorator', async () => {
600+
// Prepare
601+
const app = new TestResolver();
629602

630-
// Assess
631-
expect(result).toBeInstanceOf(Response);
632-
expect(result.status).toBe(HttpErrorCodes.BAD_REQUEST);
633-
expect(await result.text()).toBe(
634-
JSON.stringify({
603+
class Lambda {
604+
@app.errorHandler(BadRequestError)
605+
public async handleBadRequest(error: BadRequestError) {
606+
return {
635607
statusCode: HttpErrorCodes.BAD_REQUEST,
636608
error: 'Bad Request',
637-
message: 'Decorated: test error',
638-
})
639-
);
640-
});
609+
message: `Decorated: ${error.message}`,
610+
};
611+
}
612+
613+
@app.get('/test')
614+
public async getTest() {
615+
throw new BadRequestError('test error');
616+
}
641617

642-
it('works with notFound decorator', async () => {
643-
// Prepare
644-
const app = new TestResolver();
645-
646-
class Lambda {
647-
@app.notFound()
648-
public async handleNotFound(error: NotFoundError) {
649-
return {
650-
statusCode: HttpErrorCodes.NOT_FOUND,
651-
error: 'Not Found',
652-
message: `Decorated: ${error.message}`,
653-
};
654-
}
655-
656-
public async handler(event: unknown, context: Context) {
657-
return app.resolve(event, context);
658-
}
618+
public async handler(event: unknown, context: Context) {
619+
return app.resolve(event, context);
659620
}
621+
}
660622

661-
const lambda = new Lambda();
623+
const lambda = new Lambda();
662624

663-
// Act
664-
const result = (await lambda.handler(
665-
{ path: '/nonexistent', method: 'GET' },
666-
context
667-
)) as Response;
625+
// Act
626+
const result = (await lambda.handler(
627+
{ path: '/test', method: 'GET' },
628+
context
629+
)) as Response;
668630

669-
// Assess
670-
expect(result).toBeInstanceOf(Response);
671-
expect(result.status).toBe(HttpErrorCodes.NOT_FOUND);
672-
expect(await result.text()).toBe(
673-
JSON.stringify({
631+
// Assess
632+
expect(result).toBeInstanceOf(Response);
633+
expect(result.status).toBe(HttpErrorCodes.BAD_REQUEST);
634+
expect(await result.text()).toBe(
635+
JSON.stringify({
636+
statusCode: HttpErrorCodes.BAD_REQUEST,
637+
error: 'Bad Request',
638+
message: 'Decorated: test error',
639+
})
640+
);
641+
});
642+
643+
it('works with notFound decorator', async () => {
644+
// Prepare
645+
const app = new TestResolver();
646+
647+
class Lambda {
648+
@app.notFound()
649+
public async handleNotFound(error: NotFoundError) {
650+
return {
674651
statusCode: HttpErrorCodes.NOT_FOUND,
675652
error: 'Not Found',
676-
message: 'Decorated: Route GET /nonexistent not found',
677-
})
678-
);
679-
});
653+
message: `Decorated: ${error.message}`,
654+
};
655+
}
680656

681-
it('works with methodNotAllowed decorator', async () => {
682-
// Prepare
683-
const app = new TestResolver();
684-
685-
class Lambda {
686-
@app.methodNotAllowed()
687-
public async handleMethodNotAllowed(error: MethodNotAllowedError) {
688-
return {
689-
statusCode: HttpErrorCodes.METHOD_NOT_ALLOWED,
690-
error: 'Method Not Allowed',
691-
message: `Decorated: ${error.message}`,
692-
};
693-
}
694-
695-
@app.get('/test')
696-
public async getTest() {
697-
throw new MethodNotAllowedError('POST not allowed');
698-
}
699-
700-
public async handler(event: unknown, context: Context) {
701-
return app.resolve(event, context);
702-
}
657+
public async handler(event: unknown, context: Context) {
658+
return app.resolve(event, context);
703659
}
660+
}
704661

705-
const lambda = new Lambda();
662+
const lambda = new Lambda();
706663

707-
// Act
708-
const result = (await lambda.handler(
709-
{ path: '/test', method: 'GET' },
710-
context
711-
)) as Response;
664+
// Act
665+
const result = (await lambda.handler(
666+
{ path: '/nonexistent', method: 'GET' },
667+
context
668+
)) as Response;
712669

713-
// Assess
714-
expect(result).toBeInstanceOf(Response);
715-
expect(result.status).toBe(HttpErrorCodes.METHOD_NOT_ALLOWED);
716-
expect(await result.text()).toBe(
717-
JSON.stringify({
670+
// Assess
671+
expect(result).toBeInstanceOf(Response);
672+
expect(result.status).toBe(HttpErrorCodes.NOT_FOUND);
673+
expect(await result.text()).toBe(
674+
JSON.stringify({
675+
statusCode: HttpErrorCodes.NOT_FOUND,
676+
error: 'Not Found',
677+
message: 'Decorated: Route GET /nonexistent not found',
678+
})
679+
);
680+
});
681+
682+
it('works with methodNotAllowed decorator', async () => {
683+
// Prepare
684+
const app = new TestResolver();
685+
686+
class Lambda {
687+
@app.methodNotAllowed()
688+
public async handleMethodNotAllowed(error: MethodNotAllowedError) {
689+
return {
718690
statusCode: HttpErrorCodes.METHOD_NOT_ALLOWED,
719691
error: 'Method Not Allowed',
720-
message: 'Decorated: POST not allowed',
721-
})
722-
);
723-
});
692+
message: `Decorated: ${error.message}`,
693+
};
694+
}
724695

725-
it('preserves scope when using error handler decorators', async () => {
726-
// Prepare
727-
const app = new TestResolver();
728-
729-
class Lambda {
730-
public scope = 'scoped';
731-
732-
@app.errorHandler(BadRequestError)
733-
public async handleBadRequest(error: BadRequestError) {
734-
return {
735-
statusCode: HttpErrorCodes.BAD_REQUEST,
736-
error: 'Bad Request',
737-
message: `${this.scope}: ${error.message}`,
738-
};
739-
}
740-
741-
@app.get('/test')
742-
public async getTest() {
743-
throw new BadRequestError('test error');
744-
}
745-
746-
public async handler(event: unknown, context: Context) {
747-
return app.resolve(event, context, { scope: this });
748-
}
696+
@app.get('/test')
697+
public async getTest() {
698+
throw new MethodNotAllowedError('POST not allowed');
749699
}
750700

751-
const lambda = new Lambda();
752-
const handler = lambda.handler.bind(lambda);
701+
public async handler(event: unknown, context: Context) {
702+
return app.resolve(event, context);
703+
}
704+
}
705+
706+
const lambda = new Lambda();
707+
708+
// Act
709+
const result = (await lambda.handler(
710+
{ path: '/test', method: 'GET' },
711+
context
712+
)) as Response;
713+
714+
// Assess
715+
expect(result).toBeInstanceOf(Response);
716+
expect(result.status).toBe(HttpErrorCodes.METHOD_NOT_ALLOWED);
717+
expect(await result.text()).toBe(
718+
JSON.stringify({
719+
statusCode: HttpErrorCodes.METHOD_NOT_ALLOWED,
720+
error: 'Method Not Allowed',
721+
message: 'Decorated: POST not allowed',
722+
})
723+
);
724+
});
725+
726+
it('preserves scope when using error handler decorators', async () => {
727+
// Prepare
728+
const app = new TestResolver();
753729

754-
// Act
755-
const result = (await handler(
756-
{ path: '/test', method: 'GET' },
757-
context
758-
)) as Response;
730+
class Lambda {
731+
public scope = 'scoped';
759732

760-
// Assess
761-
expect(result).toBeInstanceOf(Response);
762-
expect(result.status).toBe(HttpErrorCodes.BAD_REQUEST);
763-
expect(await result.text()).toBe(
764-
JSON.stringify({
733+
@app.errorHandler(BadRequestError)
734+
public async handleBadRequest(error: BadRequestError) {
735+
return {
765736
statusCode: HttpErrorCodes.BAD_REQUEST,
766737
error: 'Bad Request',
767-
message: 'scoped: test error',
768-
})
769-
);
770-
});
738+
message: `${this.scope}: ${error.message}`,
739+
};
740+
}
741+
742+
@app.get('/test')
743+
public async getTest() {
744+
throw new BadRequestError('test error');
745+
}
746+
747+
public async handler(event: unknown, context: Context) {
748+
return app.resolve(event, context, { scope: this });
749+
}
750+
}
751+
752+
const lambda = new Lambda();
753+
const handler = lambda.handler.bind(lambda);
754+
755+
// Act
756+
const result = (await handler(
757+
{ path: '/test', method: 'GET' },
758+
context
759+
)) as Response;
760+
761+
// Assess
762+
expect(result).toBeInstanceOf(Response);
763+
expect(result.status).toBe(HttpErrorCodes.BAD_REQUEST);
764+
expect(await result.text()).toBe(
765+
JSON.stringify({
766+
statusCode: HttpErrorCodes.BAD_REQUEST,
767+
error: 'Bad Request',
768+
message: 'scoped: test error',
769+
})
770+
);
771771
});
772772
});
773773
});

0 commit comments

Comments
 (0)