@nestjs/testing 9.4.3 → 10.0.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.
- package/interfaces/mock-factory.d.ts +2 -2
- package/package.json +5 -5
- package/test.js +1 -1
- package/testing-module.builder.d.ts +5 -0
- package/testing-module.builder.js +28 -7
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export type MockFactory = (token?:
|
|
1
|
+
import { InjectionToken } from '@nestjs/common';
|
|
2
|
+
export type MockFactory = (token?: InjectionToken) => any;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nestjs/testing",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "10.0.1",
|
|
4
4
|
"description": "Nest - modern, fast, powerful node.js web framework (@testing)",
|
|
5
5
|
"author": "Kamil Mysliwiec",
|
|
6
6
|
"license": "MIT",
|
|
@@ -21,10 +21,10 @@
|
|
|
21
21
|
"tslib": "2.5.3"
|
|
22
22
|
},
|
|
23
23
|
"peerDependencies": {
|
|
24
|
-
"@nestjs/common": "^
|
|
25
|
-
"@nestjs/core": "^
|
|
26
|
-
"@nestjs/microservices": "^
|
|
27
|
-
"@nestjs/platform-express": "^
|
|
24
|
+
"@nestjs/common": "^10.0.0",
|
|
25
|
+
"@nestjs/core": "^10.0.0",
|
|
26
|
+
"@nestjs/microservices": "^10.0.0",
|
|
27
|
+
"@nestjs/platform-express": "^10.0.0"
|
|
28
28
|
},
|
|
29
29
|
"peerDependenciesMeta": {
|
|
30
30
|
"@nestjs/microservices": {
|
package/test.js
CHANGED
|
@@ -8,5 +8,5 @@ class Test {
|
|
|
8
8
|
return new testing_module_builder_1.TestingModuleBuilder(this.metadataScanner, metadata);
|
|
9
9
|
}
|
|
10
10
|
}
|
|
11
|
-
Test.metadataScanner = new metadata_scanner_1.MetadataScanner();
|
|
12
11
|
exports.Test = Test;
|
|
12
|
+
Test.metadataScanner = new metadata_scanner_1.MetadataScanner();
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { LoggerService, ModuleMetadata } from '@nestjs/common';
|
|
2
2
|
import { NestApplicationContextOptions } from '@nestjs/common/interfaces/nest-application-context-options.interface';
|
|
3
3
|
import { MetadataScanner } from '@nestjs/core/metadata-scanner';
|
|
4
|
+
import { ModuleDefinition } from '../core/interfaces/module-definition.interface';
|
|
4
5
|
import { MockFactory, OverrideBy } from './interfaces';
|
|
6
|
+
import { OverrideModule } from './interfaces/override-module.interface';
|
|
5
7
|
import { TestingModule } from './testing-module';
|
|
6
8
|
/**
|
|
7
9
|
* @publicApi
|
|
@@ -11,6 +13,7 @@ export declare class TestingModuleBuilder {
|
|
|
11
13
|
private readonly applicationConfig;
|
|
12
14
|
private readonly container;
|
|
13
15
|
private readonly overloadsMap;
|
|
16
|
+
private readonly moduleOverloadsMap;
|
|
14
17
|
private readonly module;
|
|
15
18
|
private testingLogger;
|
|
16
19
|
private mocker?;
|
|
@@ -22,10 +25,12 @@ export declare class TestingModuleBuilder {
|
|
|
22
25
|
overrideGuard<T = any>(typeOrToken: T): OverrideBy;
|
|
23
26
|
overrideInterceptor<T = any>(typeOrToken: T): OverrideBy;
|
|
24
27
|
overrideProvider<T = any>(typeOrToken: T): OverrideBy;
|
|
28
|
+
overrideModule(moduleToOverride: ModuleDefinition): OverrideModule;
|
|
25
29
|
compile(options?: Pick<NestApplicationContextOptions, 'snapshot' | 'preview'>): Promise<TestingModule>;
|
|
26
30
|
private override;
|
|
27
31
|
private createOverrideByBuilder;
|
|
28
32
|
private applyOverloadsMap;
|
|
33
|
+
private getModuleOverloads;
|
|
29
34
|
private getRootModule;
|
|
30
35
|
private createInstancesOfDependencies;
|
|
31
36
|
private createModule;
|
|
@@ -21,6 +21,7 @@ class TestingModuleBuilder {
|
|
|
21
21
|
this.applicationConfig = new application_config_1.ApplicationConfig();
|
|
22
22
|
this.container = new container_1.NestContainer(this.applicationConfig);
|
|
23
23
|
this.overloadsMap = new Map();
|
|
24
|
+
this.moduleOverloadsMap = new Map();
|
|
24
25
|
this.module = this.createModule(metadata);
|
|
25
26
|
}
|
|
26
27
|
setLogger(testingLogger) {
|
|
@@ -46,10 +47,18 @@ class TestingModuleBuilder {
|
|
|
46
47
|
overrideProvider(typeOrToken) {
|
|
47
48
|
return this.override(typeOrToken, true);
|
|
48
49
|
}
|
|
50
|
+
overrideModule(moduleToOverride) {
|
|
51
|
+
return {
|
|
52
|
+
useModule: newModule => {
|
|
53
|
+
this.moduleOverloadsMap.set(moduleToOverride, newModule);
|
|
54
|
+
return this;
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
}
|
|
49
58
|
async compile(options = {}) {
|
|
50
59
|
this.applyLogger();
|
|
51
60
|
let graphInspector;
|
|
52
|
-
if (options
|
|
61
|
+
if (options?.snapshot) {
|
|
53
62
|
graphInspector = new graph_inspector_1.GraphInspector(this.container);
|
|
54
63
|
uuid_factory_1.UuidFactory.mode = uuid_factory_1.UuidFactoryMode.Deterministic;
|
|
55
64
|
}
|
|
@@ -58,7 +67,9 @@ class TestingModuleBuilder {
|
|
|
58
67
|
uuid_factory_1.UuidFactory.mode = uuid_factory_1.UuidFactoryMode.Random;
|
|
59
68
|
}
|
|
60
69
|
const scanner = new scanner_1.DependenciesScanner(this.container, this.metadataScanner, graphInspector, this.applicationConfig);
|
|
61
|
-
await scanner.scan(this.module
|
|
70
|
+
await scanner.scan(this.module, {
|
|
71
|
+
overrides: this.getModuleOverloads(),
|
|
72
|
+
});
|
|
62
73
|
this.applyOverloadsMap();
|
|
63
74
|
await this.createInstancesOfDependencies(graphInspector, options);
|
|
64
75
|
scanner.applyApplicationProviders();
|
|
@@ -67,7 +78,10 @@ class TestingModuleBuilder {
|
|
|
67
78
|
}
|
|
68
79
|
override(typeOrToken, isProvider) {
|
|
69
80
|
const addOverload = (options) => {
|
|
70
|
-
this.overloadsMap.set(typeOrToken,
|
|
81
|
+
this.overloadsMap.set(typeOrToken, {
|
|
82
|
+
...options,
|
|
83
|
+
isProvider,
|
|
84
|
+
});
|
|
71
85
|
return this;
|
|
72
86
|
};
|
|
73
87
|
return this.createOverrideByBuilder(addOverload);
|
|
@@ -75,23 +89,30 @@ class TestingModuleBuilder {
|
|
|
75
89
|
createOverrideByBuilder(add) {
|
|
76
90
|
return {
|
|
77
91
|
useValue: value => add({ useValue: value }),
|
|
78
|
-
useFactory: (options) => add(
|
|
92
|
+
useFactory: (options) => add({ ...options, useFactory: options.factory }),
|
|
79
93
|
useClass: metatype => add({ useClass: metatype }),
|
|
80
94
|
};
|
|
81
95
|
}
|
|
82
96
|
applyOverloadsMap() {
|
|
83
|
-
[...this.overloadsMap.entries()]
|
|
97
|
+
const overloads = [...this.overloadsMap.entries()];
|
|
98
|
+
overloads.forEach(([item, options]) => {
|
|
84
99
|
this.container.replace(item, options);
|
|
85
100
|
});
|
|
86
101
|
}
|
|
102
|
+
getModuleOverloads() {
|
|
103
|
+
const overloads = [...this.moduleOverloadsMap.entries()];
|
|
104
|
+
return overloads.map(([moduleToReplace, newModule]) => ({
|
|
105
|
+
moduleToReplace,
|
|
106
|
+
newModule,
|
|
107
|
+
}));
|
|
108
|
+
}
|
|
87
109
|
getRootModule() {
|
|
88
110
|
const modules = this.container.getModules().values();
|
|
89
111
|
return modules.next().value;
|
|
90
112
|
}
|
|
91
113
|
async createInstancesOfDependencies(graphInspector, options) {
|
|
92
|
-
var _a;
|
|
93
114
|
const injector = new testing_injector_1.TestingInjector({
|
|
94
|
-
preview:
|
|
115
|
+
preview: options?.preview ?? false,
|
|
95
116
|
});
|
|
96
117
|
const instanceLoader = new testing_instance_loader_1.TestingInstanceLoader(this.container, injector, graphInspector);
|
|
97
118
|
await instanceLoader.createInstancesOfDependencies(this.container.getModules(), this.mocker);
|