Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(platform-test): Add callback() static method.
This method facilitate the integration test declaration
- Loading branch information
Travis
committed
May 5, 2020
1 parent
82bc4e7
commit 89928dd
Showing
3 changed files
with
102 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
packages/common/src/platform-test/components/PlatformTest.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters