• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

safe-global / safe-client-gateway / 16826387570

08 Aug 2025 08:59AM UTC coverage: 89.502% (+0.002%) from 89.5%
16826387570

push

github

web-flow
feat: add the ability to override guards to the testing module (#2667)

This PR enables guards override in the testing module.

3479 of 4275 branches covered (81.38%)

Branch coverage included in aggregate %.

1 of 2 new or added lines in 1 file covered. (50.0%)

12106 of 13138 relevant lines covered (92.14%)

542.52 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.64
/src/__tests__/testing-module.ts
1
import { Test, type TestingModule } from '@nestjs/testing';
102✔
2
import { AppModule } from '@/app.module';
102✔
3
import configuration from '@/config/entities/__tests__/configuration';
102✔
4
import { CacheModule } from '@/datasources/cache/cache.module';
102✔
5
import { TestCacheModule } from '@/datasources/cache/__tests__/test.cache.module';
102✔
6
import { RequestScopedLoggingModule } from '@/logging/logging.module';
102✔
7
import { TestLoggingModule } from '@/logging/__tests__/test.logging.module';
102✔
8
import { NetworkModule } from '@/datasources/network/network.module';
102✔
9
import { TestNetworkModule } from '@/datasources/network/__tests__/test.network.module';
102✔
10
import { QueuesApiModule } from '@/datasources/queues/queues-api.module';
102✔
11
import { TestQueuesApiModule } from '@/datasources/queues/__tests__/test.queues-api.module';
102✔
12
import { PostgresDatabaseModule } from '@/datasources/db/v1/postgres-database.module';
102✔
13
import { TestPostgresDatabaseModule } from '@/datasources/db/__tests__/test.postgres-database.module';
102✔
14
import { PostgresDatabaseModuleV2 } from '@/datasources/db/v2/postgres-database.module';
102✔
15
import { TestPostgresDatabaseModuleV2 } from '@/datasources/db/v2/test.postgres-database.module';
102✔
16
import { TargetedMessagingDatasourceModule } from '@/datasources/targeted-messaging/targeted-messaging.datasource.module';
102✔
17
import { TestTargetedMessagingDatasourceModule } from '@/datasources/targeted-messaging/__tests__/test.targeted-messaging.datasource.module';
102✔
18
import type { ModuleDefinition } from '@nestjs/core/interfaces/module-definition.interface';
19
import { CacheKeyPrefix } from '@/datasources/cache/constants';
102✔
20
import type { Provider } from '@nestjs/common';
21

22
export interface CreateBaseTestModuleOptions {
23
  config?: typeof configuration;
24
  overridePostgresV2?: boolean;
25
  cacheKeyPrefix?: string;
26
  modules?: Array<ModuleOverride>;
27
  providers?: Array<Provider>;
28
  guards?: Array<GuardOverride>;
29
}
30

31
export interface ModuleOverride {
32
  originalModule: ModuleDefinition;
33
  testModule: ModuleDefinition;
34
}
35

36
export interface GuardOverride {
37
  originalGuard: unknown;
38
  testGuard: unknown;
39
}
40

41
export async function createTestModule(
102✔
42
  options: CreateBaseTestModuleOptions = {},
118✔
43
): Promise<TestingModule> {
44
  const {
45
    config,
46
    cacheKeyPrefix,
47
    overridePostgresV2,
48
    modules: additionalOverrides = [],
504✔
49
    guards: guards = [],
795✔
50
    providers = [],
700✔
51
  } = options;
1,590✔
52

53
  return createBaseTestModule({
1,590✔
54
    config,
55
    overridePostgresV2,
56
    cacheKeyPrefix,
57
    guards,
58
    providers,
59
    modules: [
60
      {
61
        originalModule: CacheModule,
62
        testModule: TestCacheModule,
63
      },
64
      {
65
        originalModule: RequestScopedLoggingModule,
66
        testModule: TestLoggingModule,
67
      },
68
      {
69
        originalModule: NetworkModule,
70
        testModule: TestNetworkModule,
71
      },
72
      ...additionalOverrides,
73
    ],
74
  });
75
}
76

77
export async function createBaseTestModule(
102✔
78
  options: CreateBaseTestModuleOptions = {},
×
79
): Promise<TestingModule> {
80
  const {
81
    config = configuration,
145✔
82
    overridePostgresV2 = true, // Enable Postgres V2 by default
750✔
83
    cacheKeyPrefix = crypto.randomUUID(),
795✔
84
    modules: additionalOverrides = [],
×
85
    guards: guards = [],
×
86
    providers = [],
×
87
  } = options;
1,590✔
88

89
  const moduleBuilder = Test.createTestingModule({
1,590✔
90
    imports: [AppModule.register(config)],
91
    providers: providers,
92
  })
93
    .overrideProvider(CacheKeyPrefix)
94
    .useValue(cacheKeyPrefix)
95
    .overrideModule(PostgresDatabaseModule)
96
    .useModule(TestPostgresDatabaseModule)
97
    .overrideModule(TargetedMessagingDatasourceModule)
98
    .useModule(TestTargetedMessagingDatasourceModule)
99
    .overrideModule(QueuesApiModule)
100
    .useModule(TestQueuesApiModule);
101

102
  if (overridePostgresV2) {
1,590✔
103
    moduleBuilder
1,500✔
104
      .overrideModule(PostgresDatabaseModuleV2)
105
      .useModule(TestPostgresDatabaseModuleV2);
106
  }
107

108
  for (const guard of guards) {
1,590✔
NEW
109
    moduleBuilder.overrideGuard(guard.originalGuard).useValue(guard.testGuard);
×
110
  }
111

112
  // Apply additional overrides
113
  for (const override of additionalOverrides) {
1,590✔
114
    moduleBuilder
6,320✔
115
      .overrideModule(override.originalModule)
116
      .useModule(override.testModule);
117
  }
118

119
  return moduleBuilder.compile();
1,590✔
120
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc