UNPKG

@tsed/platform-test-sdk

Version:
113 lines (112 loc) 3.79 kB
import { __decorate, __metadata, __param } from "tslib"; import { Controller, ProviderScope, Scope, Service } from "@tsed/di"; import { PlatformTest } from "@tsed/platform-http/testing"; import { PathParams } from "@tsed/platform-params"; import { Get } from "@tsed/schema"; import SuperTest from "supertest"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; let ScopeRequestService = class ScopeRequestService { }; ScopeRequestService = __decorate([ Service(), Scope(ProviderScope.REQUEST) ], ScopeRequestService); export { ScopeRequestService }; let ScopeRequestCtrl = class ScopeRequestCtrl { constructor(scopeRequestService) { this.scopeRequestService = scopeRequestService; } $onDestroy() { this.userId = null; } testPerRequest(userId) { this.scopeRequestService.user = userId; this.userId = userId; return new Promise((resolve, reject) => { if (userId === "0") { setTimeout(() => { resolve({ userId, idSrv: this.scopeRequestService.user, idCtrl: this.userId }); }, 500); } if (userId === "1") { setTimeout(() => { resolve({ userId, idSrv: this.scopeRequestService.user, idCtrl: this.userId }); }, 300); } if (userId === "2") { setTimeout(() => { resolve({ userId, idSrv: this.scopeRequestService.user, idCtrl: this.userId }); }, 150); } }); } }; __decorate([ Get("/:id"), __param(0, PathParams("id")), __metadata("design:type", Function), __metadata("design:paramtypes", [String]), __metadata("design:returntype", Promise) ], ScopeRequestCtrl.prototype, "testPerRequest", null); ScopeRequestCtrl = __decorate([ Controller("/scopes-request"), Scope(ProviderScope.REQUEST), __metadata("design:paramtypes", [ScopeRequestService]) ], ScopeRequestCtrl); export { ScopeRequestCtrl }; export function testScopeRequest(options) { let request; beforeAll(PlatformTest.bootstrap(options.server, { ...options, mount: { "/rest": [ScopeRequestCtrl] } })); beforeAll(() => { request = SuperTest(PlatformTest.callback()); }); afterAll(PlatformTest.reset); describe("GET /rest/scopes-request/:id", () => { const send = (id) => new Promise((resolve, reject) => { request .get(`/rest/scopes-request/${id}`) .expect(200) .end((err, response) => { if (err) { reject(err); } else { resolve({ id, ...JSON.parse(response.text) }); } }); }); it("should respond with the right userId per request", () => { const promises = []; promises.push(send("0")); promises.push(send("1")); promises.push(send("2")); return Promise.all(promises).then((responses) => { expect(responses).toEqual([ { id: "0", idCtrl: "0", idSrv: "0", userId: "0" }, { id: "1", idCtrl: "1", idSrv: "1", userId: "1" }, { id: "2", idCtrl: "2", idSrv: "2", userId: "2" } ]); }); }); }); }