Skip to content

Commit

Permalink
Isolate @nestjs/testing dependency (safe-global#526)
Browse files Browse the repository at this point in the history
  • Loading branch information
hectorgomezv authored Jul 3, 2023
1 parent 8f2d84e commit 95e1d8e
Show file tree
Hide file tree
Showing 27 changed files with 64 additions and 58 deletions.
37 changes: 37 additions & 0 deletions src/__tests__/test-app.provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { INestApplication } from '@nestjs/common';
import { TestingModule } from '@nestjs/testing';
import {
AppProvider,
DEFAULT_CONFIGURATION,
configureShutdownHooks,
} from '../app.provider';

/**
* A test {@link AppProvider}
*
* This provider provides an application given a {@link TestingModule}
*
* If the module provided is not a {@link TestingModule}, an error is thrown
*/
export class TestAppProvider extends AppProvider {
// Disables shutdown hooks for tests (they are not required)
// Enabling this in the tests might result in a MaxListenersExceededWarning
// as the number of listeners that this adds exceed the default
protected readonly configuration: Array<(app: INestApplication) => void> =
DEFAULT_CONFIGURATION.filter((config) => config !== configureShutdownHooks);

constructor() {
super();
if (process.env.NODE_ENV !== 'test') {
throw Error('TestAppProvider used outside of a testing environment');
}
}

protected getApp(module: any): Promise<INestApplication> {
if (!(module instanceof TestingModule))
return Promise.reject(
`${module.constructor.name} is not a TestingModule`,
);
return Promise.resolve(module.createNestApplication());
}
}
35 changes: 2 additions & 33 deletions src/app.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ import { INestApplication, VersioningType } from '@nestjs/common';
import { DataSourceErrorFilter } from './routes/common/filters/data-source-error.filter';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { NestFactory } from '@nestjs/core';
import { TestingModule } from '@nestjs/testing/testing-module';

function configureVersioning(app: INestApplication) {
app.enableVersioning({
type: VersioningType.URI,
});
}

function configureShutdownHooks(app: INestApplication) {
export function configureShutdownHooks(app: INestApplication) {
app.enableShutdownHooks();
}

Expand All @@ -28,7 +27,7 @@ function configureSwagger(app: INestApplication) {
SwaggerModule.setup('', app, document);
}

const DEFAULT_CONFIGURATION: ((app: INestApplication) => void)[] = [
export const DEFAULT_CONFIGURATION: ((app: INestApplication) => void)[] = [
configureVersioning,
configureShutdownHooks,
configureFilters,
Expand Down Expand Up @@ -73,33 +72,3 @@ export class DefaultAppProvider extends AppProvider {
return NestFactory.create(module);
}
}

/**
* A test {@link AppProvider}
*
* This provider provides an application given a {@link TestingModule}
*
* If the module provided is not a {@link TestingModule}, an error is thrown
*/
export class TestAppProvider extends AppProvider {
// Disables shutdown hooks for tests (they are not required)
// Enabling this in the tests might result in a MaxListenersExceededWarning
// as the number of listeners that this adds exceed the default
protected readonly configuration: Array<(app: INestApplication) => void> =
DEFAULT_CONFIGURATION.filter((config) => config !== configureShutdownHooks);

constructor() {
super();
if (process.env.NODE_ENV !== 'test') {
throw Error('TestAppProvider used outside of a testing environment');
}
}

protected getApp(module: any): Promise<INestApplication> {
if (!(module instanceof TestingModule))
return Promise.reject(
`${module.constructor.name} is not a TestingModule`,
);
return Promise.resolve(module.createNestApplication());
}
}
2 changes: 1 addition & 1 deletion src/routes/balances/balances.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { INestApplication } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import * as request from 'supertest';
import { TestAppProvider } from '../../app.provider';
import { TestAppProvider } from '../../__tests__/test-app.provider';
import { TestCacheModule } from '../../datasources/cache/__tests__/test.cache.module';
import { TestNetworkModule } from '../../datasources/network/__tests__/test.network.module';
import { DomainModule } from '../../domain.module';
Expand Down
2 changes: 1 addition & 1 deletion src/routes/chains/chains.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { faker } from '@faker-js/faker';
import { INestApplication } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import * as request from 'supertest';
import { TestAppProvider } from '../../app.provider';
import { TestAppProvider } from '../../__tests__/test-app.provider';
import { TestCacheModule } from '../../datasources/cache/__tests__/test.cache.module';
import { NetworkResponseError } from '../../datasources/network/entities/network.error.entity';
import { TestNetworkModule } from '../../datasources/network/__tests__/test.network.module';
Expand Down
2 changes: 1 addition & 1 deletion src/routes/collectibles/collectibles.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
pageBuilder,
} from '../../domain/entities/__tests__/page.builder';
import { PaginationData } from '../common/pagination/pagination.data';
import { TestAppProvider } from '../../app.provider';
import { TestAppProvider } from '../../__tests__/test-app.provider';
import { ValidationModule } from '../../validation/validation.module';
import { TestLoggingModule } from '../../logging/__tests__/test.logging.module';
import { ConfigurationModule } from '../../config/configuration.module';
Expand Down
2 changes: 1 addition & 1 deletion src/routes/contracts/__tests__/get-contract.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { INestApplication } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import { AppModule } from '../../../app.module';
import { redisClientFactory } from '../../../__tests__/redis-client.factory';
import { TestAppProvider } from '../../../app.provider';
import { TestAppProvider } from '../../../__tests__/test-app.provider';

describe('Get contract e2e test', () => {
let app: INestApplication;
Expand Down
2 changes: 1 addition & 1 deletion src/routes/contracts/contracts.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { INestApplication } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import * as request from 'supertest';
import { TestAppProvider } from '../../app.provider';
import { TestAppProvider } from '../../__tests__/test-app.provider';
import { TestCacheModule } from '../../datasources/cache/__tests__/test.cache.module';
import { TestNetworkModule } from '../../datasources/network/__tests__/test.network.module';
import { DomainModule } from '../../domain.module';
Expand Down
2 changes: 1 addition & 1 deletion src/routes/data-decode/__tests__/data-decode.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Test } from '@nestjs/testing';
import { RedisClientType } from 'redis';
import * as request from 'supertest';
import { AppModule } from '../../../app.module';
import { TestAppProvider } from '../../../app.provider';
import { TestAppProvider } from '../../../__tests__/test-app.provider';
import { DataDecoded } from '../../../domain/data-decoder/entities/data-decoded.entity';
import { redisClientFactory } from '../../../__tests__/redis-client.factory';
import { getDataDecodedDtoBuilder } from '../entities/__tests__/get-data-decoded.dto.builder';
Expand Down
2 changes: 1 addition & 1 deletion src/routes/delegates/delegates.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { INestApplication } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { omit } from 'lodash';
import * as request from 'supertest';
import { TestAppProvider } from '../../app.provider';
import { TestAppProvider } from '../../__tests__/test-app.provider';
import { TestCacheModule } from '../../datasources/cache/__tests__/test.cache.module';
import { TestNetworkModule } from '../../datasources/network/__tests__/test.network.module';
import { DomainModule } from '../../domain.module';
Expand Down
2 changes: 1 addition & 1 deletion src/routes/estimations/estimations.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { INestApplication } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { omit } from 'lodash';
import * as request from 'supertest';
import { TestAppProvider } from '../../app.provider';
import { TestAppProvider } from '../../__tests__/test-app.provider';
import { TestCacheModule } from '../../datasources/cache/__tests__/test.cache.module';
import { TestNetworkModule } from '../../datasources/network/__tests__/test.network.module';
import { DomainModule } from '../../domain.module';
Expand Down
2 changes: 1 addition & 1 deletion src/routes/flush/flush.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { INestApplication } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import * as request from 'supertest';
import { TestAppProvider } from '../../app.provider';
import { TestAppProvider } from '../../__tests__/test-app.provider';
import { TestCacheModule } from '../../datasources/cache/__tests__/test.cache.module';
import { TestNetworkModule } from '../../datasources/network/__tests__/test.network.module';
import { DomainModule } from '../../domain.module';
Expand Down
2 changes: 1 addition & 1 deletion src/routes/health/__tests__/get-health.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { INestApplication } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import * as request from 'supertest';
import { AppModule } from '../../../app.module';
import { TestAppProvider } from '../../../app.provider';
import { TestAppProvider } from '../../../__tests__/test-app.provider';

describe('Get health e2e test', () => {
let app: INestApplication;
Expand Down
2 changes: 1 addition & 1 deletion src/routes/messages/messages.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { INestApplication } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { random, range } from 'lodash';
import * as request from 'supertest';
import { TestAppProvider } from '../../app.provider';
import { TestAppProvider } from '../../__tests__/test-app.provider';
import { TestCacheModule } from '../../datasources/cache/__tests__/test.cache.module';
import { TestNetworkModule } from '../../datasources/network/__tests__/test.network.module';
import { DomainModule } from '../../domain.module';
Expand Down
2 changes: 1 addition & 1 deletion src/routes/notifications/notifications.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { INestApplication } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { range } from 'lodash';
import * as request from 'supertest';
import { TestAppProvider } from '../../app.provider';
import { TestAppProvider } from '../../__tests__/test-app.provider';
import { TestCacheModule } from '../../datasources/cache/__tests__/test.cache.module';
import { NetworkResponseError } from '../../datasources/network/entities/network.error.entity';
import { TestNetworkModule } from '../../datasources/network/__tests__/test.network.module';
Expand Down
2 changes: 1 addition & 1 deletion src/routes/owners/owners.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { faker } from '@faker-js/faker';
import { INestApplication } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import * as request from 'supertest';
import { TestAppProvider } from '../../app.provider';
import { TestAppProvider } from '../../__tests__/test-app.provider';
import { TestCacheModule } from '../../datasources/cache/__tests__/test.cache.module';
import { TestNetworkModule } from '../../datasources/network/__tests__/test.network.module';
import { DomainModule } from '../../domain.module';
Expand Down
2 changes: 1 addition & 1 deletion src/routes/safe-apps/__tests__/get-safe-apps.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Test } from '@nestjs/testing';
import { RedisClientType } from 'redis';
import * as request from 'supertest';
import { AppModule } from '../../../app.module';
import { TestAppProvider } from '../../../app.provider';
import { TestAppProvider } from '../../../__tests__/test-app.provider';
import { redisClientFactory } from '../../../__tests__/redis-client.factory';

describe('Get Safe Apps e2e test', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/routes/safe-apps/safe-apps.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { faker } from '@faker-js/faker';
import { INestApplication } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import * as request from 'supertest';
import { TestAppProvider } from '../../app.provider';
import { TestAppProvider } from '../../__tests__/test-app.provider';
import { TestCacheModule } from '../../datasources/cache/__tests__/test.cache.module';
import { TestNetworkModule } from '../../datasources/network/__tests__/test.network.module';
import { DomainModule } from '../../domain.module';
Expand Down
2 changes: 1 addition & 1 deletion src/routes/safes/safes.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
moduleTransactionBuilder,
toJson as moduleTransactionToJson,
} from '../../domain/safe/entities/__tests__/module-transaction.builder';
import { TestAppProvider } from '../../app.provider';
import { TestAppProvider } from '../../__tests__/test-app.provider';
import { ValidationModule } from '../../validation/validation.module';
import { TestLoggingModule } from '../../logging/__tests__/test.logging.module';
import { ConfigurationModule } from '../../config/configuration.module';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { faker } from '@faker-js/faker';
import { INestApplication } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import * as request from 'supertest';
import { TestAppProvider } from '../../../../app.provider';
import { TestAppProvider } from '../../../../__tests__/test-app.provider';
import { TestCacheModule } from '../../../../datasources/cache/__tests__/test.cache.module';
import { TestNetworkModule } from '../../../../datasources/network/__tests__/test.network.module';
import { DomainModule } from '../../../../domain.module';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { faker } from '@faker-js/faker';
import { INestApplication } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import * as request from 'supertest';
import { TestAppProvider } from '../../../../app.provider';
import { TestAppProvider } from '../../../../__tests__/test-app.provider';
import { TestCacheModule } from '../../../../datasources/cache/__tests__/test.cache.module';
import { TestNetworkModule } from '../../../../datasources/network/__tests__/test.network.module';
import { DomainModule } from '../../../../domain.module';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { faker } from '@faker-js/faker';
import { INestApplication } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import * as request from 'supertest';
import { TestAppProvider } from '../../../../app.provider';
import { TestAppProvider } from '../../../../__tests__/test-app.provider';
import { ConfigurationModule } from '../../../../config/configuration.module';
import { IConfigurationService } from '../../../../config/configuration.service.interface';
import configuration from '../../../../config/entities/__tests__/configuration';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { faker } from '@faker-js/faker';
import { INestApplication } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import * as request from 'supertest';
import { TestAppProvider } from '../../../../app.provider';
import { TestAppProvider } from '../../../../__tests__/test-app.provider';
import { TestCacheModule } from '../../../../datasources/cache/__tests__/test.cache.module';
import { TestNetworkModule } from '../../../../datasources/network/__tests__/test.network.module';
import { DomainModule } from '../../../../domain.module';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { faker } from '@faker-js/faker';
import { INestApplication } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import * as request from 'supertest';
import { TestAppProvider } from '../../../../app.provider';
import { TestAppProvider } from '../../../../__tests__/test-app.provider';
import { ConfigurationModule } from '../../../../config/configuration.module';
import { IConfigurationService } from '../../../../config/configuration.service.interface';
import configuration from '../../../../config/entities/__tests__/configuration';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { faker } from '@faker-js/faker';
import { INestApplication } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import * as request from 'supertest';
import { TestAppProvider } from '../../../../app.provider';
import { TestAppProvider } from '../../../../__tests__/test-app.provider';
import { TestCacheModule } from '../../../../datasources/cache/__tests__/test.cache.module';
import { TestNetworkModule } from '../../../../datasources/network/__tests__/test.network.module';
import { DomainModule } from '../../../../domain.module';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { faker } from '@faker-js/faker';
import { INestApplication } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import * as request from 'supertest';
import { TestAppProvider } from '../../../../app.provider';
import { TestAppProvider } from '../../../../__tests__/test-app.provider';
import { TestCacheModule } from '../../../../datasources/cache/__tests__/test.cache.module';
import { TestNetworkModule } from '../../../../datasources/network/__tests__/test.network.module';
import { DomainModule } from '../../../../domain.module';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { faker } from '@faker-js/faker';
import { INestApplication } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import * as request from 'supertest';
import { TestAppProvider } from '../../../../app.provider';
import { TestAppProvider } from '../../../../__tests__/test-app.provider';
import { TestCacheModule } from '../../../../datasources/cache/__tests__/test.cache.module';
import { TestNetworkModule } from '../../../../datasources/network/__tests__/test.network.module';
import { DomainModule } from '../../../../domain.module';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { faker } from '@faker-js/faker';
import { INestApplication } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import * as request from 'supertest';
import { TestAppProvider } from '../../app.provider';
import { TestAppProvider } from '../../__tests__/test-app.provider';
import { ConfigurationModule } from '../../config/configuration.module';
import { IConfigurationService } from '../../config/configuration.service.interface';
import configuration from '../../config/entities/__tests__/configuration';
Expand Down

0 comments on commit 95e1d8e

Please sign in to comment.