UNPKG

@tsed/platform-test-sdk

Version:
84 lines (83 loc) 2.91 kB
import { __decorate, __metadata, __param } from "tslib"; import { EOL } from "node:os"; import { Controller } from "@tsed/di"; import { PlatformTest } from "@tsed/platform-http/testing"; import { Middleware, UseBefore } from "@tsed/platform-middlewares"; import { Locals } from "@tsed/platform-params"; import { View } from "@tsed/platform-views"; import { Get } from "@tsed/schema"; import SuperTest from "supertest"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; let LocalsMiddleware = class LocalsMiddleware { use(locals) { locals.localID = "local-10909"; } }; __decorate([ __param(0, Locals()), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", void 0) ], LocalsMiddleware.prototype, "use", null); LocalsMiddleware = __decorate([ Middleware() ], LocalsMiddleware); let ViewCtrl = class ViewCtrl { testScenario1() { return { world: "world" }; } testScenario2() { return { world: "world" }; } }; __decorate([ Get("/scenario-1"), UseBefore(LocalsMiddleware), View("view.ejs", { opts: "opts" }), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], ViewCtrl.prototype, "testScenario1", null); __decorate([ Get("/scenario-2"), View("view", { opts: "opts" }) // missing extension , __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], ViewCtrl.prototype, "testScenario2", null); ViewCtrl = __decorate([ Controller("/views") ], ViewCtrl); export function testView(options) { let request; beforeAll(PlatformTest.bootstrap(options.server, { ...options, mount: { "/rest": [ViewCtrl] } })); beforeAll(() => { request = SuperTest(PlatformTest.callback()); }); afterAll(() => PlatformTest.reset()); describe("Scenario1: GET /rest/views/scenario-1", () => { it("should render a view", async () => { const response = await request.get("/rest/views/scenario-1").expect(200); expect(response.headers["content-type"]).toContain("text/html"); expect(response.text).toEqual(`<p>Hello world with opts and ID local-10909</p>${EOL}`); }); }); describe("Scenario2: GET /rest/views/scenario-2", () => { it("should throw an error when extension is not defined", async () => { const response = await request.get("/rest/views/scenario-2").expect(500); expect(response.body.name).toEqual("TEMPLATE_RENDER_ERROR"); expect(response.body.message).toContain("Template rendering error: ViewCtrl.testScenario2()"); expect(response.body.status).toEqual(500); }); }); }