import { Test, TestingModule } from '@nestjs/testing'; import { PipelinesService } from './pipelines.service'; import { Pipeline } from './pipeline.entity'; import { getRepositoryToken } from '@nestjs/typeorm'; import { getQueueToken } from '@nestjs/bull'; import { LIST_LOGS_TASK } from '../repos/repos.constants'; import { Repository } from 'typeorm'; import { Project } from '../projects/project.entity'; import { Job, Queue } from 'bull'; import { ListLogsOption } from '../repos/models/list-logs.options'; describe('PipelinesService', () => { let service: PipelinesService; let repository: Repository; let pipeline: Pipeline; let queue: Queue; beforeEach(async () => { pipeline = Object.assign(new Pipeline(), { id: 'test-pipeline', name: 'pipeline', branch: 'master', projectId: 'test-project', project: Object.assign(new Project(), { id: 'test-project', name: 'project', } as Project), } as Pipeline); const module: TestingModule = await Test.createTestingModule({ providers: [ PipelinesService, { provide: getRepositoryToken(Pipeline), useValue: { findOneOrFail: jest.fn().mockImplementation(() => pipeline), }, }, { provide: getQueueToken(LIST_LOGS_TASK), useValue: { add: jest.fn().mockImplementation(() => ({ id: 1 } as Job)), }, }, ], }).compile(); service = module.get(PipelinesService); repository = module.get(getRepositoryToken(Pipeline)); queue = module.get(getQueueToken(LIST_LOGS_TASK)); }); it('should be defined', () => { expect(service).toBeDefined(); }); describe('listLogsForPipeline', () => { it('should send task to queue.', async () => { const add = jest.spyOn(queue, 'add'); await expect( service.listLogsForPipeline('test-pipeline'), ).resolves.toEqual({ id: 1 }); expect(add).toBeCalledWith({ project: pipeline.project, branch: pipeline.branch, }); }); }); });