-
Notifications
You must be signed in to change notification settings - Fork 355
/
Copy pathtest.utils.ts
109 lines (102 loc) · 2.64 KB
/
test.utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright 2020-2024 SubQuery Pte Ltd authors & contributors
// SPDX-License-Identifier: GPL-3.0
// Utils for integration tests
import { DynamicModule, INestApplication } from '@nestjs/common';
import { EventEmitterModule } from '@nestjs/event-emitter';
import { ScheduleModule } from '@nestjs/schedule';
import { Test } from '@nestjs/testing';
import { DbModule, NodeConfig, registerApp } from '@subql/node-core';
import { ConfigureModule } from '../configure/configure.module';
import { SubqueryProject } from '../configure/SubqueryProject';
import { FetchModule } from '../indexer/fetch.module';
import { MetaModule } from '../meta/meta.module';
const mockInstance = async (
cid: string,
schemaName: string,
disableHistorical: boolean,
useSubscription: boolean,
timestampField: boolean,
) => {
const argv: Record<string, any> = {
_: [],
disableHistorical,
subquery: `ipfs://${cid}`,
dbSchema: schemaName,
allowSchemaMigration: true,
ipfs: 'https://unauthipfs.subquery.network/ipfs/api/v0',
networkEndpoint: 'wss://rpc.polkadot.io/public-ws',
timestampField,
subscription: useSubscription,
};
return registerApp<SubqueryProject>(
argv,
SubqueryProject.create.bind(SubqueryProject),
jest.fn(),
'',
);
};
async function mockRegister(
cid: string,
schemaName: string,
disableHistorical: boolean,
useSubscription: boolean,
timestampField: boolean,
): Promise<DynamicModule> {
const { nodeConfig, project } = await mockInstance(
cid,
schemaName,
disableHistorical,
useSubscription,
timestampField,
);
return {
module: ConfigureModule,
providers: [
{
provide: NodeConfig,
useValue: nodeConfig,
},
{
provide: 'ISubqueryProject',
useValue: project,
},
{
provide: 'IProjectUpgradeService',
useValue: project,
},
{
provide: 'Null',
useValue: null,
},
],
exports: [NodeConfig, 'ISubqueryProject', 'IProjectUpgradeService', 'Null'],
};
}
export async function prepareApp(
schemaName: string,
cid: string,
disableHistorical = false,
useSubscription = false,
timestampField = false,
): Promise<INestApplication> {
const m = await Test.createTestingModule({
imports: [
DbModule.forRoot(),
EventEmitterModule.forRoot(),
mockRegister(
cid,
schemaName,
disableHistorical,
useSubscription,
timestampField,
),
ScheduleModule.forRoot(),
FetchModule,
MetaModule,
],
controllers: [],
}).compile();
const app = m.createNestApplication();
await app.init();
return app;
}