UNPKG

@tsed/platform-test-sdk

Version:
104 lines (103 loc) 3.32 kB
import { __decorate, __metadata, __param } from "tslib"; import { Controller } from "@tsed/di"; import { PlatformTest } from "@tsed/platform-http/testing"; import { Context, PathParams } from "@tsed/platform-params"; import { Get, Property, Required, Title } from "@tsed/schema"; import SuperTest from "supertest"; import { afterAll, beforeAll, expect, it } from "vitest"; export class MyModel { } __decorate([ Title("ID"), Property(), __metadata("design:type", String) ], MyModel.prototype, "id", void 0); __decorate([ Property(), Required(), __metadata("design:type", String) ], MyModel.prototype, "name", void 0); let HandlersCtrl = class HandlersCtrl { scenario1(id) { const model = new MyModel(); model.id = id; model.name = "test"; return model; } scenario2(ctx, id) { const model = new MyModel(); model.id = "1"; model.name = "test"; try { ctx.response.status(202).body(model); } catch (er) { console.log(er); } } scenario3(id) { const model = new MyModel(); model.id = id; model.name = "test"; return Promise.resolve(model); } }; __decorate([ Get("/scenario-1/:id") // Express style , __param(0, PathParams("id")), __metadata("design:type", Function), __metadata("design:paramtypes", [String]), __metadata("design:returntype", MyModel) ], HandlersCtrl.prototype, "scenario1", null); __decorate([ Get("/scenario-2/:id") // Ts.ED style with response injection , __param(0, Context()), __param(1, PathParams("id")), __param(1, Required()), __metadata("design:type", Function), __metadata("design:paramtypes", [Object, String]), __metadata("design:returntype", void 0) ], HandlersCtrl.prototype, "scenario2", null); __decorate([ Get("/scenario-3/:id") // Ts.ED style , __param(0, PathParams("id")), __metadata("design:type", Function), __metadata("design:paramtypes", [String]), __metadata("design:returntype", Promise) ], HandlersCtrl.prototype, "scenario3", null); HandlersCtrl = __decorate([ Controller("/handlers") ], HandlersCtrl); export { HandlersCtrl }; export function testHandlers(options) { let request; beforeAll(PlatformTest.bootstrap(options.server, { ...options, logger: {}, mount: { "/rest": [HandlersCtrl] } })); beforeAll(() => { request = SuperTest(PlatformTest.callback()); }); afterAll(PlatformTest.reset); it("Scenario 1: GET /rest/handlers/scenario-1/:id", async () => { const { body } = await request.get("/rest/handlers/scenario-1/1").expect(200); expect(body.id).toEqual("1"); expect(body.name).toEqual("test"); }); it("Scenario 2: GET /rest/handlers/scenario-2/:id", async () => { const { body } = await request.get("/rest/handlers/scenario-2/1").expect(202); expect(body.id).toEqual("1"); expect(body.name).toEqual("test"); }); it("Scenario 3: GET /rest/handlers/scenario-3/:id", async () => { const { body } = await request.get("/rest/handlers/scenario-3/1").expect(200); expect(body.id).toEqual("1"); expect(body.name).toEqual("test"); }); }