@tsed/platform-test-sdk
Version:
Package to test platform adapter integration with Ts.ED
63 lines (62 loc) • 2.38 kB
JavaScript
import { __decorate, __metadata } from "tslib";
import { createReadStream } from "node:fs";
import { join } from "node:path";
import { Controller } from "@tsed/di";
import { PlatformTest } from "@tsed/platform-http/testing";
import { ContentType, Get } from "@tsed/schema";
import SuperTest from "supertest";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
const rootDir = import.meta.dirname; // automatically replaced by import.meta.dirname on build
let TestStreamCtrl = class TestStreamCtrl {
testScenario1() {
return createReadStream(join(rootDir, "../data/response.data.json"), "utf-8");
}
testScenario2() {
return Promise.resolve(createReadStream(join(rootDir, "../data/response.data.json"), "utf-8"));
}
};
__decorate([
Get("/scenario1"),
ContentType("application/json"),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], TestStreamCtrl.prototype, "testScenario1", null);
__decorate([
Get("/scenario1b"),
ContentType("application/json"),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], TestStreamCtrl.prototype, "testScenario2", null);
TestStreamCtrl = __decorate([
Controller("/stream")
], TestStreamCtrl);
export function testStream(options) {
let request;
beforeAll(PlatformTest.bootstrap(options.server, {
...options,
mount: {
"/rest": [TestStreamCtrl]
}
}));
beforeAll(() => {
request = SuperTest(PlatformTest.callback());
});
afterAll(PlatformTest.reset);
describe("Scenario1: when endpoint return a stream", () => {
describe("GET /rest/stream/scenario1", () => {
it("should return a body", async () => {
const response = await request.get("/rest/stream/scenario1");
expect(response.headers["content-type"]).toContain("application/json");
expect(response.body).toEqual({ id: "1" });
});
});
describe("GET /rest/stream/scenario1b", () => {
it("should return a body", async () => {
const response = await request.get("/rest/stream/scenario1b");
expect(response.body).toEqual({ id: "1" });
});
});
});
}