@nestia/e2e 7.1.1-dev.20250714 → 7.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,296 +1,296 @@
1
- import fs from "fs";
2
- import NodePath from "path";
3
-
4
- /**
5
- * Dynamic Executor running prefixed functions.
6
- *
7
- * `DynamicExecutor` runs every (or some filtered) prefixed functions
8
- * in a specific directory.
9
- *
10
- * For reference, it's useful for test program development of a backend server.
11
- * Just write test functions under a directory, and just specify it.
12
- * Furthermore, if you compose e2e test programs to utilize the `@nestia/sdk`
13
- * generated API functions, you can take advantage of {@link DynamicBenchmarker}
14
- * at the same time.
15
- *
16
- * When you want to see some utilization cases, see the below example links.
17
- *
18
- * @example https://github.com/samchon/nestia-start/blob/master/test/index.ts
19
- * @example https://github.com/samchon/backend/blob/master/test/index.ts
20
- * @author Jeongho Nam - https://github.com/samchon
21
- */
22
- export namespace DynamicExecutor {
23
- /**
24
- * Function type of a prefixed.
25
- *
26
- * @template Arguments Type of parameters
27
- * @template Ret Type of return value
28
- */
29
- export interface Closure<Arguments extends any[], Ret = any> {
30
- (...args: Arguments): Promise<Ret>;
31
- }
32
-
33
- /**
34
- * Options for dynamic executor.
35
- */
36
- export interface IProps<Parameters extends any[], Ret = any> {
37
- /**
38
- * Prefix of function name.
39
- *
40
- * Every prefixed function will be executed.
41
- *
42
- * In other words, if a function name doesn't start with the prefix, then it would never be executed.
43
- */
44
- prefix: string;
45
-
46
- /**
47
- * Location of the test functions.
48
- */
49
- location: string;
50
-
51
- /**
52
- * Get parameters of a function.
53
- *
54
- * @param name Function name
55
- * @returns Parameters
56
- */
57
- parameters: (name: string) => Parameters;
58
-
59
- /**
60
- * On complete function.
61
- *
62
- * Listener of completion of a test function.
63
- *
64
- * @param exec Execution result of a test function
65
- */
66
- onComplete?: (exec: IExecution) => void;
67
-
68
- /**
69
- * Filter function whether to run or not.
70
- *
71
- * @param name Function name
72
- * @returns Whether to run or not
73
- */
74
- filter?: (name: string) => boolean;
75
-
76
- /**
77
- * Wrapper of test function.
78
- *
79
- * If you specify this `wrapper` property, every dynamic functions
80
- * loaded and called by this `DynamicExecutor` would be wrapped by
81
- * the `wrapper` function.
82
- *
83
- * @param name Function name
84
- * @param closure Function to be executed
85
- * @param parameters Parameters, result of options.parameters function.
86
- * @returns Wrapper function
87
- */
88
- wrapper?: (
89
- name: string,
90
- closure: Closure<Parameters, Ret>,
91
- parameters: Parameters,
92
- ) => Promise<any>;
93
-
94
- /**
95
- * Number of simultaneous requests.
96
- *
97
- * The number of requests to be executed simultaneously.
98
- *
99
- * If you configure a value greater than one, the dynamic executor will
100
- * process the functions concurrently with the given capacity value.
101
- *
102
- * @default 1
103
- */
104
- simultaneous?: number;
105
-
106
- /**
107
- * Extension of dynamic functions.
108
- *
109
- * @default js
110
- */
111
- extension?: string;
112
- }
113
-
114
- /**
115
- * Report, result of dynamic execution.
116
- */
117
- export interface IReport {
118
- /**
119
- * Location path of dynamic functions.
120
- */
121
- location: string;
122
-
123
- /**
124
- * Execution results of dynamic functions.
125
- */
126
- executions: IExecution[];
127
-
128
- /**
129
- * Total elapsed time.
130
- */
131
- time: number;
132
- }
133
-
134
- /**
135
- * Execution of a test function.
136
- */
137
- export interface IExecution {
138
- /**
139
- * Name of function.
140
- */
141
- name: string;
142
-
143
- /**
144
- * Location path of the function.
145
- */
146
- location: string;
147
-
148
- /**
149
- * Returned value from the function.
150
- */
151
- value: unknown;
152
-
153
- /**
154
- * Error when occurred.
155
- */
156
- error: Error | null;
157
-
158
- /**
159
- * Elapsed time.
160
- */
161
- started_at: string;
162
-
163
- /**
164
- * Completion time.
165
- */
166
- completed_at: string;
167
- }
168
-
169
- /**
170
- * Prepare dynamic executor in strict mode.
171
- *
172
- * In strict mode, if any error occurs, the program will be terminated directly.
173
- * Otherwise, {@link validate} mode does not terminate when error occurs, but
174
- * just archive the error log.
175
- *
176
- * @param props Properties of dynamic execution
177
- * @returns Report of dynamic test functions execution
178
- */
179
- export const assert = <Arguments extends any[]>(
180
- props: IProps<Arguments>,
181
- ): Promise<IReport> => main(true)(props);
182
-
183
- /**
184
- * Prepare dynamic executor in loose mode.
185
- *
186
- * In loose mode, the program would not be terminated even when error occurs.
187
- * Instead, the error would be archived and returns as a list. Otherwise,
188
- * {@link assert} mode terminates the program directly when error occurs.
189
- *
190
- * @param props Properties of dynamic executor
191
- * @returns Report of dynamic test functions execution
192
- */
193
- export const validate = <Arguments extends any[]>(
194
- props: IProps<Arguments>,
195
- ): Promise<IReport> => main(false)(props);
196
-
197
- const main =
198
- (assert: boolean) =>
199
- async <Arguments extends any[]>(
200
- props: IProps<Arguments>,
201
- ): Promise<IReport> => {
202
- const report: IReport = {
203
- location: props.location,
204
- time: Date.now(),
205
- executions: [],
206
- };
207
-
208
- const executor = execute(props)(report)(assert);
209
- const processes: Array<() => Promise<void>> = await iterate({
210
- extension: props.extension ?? "js",
211
- location: props.location,
212
- executor,
213
- });
214
- await Promise.all(
215
- new Array(props.simultaneous ?? 1).fill(0).map(async () => {
216
- while (processes.length !== 0) {
217
- const task = processes.shift();
218
- await task?.();
219
- }
220
- }),
221
- );
222
- report.time = Date.now() - report.time;
223
- return report;
224
- };
225
-
226
- const iterate = async <Arguments extends any[]>(props: {
227
- location: string;
228
- extension: string;
229
- executor: (path: string, modulo: Module<Arguments>) => Promise<void>;
230
- }): Promise<Array<() => Promise<void>>> => {
231
- const container: Array<() => Promise<void>> = [];
232
- const visitor = async (path: string): Promise<void> => {
233
- const directory: string[] = await fs.promises.readdir(path);
234
- for (const file of directory) {
235
- const location: string = NodePath.resolve(`${path}/${file}`);
236
- const stats: fs.Stats = await fs.promises.lstat(location);
237
-
238
- if (stats.isDirectory() === true) {
239
- await visitor(location);
240
- continue;
241
- } else if (file.substr(-3) !== `.${props.extension}`) continue;
242
-
243
- const modulo: Module<Arguments> = await import(location);
244
- container.push(() => props.executor(location, modulo));
245
- }
246
- };
247
- await visitor(props.location);
248
- return container;
249
- };
250
-
251
- const execute =
252
- <Arguments extends any[]>(props: IProps<Arguments>) =>
253
- (report: IReport) =>
254
- (assert: boolean) =>
255
- async (location: string, modulo: Module<Arguments>): Promise<void> => {
256
- for (const [key, closure] of Object.entries(modulo)) {
257
- if (
258
- key.substring(0, props.prefix.length) !== props.prefix ||
259
- typeof closure !== "function" ||
260
- (props.filter && props.filter(key) === false)
261
- )
262
- continue;
263
-
264
- const func = () => {
265
- if (props.wrapper !== undefined)
266
- return props.wrapper(key, closure, props.parameters(key));
267
- else return closure(...props.parameters(key));
268
- };
269
-
270
- const result: IExecution = {
271
- name: key,
272
- location,
273
- value: undefined,
274
- error: null,
275
- started_at: new Date().toISOString(),
276
- completed_at: new Date().toISOString(),
277
- };
278
- report.executions.push(result);
279
-
280
- try {
281
- result.value = await func();
282
- result.completed_at = new Date().toISOString();
283
- } catch (exp) {
284
- result.error = exp as Error;
285
- if (assert === true) throw exp;
286
- } finally {
287
- result.completed_at = new Date().toISOString();
288
- if (props.onComplete) props.onComplete(result);
289
- }
290
- }
291
- };
292
-
293
- interface Module<Arguments extends any[]> {
294
- [key: string]: Closure<Arguments>;
295
- }
296
- }
1
+ import fs from "fs";
2
+ import NodePath from "path";
3
+
4
+ /**
5
+ * Dynamic Executor running prefixed functions.
6
+ *
7
+ * `DynamicExecutor` runs every (or some filtered) prefixed functions
8
+ * in a specific directory.
9
+ *
10
+ * For reference, it's useful for test program development of a backend server.
11
+ * Just write test functions under a directory, and just specify it.
12
+ * Furthermore, if you compose e2e test programs to utilize the `@nestia/sdk`
13
+ * generated API functions, you can take advantage of {@link DynamicBenchmarker}
14
+ * at the same time.
15
+ *
16
+ * When you want to see some utilization cases, see the below example links.
17
+ *
18
+ * @example https://github.com/samchon/nestia-start/blob/master/test/index.ts
19
+ * @example https://github.com/samchon/backend/blob/master/test/index.ts
20
+ * @author Jeongho Nam - https://github.com/samchon
21
+ */
22
+ export namespace DynamicExecutor {
23
+ /**
24
+ * Function type of a prefixed.
25
+ *
26
+ * @template Arguments Type of parameters
27
+ * @template Ret Type of return value
28
+ */
29
+ export interface Closure<Arguments extends any[], Ret = any> {
30
+ (...args: Arguments): Promise<Ret>;
31
+ }
32
+
33
+ /**
34
+ * Options for dynamic executor.
35
+ */
36
+ export interface IProps<Parameters extends any[], Ret = any> {
37
+ /**
38
+ * Prefix of function name.
39
+ *
40
+ * Every prefixed function will be executed.
41
+ *
42
+ * In other words, if a function name doesn't start with the prefix, then it would never be executed.
43
+ */
44
+ prefix: string;
45
+
46
+ /**
47
+ * Location of the test functions.
48
+ */
49
+ location: string;
50
+
51
+ /**
52
+ * Get parameters of a function.
53
+ *
54
+ * @param name Function name
55
+ * @returns Parameters
56
+ */
57
+ parameters: (name: string) => Parameters;
58
+
59
+ /**
60
+ * On complete function.
61
+ *
62
+ * Listener of completion of a test function.
63
+ *
64
+ * @param exec Execution result of a test function
65
+ */
66
+ onComplete?: (exec: IExecution) => void;
67
+
68
+ /**
69
+ * Filter function whether to run or not.
70
+ *
71
+ * @param name Function name
72
+ * @returns Whether to run or not
73
+ */
74
+ filter?: (name: string) => boolean;
75
+
76
+ /**
77
+ * Wrapper of test function.
78
+ *
79
+ * If you specify this `wrapper` property, every dynamic functions
80
+ * loaded and called by this `DynamicExecutor` would be wrapped by
81
+ * the `wrapper` function.
82
+ *
83
+ * @param name Function name
84
+ * @param closure Function to be executed
85
+ * @param parameters Parameters, result of options.parameters function.
86
+ * @returns Wrapper function
87
+ */
88
+ wrapper?: (
89
+ name: string,
90
+ closure: Closure<Parameters, Ret>,
91
+ parameters: Parameters,
92
+ ) => Promise<any>;
93
+
94
+ /**
95
+ * Number of simultaneous requests.
96
+ *
97
+ * The number of requests to be executed simultaneously.
98
+ *
99
+ * If you configure a value greater than one, the dynamic executor will
100
+ * process the functions concurrently with the given capacity value.
101
+ *
102
+ * @default 1
103
+ */
104
+ simultaneous?: number;
105
+
106
+ /**
107
+ * Extension of dynamic functions.
108
+ *
109
+ * @default js
110
+ */
111
+ extension?: string;
112
+ }
113
+
114
+ /**
115
+ * Report, result of dynamic execution.
116
+ */
117
+ export interface IReport {
118
+ /**
119
+ * Location path of dynamic functions.
120
+ */
121
+ location: string;
122
+
123
+ /**
124
+ * Execution results of dynamic functions.
125
+ */
126
+ executions: IExecution[];
127
+
128
+ /**
129
+ * Total elapsed time.
130
+ */
131
+ time: number;
132
+ }
133
+
134
+ /**
135
+ * Execution of a test function.
136
+ */
137
+ export interface IExecution {
138
+ /**
139
+ * Name of function.
140
+ */
141
+ name: string;
142
+
143
+ /**
144
+ * Location path of the function.
145
+ */
146
+ location: string;
147
+
148
+ /**
149
+ * Returned value from the function.
150
+ */
151
+ value: unknown;
152
+
153
+ /**
154
+ * Error when occurred.
155
+ */
156
+ error: Error | null;
157
+
158
+ /**
159
+ * Elapsed time.
160
+ */
161
+ started_at: string;
162
+
163
+ /**
164
+ * Completion time.
165
+ */
166
+ completed_at: string;
167
+ }
168
+
169
+ /**
170
+ * Prepare dynamic executor in strict mode.
171
+ *
172
+ * In strict mode, if any error occurs, the program will be terminated directly.
173
+ * Otherwise, {@link validate} mode does not terminate when error occurs, but
174
+ * just archive the error log.
175
+ *
176
+ * @param props Properties of dynamic execution
177
+ * @returns Report of dynamic test functions execution
178
+ */
179
+ export const assert = <Arguments extends any[]>(
180
+ props: IProps<Arguments>,
181
+ ): Promise<IReport> => main(true)(props);
182
+
183
+ /**
184
+ * Prepare dynamic executor in loose mode.
185
+ *
186
+ * In loose mode, the program would not be terminated even when error occurs.
187
+ * Instead, the error would be archived and returns as a list. Otherwise,
188
+ * {@link assert} mode terminates the program directly when error occurs.
189
+ *
190
+ * @param props Properties of dynamic executor
191
+ * @returns Report of dynamic test functions execution
192
+ */
193
+ export const validate = <Arguments extends any[]>(
194
+ props: IProps<Arguments>,
195
+ ): Promise<IReport> => main(false)(props);
196
+
197
+ const main =
198
+ (assert: boolean) =>
199
+ async <Arguments extends any[]>(
200
+ props: IProps<Arguments>,
201
+ ): Promise<IReport> => {
202
+ const report: IReport = {
203
+ location: props.location,
204
+ time: Date.now(),
205
+ executions: [],
206
+ };
207
+
208
+ const executor = execute(props)(report)(assert);
209
+ const processes: Array<() => Promise<void>> = await iterate({
210
+ extension: props.extension ?? "js",
211
+ location: props.location,
212
+ executor,
213
+ });
214
+ await Promise.all(
215
+ new Array(props.simultaneous ?? 1).fill(0).map(async () => {
216
+ while (processes.length !== 0) {
217
+ const task = processes.shift();
218
+ await task?.();
219
+ }
220
+ }),
221
+ );
222
+ report.time = Date.now() - report.time;
223
+ return report;
224
+ };
225
+
226
+ const iterate = async <Arguments extends any[]>(props: {
227
+ location: string;
228
+ extension: string;
229
+ executor: (path: string, modulo: Module<Arguments>) => Promise<void>;
230
+ }): Promise<Array<() => Promise<void>>> => {
231
+ const container: Array<() => Promise<void>> = [];
232
+ const visitor = async (path: string): Promise<void> => {
233
+ const directory: string[] = await fs.promises.readdir(path);
234
+ for (const file of directory) {
235
+ const location: string = NodePath.resolve(`${path}/${file}`);
236
+ const stats: fs.Stats = await fs.promises.lstat(location);
237
+
238
+ if (stats.isDirectory() === true) {
239
+ await visitor(location);
240
+ continue;
241
+ } else if (file.substr(-3) !== `.${props.extension}`) continue;
242
+
243
+ const modulo: Module<Arguments> = await import(location);
244
+ container.push(() => props.executor(location, modulo));
245
+ }
246
+ };
247
+ await visitor(props.location);
248
+ return container;
249
+ };
250
+
251
+ const execute =
252
+ <Arguments extends any[]>(props: IProps<Arguments>) =>
253
+ (report: IReport) =>
254
+ (assert: boolean) =>
255
+ async (location: string, modulo: Module<Arguments>): Promise<void> => {
256
+ for (const [key, closure] of Object.entries(modulo)) {
257
+ if (
258
+ key.substring(0, props.prefix.length) !== props.prefix ||
259
+ typeof closure !== "function" ||
260
+ (props.filter && props.filter(key) === false)
261
+ )
262
+ continue;
263
+
264
+ const func = () => {
265
+ if (props.wrapper !== undefined)
266
+ return props.wrapper(key, closure, props.parameters(key));
267
+ else return closure(...props.parameters(key));
268
+ };
269
+
270
+ const result: IExecution = {
271
+ name: key,
272
+ location,
273
+ value: undefined,
274
+ error: null,
275
+ started_at: new Date().toISOString(),
276
+ completed_at: new Date().toISOString(),
277
+ };
278
+ report.executions.push(result);
279
+
280
+ try {
281
+ result.value = await func();
282
+ result.completed_at = new Date().toISOString();
283
+ } catch (exp) {
284
+ result.error = exp as Error;
285
+ if (assert === true) throw exp;
286
+ } finally {
287
+ result.completed_at = new Date().toISOString();
288
+ if (props.onComplete) props.onComplete(result);
289
+ }
290
+ }
291
+ };
292
+
293
+ interface Module<Arguments extends any[]> {
294
+ [key: string]: Closure<Arguments>;
295
+ }
296
+ }