Skip to content

Commit

Permalink
feat(platform-test): Add callback() static method.
Browse files Browse the repository at this point in the history
This method facilitate the integration test declaration
  • Loading branch information
Travis committed May 5, 2020
1 parent 82bc4e7 commit 89928dd
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 4 deletions.
8 changes: 4 additions & 4 deletions .nycrc
Expand Up @@ -21,8 +21,8 @@
"lcov"
],
"check-coverage": true,
"lines": 99.84,
"statements": 99.85,
"functions": 99.68,
"branches": 88.72
"lines": 99.98,
"statements": 99.98,
"functions": 99.85,
"branches": 88.71
}
77 changes: 77 additions & 0 deletions packages/common/src/platform-test/components/PlatformTest.spec.ts
@@ -0,0 +1,77 @@
import {
Configuration,
Controller,
createExpressApplication,
createHttpServer,
createHttpsServer,
Get,
GlobalAcceptMimesMiddleware,
GlobalErrorHandlerMiddleware,
IRoute,
LogIncomingRequestMiddleware,
PlatformBuilder,
PlatformTest
} from "@tsed/common";
import {Type} from "@tsed/core";
import {expect} from "chai";
import * as SuperTest from "supertest";

@Configuration({})
class Server {}

@Controller("/")
class MyController {
@Get("/")
get() {
return "hello";
}
}

describe("PlatformTest", () => {
let request: SuperTest.SuperTest<SuperTest.Test>;
beforeEach(() => {
PlatformTest.platformBuilder = class PlatformExpress extends PlatformBuilder {
static async bootstrap(module: Type<any>, settings: Partial<TsED.Configuration> = {}): Promise<PlatformExpress> {
return this.build<PlatformExpress>(PlatformExpress).bootstrap(module, settings);
}

async loadStatics() {}

protected async loadRoutes(routes: IRoute[]): Promise<void> {
this.app.use(LogIncomingRequestMiddleware);
this.app.use(GlobalAcceptMimesMiddleware);

await super.loadRoutes(routes);

this.app.use(GlobalErrorHandlerMiddleware);
}

protected createInjector(module: Type<any>, settings: any) {
super.createInjector(module, settings);
createExpressApplication(this.injector);
createHttpsServer(this.injector);
createHttpServer(this.injector);
}
};
});
beforeEach(
PlatformTest.bootstrap(Server, {
mount: {
"/rest": [MyController]
}
})
);
beforeEach(() => {
request = SuperTest(PlatformTest.callback());
});

afterEach(PlatformTest.reset);
afterEach(() => {
delete PlatformTest.platformBuilder;
});

it("should create server and mount the controller", async () => {
const result = await request.get("/rest");
expect(result.text).to.equal("hello");
});
});
21 changes: 21 additions & 0 deletions packages/common/src/platform-test/components/PlatformTest.ts
@@ -1,6 +1,7 @@
import {Env, Type} from "@tsed/core";
import {InjectorService, LocalsContainer, OnInit, TokenProvider} from "@tsed/di";
import {createInjector, loadInjector, PlatformBuilder} from "../../platform-builder";
import {PlatformApplication} from "../../platform/services/PlatformApplication";

export interface ITestInvokeOptions {
token?: TokenProvider;
Expand Down Expand Up @@ -126,4 +127,24 @@ export class PlatformTest {

return instance as any;
}

/**
* Return the raw application (express or koa).
* Use this callback with SuperTest.
*
* ```typescript
* let request: SuperTest.SuperTest<SuperTest.Test>;
* beforeEach(PlatformTest.bootstrap(Server, {
* mount: {
* "/rest": [ProductsController]
* }
* }));
* beforeEach(() => {
* request = SuperTest(PlatformTest.callback());
* });
* ```
*/
static callback() {
return PlatformTest.injector.get<PlatformApplication>(PlatformApplication)?.callback();
}
}

0 comments on commit 89928dd

Please sign in to comment.