@tsed/platform-test-sdk
Version:
Package to test platform adapter integration with Ts.ED
65 lines (64 loc) • 2.15 kB
JavaScript
import { __decorate, __metadata, __param } from "tslib";
import { Controller } from "@tsed/di";
import { PlatformContext, Req } from "@tsed/platform-http";
import { PlatformTest } from "@tsed/platform-http/testing";
import { Middleware, Use } from "@tsed/platform-middlewares";
import { Context, Locals } from "@tsed/platform-params";
import { Get } from "@tsed/schema";
import SuperTest from "supertest";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
let LocalsMiddleware = class LocalsMiddleware {
use(request, locals, ctx) {
locals.id = "local-10909";
ctx.set("uid", "ctx-10909");
}
};
__decorate([
__param(0, Req()),
__param(1, Locals()),
__param(2, Context()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object, Object, PlatformContext]),
__metadata("design:returntype", void 0)
], LocalsMiddleware.prototype, "use", null);
LocalsMiddleware = __decorate([
Middleware()
], LocalsMiddleware);
let LocalsCtrl = class LocalsCtrl {
testStackMiddlewares(id, uid) {
return {
id: `${id}-${uid}`
};
}
};
__decorate([
Get("/scenario-1"),
Use(LocalsMiddleware),
__param(0, Locals("id")),
__param(1, Context("uid")),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object, String]),
__metadata("design:returntype", Object)
], LocalsCtrl.prototype, "testStackMiddlewares", null);
LocalsCtrl = __decorate([
Controller("/locals")
], LocalsCtrl);
export function testLocals(options) {
let request;
beforeAll(PlatformTest.bootstrap(options.server, {
...options,
mount: {
"/rest": [LocalsCtrl]
}
}));
beforeAll(() => {
request = SuperTest(PlatformTest.callback());
});
afterAll(PlatformTest.reset);
describe("Scenario 1: GET /rest/locals/scenario-1", () => {
it("should call middleware and set a id in locals", async () => {
const { body } = await request.get("/rest/locals/scenario-1").expect(200);
expect(body.id).toEqual("local-10909-ctx-10909");
});
});
}