1
1
import fs from "fs"
2
2
import path from "path"
3
3
4
- import { INestApplication } from "@nestjs/common"
5
- import { DynamicModule } from "@nestjs/common/interfaces/modules/dynamic-module.interface"
6
- import { ForwardReference } from "@nestjs/common/interfaces/modules/forward-reference.interface"
4
+ import { ModuleMetadata } from "@nestjs/common/interfaces/modules/module-metadata.interface"
7
5
import { Provider } from "@nestjs/common/interfaces/modules/provider.interface"
8
- import { Type } from "@nestjs/common/interfaces/type.interface"
9
- import { FastifyAdapter } from "@nestjs/platform-fastify"
6
+ import { FastifyAdapter , NestFastifyApplication } from "@nestjs/platform-fastify"
10
7
import { Test , TestingModule } from "@nestjs/testing"
11
8
import { Connection , createConnection } from "typeorm"
12
- import { getRepository } from "typeorm"
13
9
import { Builder , Loader , Parser , Resolver , fixturesIterator } from "typeorm-fixtures-cli/dist"
10
+ import { ConnectionOptions } from "typeorm/connection/ConnectionOptions"
14
11
15
12
import { configurationTest } from "../config/configuration.test"
16
13
import { getRootModuleImports } from "./utils"
17
14
18
15
export class TestingHelper {
19
16
module : TestingModule
20
- app : INestApplication
17
+ app : NestFastifyApplication
21
18
22
- async initializeModuleAndApp (
23
- testName : string ,
24
- importedModules : Array < Type < unknown > | DynamicModule | Promise < DynamicModule > | ForwardReference > ,
25
- providers : Provider [ ] = [ ]
26
- ) {
19
+ async initializeModuleAndApp ( testName : string , importedModules : ModuleMetadata [ "imports" ] , providers : Provider [ ] = undefined ) {
27
20
const databaseName = `stator_test_${ testName } `
28
21
const configuration = configurationTest . bind ( this , databaseName )
29
22
30
- const connection = await createConnection ( { ...configuration ( ) . database } )
23
+ const connectionOptions : ConnectionOptions = { ...configuration ( ) . database }
24
+ const connection = await createConnection ( connectionOptions )
31
25
await this . createDatabaseIfNotExist ( connection , databaseName )
32
26
33
27
this . module = await Test . createTestingModule ( {
34
28
imports : [ ...getRootModuleImports ( configuration ) , ...importedModules ] ,
35
29
providers : providers ,
36
30
} ) . compile ( )
37
31
38
- this . app = this . module . createNestApplication ( new FastifyAdapter ( ) )
32
+ this . app = this . module . createNestApplication < NestFastifyApplication > ( new FastifyAdapter ( ) )
39
33
40
34
await this . app . init ( )
41
35
await this . app . getHttpAdapter ( ) . getInstance ( ) . ready ( )
@@ -56,7 +50,7 @@ export class TestingHelper {
56
50
57
51
for ( const fixture of fixtures ) {
58
52
const entity = await builder . build ( fixture )
59
- await getRepository ( entity . constructor . name ) . save ( entity )
53
+ await connection . getRepository ( entity . constructor . name ) . save ( entity )
60
54
}
61
55
}
62
56
@@ -81,5 +75,6 @@ PERFORM dblink_exec('', 'CREATE DATABASE ${databaseName}');
81
75
EXCEPTION WHEN duplicate_database THEN RAISE NOTICE '%, skipping', SQLERRM USING ERRCODE = SQLSTATE;
82
76
END
83
77
$$;` )
78
+ await connection . close ( )
84
79
}
85
80
}
0 commit comments