UNPKG

@tsed/platform-test-sdk

Version:
76 lines (75 loc) 2.54 kB
import { __decorate, __metadata } from "tslib"; import { Controller } from "@tsed/di"; import { PlatformTest } from "@tsed/platform-http/testing"; import { Get, Put } from "@tsed/schema"; import SuperTest from "supertest"; import { afterAll, afterEach, beforeAll, expect, it, vi } from "vitest"; const stub = vi.fn(); let TestRoutingController = class TestRoutingController { scenario1() { stub("scenario1"); return "test1"; } scenario2() { stub("scenario2"); return "shouldNotRun"; } scenario3() { stub("scenario3"); return "test3"; } }; __decorate([ Get("/1"), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], TestRoutingController.prototype, "scenario1", null); __decorate([ Put("") // empty string has an effect on the calls orders , __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], TestRoutingController.prototype, "scenario2", null); __decorate([ Get("/3"), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], TestRoutingController.prototype, "scenario3", null); TestRoutingController = __decorate([ Controller("/routing") ], TestRoutingController); export { TestRoutingController }; export function testRouting(options) { let request; beforeAll(PlatformTest.bootstrap(options.server, { ...options, mount: { "/rest": [TestRoutingController] } })); beforeAll(() => { request = SuperTest(PlatformTest.callback()); }); afterAll(PlatformTest.reset); afterEach(() => { vi.resetAllMocks(); }); it("Scenario1: should call scenario1 only", async () => { const { text } = await request.get("/rest/routing/1").expect(200); expect(text).toEqual("test1"); expect(stub).toHaveBeenCalledWith("scenario1"); }); it("Scenario2: should call scenario2 only", async () => { const { text } = await request.put("/rest/routing/").expect(200); expect(text).toEqual("shouldNotRun"); expect(stub).toHaveBeenCalledWith("scenario2"); }); it("Scenario3: should call scenario3 only", async () => { const { text } = await request.get("/rest/routing/3").expect(200); expect(text).toEqual("test3"); expect(stub).toHaveBeenCalledWith("scenario3"); }); }