- Adiciona variáveis globais do Node.js (process, console, __dirname, require, module, exports) - Adiciona variáveis globais do Jest (describe, it, beforeEach, fail, etc.) - Configura ESLint para arquivos JavaScript de configuração - Remove diretivas eslint-disable não utilizadas - Corrige variáveis não usadas prefixando com _ - Ajusta regras do ESLint para ignorar variáveis que começam com _ - Formata código com Prettier
91 lines
2.5 KiB
TypeScript
91 lines
2.5 KiB
TypeScript
import { Test, TestingModule } from '@nestjs/testing';
|
|
import { Logger } from '@nestjs/common';
|
|
import { DataConsultService } from '../data-consult.service';
|
|
import { DataConsultRepository } from '../data-consult.repository';
|
|
import { IRedisClient } from '../../core/configs/cache/IRedisClient';
|
|
import { RedisClientToken } from '../../core/configs/cache/redis-client.adapter.provider';
|
|
import { DataSource } from 'typeorm';
|
|
import { DATA_SOURCE } from '../../core/constants';
|
|
|
|
export const createMockRepository = (
|
|
methods: Partial<DataConsultRepository> = {},
|
|
) =>
|
|
({
|
|
findStores: jest.fn(),
|
|
findSellers: jest.fn(),
|
|
findBillings: jest.fn(),
|
|
findCustomers: jest.fn(),
|
|
findProducts: jest.fn(),
|
|
findProductsByCodauxiliar: jest.fn(),
|
|
findAllProducts: jest.fn(),
|
|
findAllCarriers: jest.fn(),
|
|
findRegions: jest.fn(),
|
|
...methods,
|
|
} as any);
|
|
|
|
export const createMockRedisClient = () =>
|
|
({
|
|
get: jest.fn().mockResolvedValue(null),
|
|
set: jest.fn().mockResolvedValue(undefined),
|
|
} as any);
|
|
|
|
export interface DataConsultServiceTestContext {
|
|
service: DataConsultService;
|
|
mockRepository: jest.Mocked<DataConsultRepository>;
|
|
mockRedisClient: jest.Mocked<IRedisClient>;
|
|
mockDataSource: jest.Mocked<DataSource>;
|
|
mockLogger: {
|
|
error: jest.Mock;
|
|
};
|
|
}
|
|
|
|
export async function createDataConsultServiceTestModule(
|
|
repositoryMethods: Partial<DataConsultRepository> = {},
|
|
redisClientMethods: Partial<IRedisClient> = {},
|
|
): Promise<DataConsultServiceTestContext> {
|
|
const mockRepository = createMockRepository(repositoryMethods);
|
|
const mockRedisClient = {
|
|
...createMockRedisClient(),
|
|
...redisClientMethods,
|
|
} as any;
|
|
const mockDataSource = {} as any;
|
|
|
|
const module: TestingModule = await Test.createTestingModule({
|
|
providers: [
|
|
DataConsultService,
|
|
{
|
|
provide: DataConsultRepository,
|
|
useValue: mockRepository,
|
|
},
|
|
{
|
|
provide: RedisClientToken,
|
|
useValue: mockRedisClient,
|
|
},
|
|
{
|
|
provide: DATA_SOURCE,
|
|
useValue: mockDataSource,
|
|
},
|
|
],
|
|
}).compile();
|
|
|
|
const service = module.get<DataConsultService>(DataConsultService);
|
|
|
|
const mockLogger = {
|
|
error: jest.fn(),
|
|
};
|
|
|
|
jest
|
|
.spyOn(Logger.prototype, 'error')
|
|
.mockImplementation((message: any, ...optionalParams: any[]) => {
|
|
mockLogger.error(message, ...optionalParams);
|
|
});
|
|
|
|
return {
|
|
service,
|
|
mockRepository,
|
|
mockRedisClient,
|
|
mockDataSource,
|
|
mockLogger,
|
|
};
|
|
}
|