Skip to content

Commit

Permalink
feat(testing): Make sure injectbootstrap can be called from strict …
Browse files Browse the repository at this point in the history
…context
  • Loading branch information
nicojs authored and Romakita committed Dec 23, 2018
1 parent a465f1f commit 204e15e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
11 changes: 11 additions & 0 deletions packages/testing/src/TestContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {InjectorService} from "@tsed/di";

export class TestContext {
static injector: InjectorService | null;
static reset() {
if (this.injector) {
this.injector.clear();
}
this.injector = null;
}
}
5 changes: 3 additions & 2 deletions packages/testing/src/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {$log} from "ts-log-debug";
import {TestContext} from "./testContext";

/**
* Load the server silently without listening port and configure it on test profile.
Expand All @@ -16,8 +17,8 @@ export function bootstrap(server: any, ...args: any[]) {

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

// TODO used by inject method
this.$$injector = instance.injector;
// used by inject method
TestContext.injector = instance.injector;

instance
.start()
Expand Down
3 changes: 2 additions & 1 deletion packages/testing/src/inject.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {Done} from "./done";
import {loadInjector} from "./loadInjector";
import {TestContext} from "./testContext";

/**
* The inject function is one of the TsExpressDecorator testing utilities.
Expand All @@ -16,7 +17,7 @@ import {loadInjector} from "./loadInjector";
*/
export function inject(targets: any[], func: Function) {
return function before(done: Function) {
const injector = this.$$injector || loadInjector();
const injector = TestContext.injector || loadInjector();

let isDoneInjected = false;
const args = targets.map(target => {
Expand Down

0 comments on commit 204e15e

Please sign in to comment.