1
1
import { ProductTemplate } from "./types" ;
2
2
import { Test } from "@nestjs/testing" ;
3
3
import { InMemoryOrderRepository , InMemoryProductRepository } from "./adapters/fake" ;
4
- import { AppModule } from "./app.module" ;
5
- export async function createTestingModule ( products : ProductTemplate [ ] = [ ] ) {
4
+ import { AppModuleInversionOfControl } from "./app.module.ioc" ;
5
+ import { AppModuleOverrides } from "./app.module.overrides" ;
6
+ import { ORDER_REPO , PRODUCT_REPO } from "./adapters" ;
7
+ import { MongoDBModule } from "./adapters/mongodb.module" ;
8
+ import { Module } from "@nestjs/common" ;
9
+ import { AppModuleWithRegister } from "./app.module.register" ;
10
+ import { MemoryModule } from "./adapters/memory.module" ;
11
+ export async function createTestingModuleWithIoC ( products : ProductTemplate [ ] = [ ] ) {
6
12
const productRepo = new InMemoryProductRepository ( products ) ;
7
13
const orderRepo = new InMemoryOrderRepository ( ) ;
8
14
const testingModule = await Test . createTestingModule ( {
9
- imports : [ AppModule . register ( productRepo , orderRepo ) ] ,
15
+ imports : [ AppModuleInversionOfControl . register ( productRepo , orderRepo ) ] ,
10
16
} )
11
17
. compile ( ) ;
12
18
13
19
const nest = testingModule . createNestApplication ( ) ;
14
20
nest . enableCors ( { origin : "*" } ) ;
15
21
await nest . init ( ) ;
16
22
return { nest, orderRepo, productRepo} ;
17
- }
23
+ }
24
+
25
+ export async function createTestingModuleWithOverrides ( products : ProductTemplate [ ] = [ ] ) {
26
+ const productRepo = new InMemoryProductRepository ( products ) ;
27
+ const orderRepo = new InMemoryOrderRepository ( ) ;
28
+ const testingModule = await Test . createTestingModule ( {
29
+ imports : [ AppModuleOverrides ] ,
30
+ } )
31
+ . overrideModule ( MongoDBModule ) . useModule ( NopModule ) // this doesn't actually do anything, MongoDB needs to be available for connection even though we don't use it
32
+ . overrideProvider ( PRODUCT_REPO ) . useValue ( productRepo )
33
+ . overrideProvider ( ORDER_REPO ) . useValue ( orderRepo )
34
+ . compile ( ) ;
35
+
36
+ const nest = testingModule . createNestApplication ( ) ;
37
+ nest . enableCors ( { origin : "*" } ) ;
38
+ await nest . init ( ) ;
39
+ return { nest, orderRepo, productRepo} ;
40
+ }
41
+
42
+ export async function createTestingModuleWithRegister ( products : ProductTemplate [ ] = [ ] ) {
43
+
44
+ const testingModule = await Test . createTestingModule ( {
45
+ imports : [ AppModuleWithRegister . register ( MemoryModule . forTests ( products ) ) ] ,
46
+ } )
47
+ . compile ( ) ;
48
+
49
+ const productRepo : InMemoryProductRepository = testingModule . get ( PRODUCT_REPO ) ;
50
+ const orderRepo : InMemoryOrderRepository = testingModule . get ( ORDER_REPO ) ;
51
+
52
+ const nest = testingModule . createNestApplication ( ) ;
53
+ nest . enableCors ( { origin : "*" } ) ;
54
+ await nest . init ( ) ;
55
+ return { nest, orderRepo, productRepo} ;
56
+ }
57
+
58
+ // export const createTestingModule = createTestingModuleWithOverrides;
59
+ // export const createTestingModule = createTestingModuleWithIoC;
60
+ export const createTestingModule = createTestingModuleWithRegister ;
61
+
62
+ @Module ( {
63
+ providers : [ {
64
+ provide : "storeDB" ,
65
+ useValue : null
66
+ } ] ,
67
+ } )
68
+ class NopModule { }
0 commit comments