1
1
import { faker } from '@faker-js/faker' ;
2
2
import { INestApplication } from '@nestjs/common' ;
3
3
import { Test } from '@nestjs/testing' ;
4
+ import { andCall } from '~/common' ;
4
5
import { AppModule } from '../../src/app.module' ;
5
6
import { LogLevel } from '../../src/core/logger' ;
6
7
import { LevelMatcher } from '../../src/core/logger/level-matcher' ;
7
8
import {
8
9
createGraphqlClient ,
9
10
GraphQLTestClient ,
10
11
} from './create-graphql-client' ;
12
+ import { ephemeralEdgeDB } from './edgedb-setup' ;
11
13
12
14
// Patch faker email to be more unique
13
15
const origEmail = faker . internet . email . bind ( faker . internet ) ;
@@ -19,16 +21,29 @@ export interface TestApp extends INestApplication {
19
21
}
20
22
21
23
export const createTestApp = async ( ) => {
22
- const moduleFixture = await Test . createTestingModule ( {
23
- imports : [ AppModule ] ,
24
- } )
25
- . overrideProvider ( LevelMatcher )
26
- . useValue ( new LevelMatcher ( [ ] , LogLevel . ERROR ) )
27
- . compile ( ) ;
24
+ const db = await ephemeralEdgeDB ( ) ;
28
25
29
- const app = moduleFixture . createNestApplication < TestApp > ( ) ;
30
- await app . init ( ) ;
31
- app . graphql = await createGraphqlClient ( app ) ;
26
+ try {
27
+ const moduleFixture = await Test . createTestingModule ( {
28
+ imports : [ AppModule ] ,
29
+ } )
30
+ . overrideProvider ( LevelMatcher )
31
+ . useValue ( new LevelMatcher ( [ ] , LogLevel . ERROR ) )
32
+ . overrideProvider ( 'EDGEDB_CONNECT' )
33
+ . useValue ( db ?. options )
34
+ . compile ( ) ;
32
35
33
- return app ;
36
+ const app = moduleFixture . createNestApplication < TestApp > ( ) ;
37
+ await app . init ( ) ;
38
+ app . graphql = await createGraphqlClient ( app ) ;
39
+
40
+ andCall ( app , 'close' , async ( ) => {
41
+ await db ?. cleanup ( ) ;
42
+ } ) ;
43
+
44
+ return app ;
45
+ } catch ( e ) {
46
+ await db ?. cleanup ( ) ;
47
+ throw e ;
48
+ }
34
49
} ;
0 commit comments