-
-
Notifications
You must be signed in to change notification settings - Fork 281
/
DITest.ts
140 lines (115 loc) · 3.66 KB
/
DITest.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import {Env, getValue, isClass, isPromise, setValue} from "@tsed/core";
import {$log} from "@tsed/logger";
import {createContainer, InjectorService, LocalsContainer, OnInit, TokenProvider, TokenProviderOpts} from "../../common/index";
import {DIContext} from "../domain/DIContext";
import {setLoggerConfiguration} from "../utils/setLoggerConfiguration";
/**
* Tool to run test with lightweight DI sandbox.
*/
export class DITest {
static options: Partial<TsED.Configuration> = {};
protected static _injector: InjectorService | null = null;
static get injector(): InjectorService {
if (DITest._injector) {
return DITest._injector!;
}
/* istanbul ignore next */
throw new Error(
"PlatformTest.injector is not initialized. Use PlatformTest.create(): Promise before PlatformTest.invoke() or PlatformTest.injector.\n" +
"Example:\n" +
"before(async () => {\n" +
" await PlatformTest.create()\n" +
" await PlatformTest.invoke(MyService, [])\n" +
"})"
);
}
static set injector(injector: InjectorService) {
DITest._injector = injector;
}
static set(key: string, value: any) {
setValue(DITest.options, key, value);
}
static hasInjector() {
return !!DITest._injector;
}
static async create(settings: Partial<TsED.Configuration> = {}) {
settings = {
...DITest.options,
...settings
};
DITest.injector = DITest.createInjector(settings);
await DITest.createContainer();
}
static async createContainer() {
await DITest.injector.load(createContainer());
}
/**
* Create a new injector with the right default services
*/
static createInjector(settings: any = {}): InjectorService {
const injector = new InjectorService();
injector.logger = $log;
// @ts-ignore
injector.settings.set(DITest.configure(settings));
setLoggerConfiguration(injector);
return injector;
}
/**
* Resets the test injector of the test context, so it won't pollute your next test. Call this in your `tearDown` logic.
*/
static async reset() {
if (DITest.hasInjector()) {
await DITest.injector.destroy();
DITest._injector = null;
}
}
/**
* Invoke a provider and return a fresh instance
* @param target
* @param providers
*/
static async invoke<T = any>(target: TokenProvider, providers: TokenProviderOpts[] = []): Promise<T> {
const locals = new LocalsContainer();
providers.forEach((p) => {
locals.set(p.token, p.use);
});
locals.set(InjectorService, DITest.injector);
const instance: OnInit = DITest.injector.invoke(target, locals, {rebuild: true});
if (instance && instance.$onInit) {
// await instance.$onInit();
const result = instance.$onInit();
if (result instanceof Promise) {
return result.then(() => instance as any);
}
}
if (isClass(instance)) {
await Promise.all(Object.values(instance).filter(isPromise));
}
return instance as any;
}
/**
* Return the instance from injector registry
* @param target
* @param options
*/
static get<T = any>(target: TokenProvider, options: any = {}): T {
return DITest.injector.get<T>(target, options)!;
}
static createDIContext() {
return new DIContext({
id: "id",
injector: DITest.injector,
logger: DITest.injector.logger
});
}
protected static configure(settings: Partial<TsED.Configuration> = {}): Partial<TsED.Configuration> {
return {
...settings,
env: getValue(settings, "env", Env.TEST),
logger: {
...getValue(settings, "logger", {}),
level: getValue(settings, "logger.level", "off")
}
};
}
}