@velajs/testing 0.4.0 → 0.5.1

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,160 +0,0 @@
1
- import { DiscoveryService, REQUEST_CONTEXT, Scope } from "@velajs/vela";
2
- import { APP_FILTER, APP_GUARD, APP_INTERCEPTOR, APP_MIDDLEWARE, APP_PIPE, Container, MetadataRegistry, ModuleLoader, ModuleRef, RouteManager, VelaApplication, bindAppProviders } from "@velajs/vela/internal";
3
- import { TestingModule } from "./testing-module.js";
4
- export class OverrideBy {
5
- builder;
6
- token;
7
- constructor(builder, token){
8
- this.builder = builder;
9
- this.token = token;
10
- }
11
- useValue(value) {
12
- this.builder['addOverride']({
13
- token: this.token,
14
- provider: {
15
- provide: this.token,
16
- useValue: value
17
- }
18
- });
19
- return this.builder;
20
- }
21
- useClass(cls) {
22
- this.builder['addOverride']({
23
- token: this.token,
24
- provider: {
25
- provide: this.token,
26
- useClass: cls
27
- }
28
- });
29
- return this.builder;
30
- }
31
- useFactory(options) {
32
- this.builder['addOverride']({
33
- token: this.token,
34
- provider: {
35
- provide: this.token,
36
- useFactory: options.factory,
37
- inject: options.inject
38
- }
39
- });
40
- return this.builder;
41
- }
42
- }
43
- export class TestingModuleBuilder {
44
- metadata;
45
- overrides = [];
46
- constructor(metadata){
47
- this.metadata = metadata;
48
- }
49
- overrideProvider(token) {
50
- return new OverrideBy(this, token);
51
- }
52
- overrideGuard(guard) {
53
- return new OverrideBy(this, guard);
54
- }
55
- overridePipe(pipe) {
56
- return new OverrideBy(this, pipe);
57
- }
58
- overrideInterceptor(interceptor) {
59
- return new OverrideBy(this, interceptor);
60
- }
61
- overrideFilter(filter) {
62
- return new OverrideBy(this, filter);
63
- }
64
- addOverride(entry) {
65
- const idx = this.overrides.findIndex((o)=>o.token === entry.token);
66
- if (idx !== -1) {
67
- this.overrides[idx] = entry;
68
- } else {
69
- this.overrides.push(entry);
70
- }
71
- }
72
- async compile() {
73
- let TestRootModule = class TestRootModule {
74
- };
75
- MetadataRegistry.setModuleOptions(TestRootModule, {
76
- imports: this.metadata.imports,
77
- providers: this.metadata.providers,
78
- controllers: this.metadata.controllers,
79
- exports: this.metadata.exports
80
- });
81
- const container = new Container();
82
- // Replicate bootstrap()'s global token setup so request-pipeline tests
83
- // can resolve REQUEST_CONTEXT, ModuleRef, and the APP_* sentinel tokens
84
- // from any module scope. Without this, guards / decorators that inject
85
- // REQUEST_CONTEXT through ExecutionContext fail with "no provider"
86
- // when the request enters the pipeline. See vela/src/factory/bootstrap.ts.
87
- container.register({
88
- provide: Container,
89
- useValue: container
90
- });
91
- container.markGlobalToken(Container);
92
- container.register({
93
- provide: ModuleRef,
94
- useFactory: (c)=>new ModuleRef(c),
95
- inject: [
96
- Container
97
- ]
98
- });
99
- container.markGlobalToken(ModuleRef);
100
- // Decorator-driven discovery — global so any provider can inject it, and so
101
- // callOnApplicationBootstrap() reuses this instance instead of self-building
102
- // one. Mirrors vela/src/factory/bootstrap.ts.
103
- container.register({
104
- provide: DiscoveryService,
105
- useFactory: (c)=>new DiscoveryService(c),
106
- inject: [
107
- Container
108
- ]
109
- });
110
- container.markGlobalToken(DiscoveryService);
111
- for (const t of [
112
- APP_GUARD,
113
- APP_PIPE,
114
- APP_INTERCEPTOR,
115
- APP_FILTER,
116
- APP_MIDDLEWARE
117
- ]){
118
- container.markGlobalToken(t);
119
- }
120
- // REQUEST_CONTEXT is seeded into each per-request child container by
121
- // RouteManager via setRequestInstance. Registering with a throw-factory
122
- // here ensures findRegistration succeeds (so the child's cached request
123
- // instance is returned) while surfacing a clear error if the token is
124
- // ever resolved outside the request path.
125
- container.register({
126
- provide: REQUEST_CONTEXT,
127
- scope: Scope.REQUEST,
128
- useFactory: ()=>{
129
- throw new Error('REQUEST_CONTEXT can only be resolved inside a request — ' + 'it is seeded by RouteManager when the request enters the pipeline.');
130
- }
131
- });
132
- container.markGlobalToken(REQUEST_CONTEXT);
133
- // Pre-register at root so `moduleRef.get(token)` and framework-internal
134
- // resolves (instantiate(guard, container) with no requestingModuleId) see
135
- // the override immediately.
136
- for (const override of this.overrides){
137
- container.register(override.provider);
138
- }
139
- const routeManager = new RouteManager(container);
140
- const loader = new ModuleLoader(container, routeManager);
141
- loader.load(TestRootModule);
142
- // Force-apply overrides into every module bucket that already holds the
143
- // token (plus root). Without this, controller constructor-injection (which
144
- // passes requestingModuleId to findRegistration) finds the module's own
145
- // registration first and never consults the root override. The default
146
- // 'all-existing' buckets replace every non-root bucket holding the token
147
- // and re-register at root — the supported form of the old private loop.
148
- for (const override of this.overrides){
149
- container.replaceProvider(override.provider);
150
- }
151
- bindAppProviders(routeManager, container, loader);
152
- const app = new VelaApplication(container, routeManager);
153
- const instances = await loader.resolveAllInstances();
154
- app.setInstances(instances);
155
- await app.callOnModuleInit();
156
- await app.callOnApplicationBootstrap();
157
- await app.initRoutes();
158
- return new TestingModule(app, container);
159
- }
160
- }
@@ -1,10 +0,0 @@
1
- import type { Token, VelaApplication } from '@velajs/vela';
2
- import type { Container } from '@velajs/vela/internal';
3
- export declare class TestingModule {
4
- private readonly app;
5
- private readonly container;
6
- constructor(app: VelaApplication, container: Container);
7
- get<T>(token: Token<T>): T;
8
- createApplication(): Promise<VelaApplication>;
9
- close(signal?: string): Promise<void>;
10
- }
@@ -1,18 +0,0 @@
1
- export class TestingModule {
2
- app;
3
- container;
4
- constructor(app, container){
5
- this.app = app;
6
- this.container = container;
7
- }
8
- get(token) {
9
- return this.container.resolve(token);
10
- }
11
- async createApplication() {
12
- await this.app.initRoutes();
13
- return this.app;
14
- }
15
- async close(signal) {
16
- await this.app.close(signal);
17
- }
18
- }