fix(backend): isolate health service unit test
All checks were successful
CI / ci (push) Successful in 15m16s
All checks were successful
CI / ci (push) Successful in 15m16s
This commit is contained in:
parent
bbbdca30f6
commit
8dab915edd
@ -1,27 +1,43 @@
|
|||||||
import { Test, TestingModule } from '@nestjs/testing';
|
import { Test, TestingModule } from '@nestjs/testing';
|
||||||
import { ConfigModule } from '@nestjs/config';
|
import { ConfigService } from '@nestjs/config';
|
||||||
import { HealthService } from './health.service';
|
import { HealthService } from './health.service';
|
||||||
import { PrismaService } from '../prisma/prisma.service';
|
import { PrismaService } from '../prisma/prisma.service';
|
||||||
import configuration from '../../config/configuration';
|
|
||||||
|
|
||||||
describe('HealthService', () => {
|
describe('HealthService', () => {
|
||||||
let service: HealthService;
|
let service: HealthService;
|
||||||
const prisma = { $queryRaw: vi.fn() } as any;
|
const prisma = { $queryRaw: vi.fn() } as any;
|
||||||
|
const config = {
|
||||||
|
get: vi.fn((key: string, fallback?: unknown) => {
|
||||||
|
const values: Record<string, unknown> = {
|
||||||
|
'app.moex.baseUrl': 'https://iss.moex.test/iss',
|
||||||
|
'app.tbank.token': 'token-1',
|
||||||
|
};
|
||||||
|
|
||||||
|
return values[key] ?? fallback;
|
||||||
|
}),
|
||||||
|
} as unknown as ConfigService;
|
||||||
|
const fetchMock = vi.fn();
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
vi.clearAllMocks();
|
vi.clearAllMocks();
|
||||||
|
vi.stubGlobal('fetch', fetchMock);
|
||||||
|
fetchMock.mockResolvedValue({ ok: true, status: 200 });
|
||||||
|
|
||||||
const module: TestingModule = await Test.createTestingModule({
|
const module: TestingModule = await Test.createTestingModule({
|
||||||
imports: [ConfigModule.forRoot({ load: [configuration], isGlobal: true })],
|
|
||||||
providers: [
|
providers: [
|
||||||
HealthService,
|
HealthService,
|
||||||
{ provide: PrismaService, useValue: prisma },
|
{ provide: PrismaService, useValue: prisma },
|
||||||
|
{ provide: ConfigService, useValue: config },
|
||||||
],
|
],
|
||||||
}).compile();
|
}).compile();
|
||||||
|
|
||||||
service = module.get<HealthService>(HealthService);
|
service = module.get<HealthService>(HealthService);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
vi.unstubAllGlobals();
|
||||||
|
});
|
||||||
|
|
||||||
it('returns ok when all dependencies are healthy', async () => {
|
it('returns ok when all dependencies are healthy', async () => {
|
||||||
prisma.$queryRaw.mockResolvedValue([{ 1: 1 }]);
|
prisma.$queryRaw.mockResolvedValue([{ 1: 1 }]);
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user