Skip to content

Commit

Permalink
feat(testing): Promisify the bootstrap function
Browse files Browse the repository at this point in the history
Make sure the bootstrap test setup function
returns a `Promise` (instead of `(done) => void`).

BREAKING CHANGE: `boostrap(MyServer)` now returns a `Promise`.

Change
```typescript
beforeEach(done => bootstrap(MyServer)(done))
```
to
```typescript
beforeEach(async () => await bootstrap(MyServer)())
```
or
```typescript
beforeEach(bootstrap(MyServer))
```
  • Loading branch information
nicojs authored and Romakita committed Dec 23, 2018
1 parent 204e15e commit f2885a9
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 14 deletions.
12 changes: 8 additions & 4 deletions packages/testing/src/TestContext.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import {InjectorService} from "@tsed/di";

export class TestContext {
static injector: InjectorService | null;
static injector: InjectorService | null = null;

/**
* Resets the test injector of the test context, so it won't pollute your next test. Call this in your `tearDown` logic.
*/
static reset() {
if (this.injector) {
this.injector.clear();
if (TestContext.injector && TestContext.injector.clear) {
TestContext.injector.clear();
}
this.injector = null;
TestContext.injector = null;
}
}
14 changes: 5 additions & 9 deletions packages/testing/src/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
import {$log} from "ts-log-debug";
import {TestContext} from "./testContext";
import {TestContext} from "./TestContext";

/**
* Load the server silently without listening port and configure it on test profile.
* @decorator
* @param server
* @param args
* @returns {(done:Function)=>undefined}
* @returns {Promise<void>}
*/
export function bootstrap(server: any, ...args: any[]) {
export function bootstrap(server: any, ...args: any[]): () => Promise<void> {
$log.stop();
process.env.NODE_ENV = process.env.NODE_ENV || "test";

return function before(done: any) {
return function before() {
const instance = new server(...args);

instance.startServers = () => Promise.resolve();

// used by inject method
TestContext.injector = instance.injector;

instance
.start()
.then(done)
.catch(done);
return instance.start();
};
}
1 change: 1 addition & 0 deletions packages/testing/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from "./done";
export * from "./bootstrap";
export * from "./invoke";
export * from "./loadInjector";
export * from "./TestContext";
2 changes: 1 addition & 1 deletion packages/testing/src/inject.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Done} from "./done";
import {loadInjector} from "./loadInjector";
import {TestContext} from "./testContext";
import {TestContext} from "./TestContext";

/**
* The inject function is one of the TsExpressDecorator testing utilities.
Expand Down

0 comments on commit f2885a9

Please sign in to comment.