Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@shirish87
Created February 3, 2020 23:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shirish87/4bbe0c326f58e3e5b66a84614e6bfe7c to your computer and use it in GitHub Desktop.
Save shirish87/4bbe0c326f58e3e5b66a84614e6bfe7c to your computer and use it in GitHub Desktop.
import { Test, TestingModule } from '@nestjs/testing';
import { OrganisationTariffService } from './organisation-tariff.service';
import { getRepositoryToken } from '@nestjs/typeorm';
import { OrganisationTariff } from './organisation-tariff.entity';
import { Repository } from 'typeorm';
// @ts-ignore
export const repositoryMockFactory: () => MockType<Repository<OrganisationTariff>> = jest.fn(() => ({
manager: { connection: { close: () => Promise.resolve() } },
findOneOrFail: jest.fn(entity => entity),
}));
describe('OrganisationTariffService', () => {
let service: OrganisationTariffService;
// @ts-ignore
let repositoryMock: MockType<Repository<OrganisationTariff>>;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [OrganisationTariffService, {
provide: getRepositoryToken(OrganisationTariff),
useFactory: repositoryMockFactory,
}],
}).compile();
service = module.get<OrganisationTariffService>(OrganisationTariffService);
repositoryMock = module.get(getRepositoryToken(OrganisationTariff));
});
afterEach(async () => { await service.repository.manager.connection.close(); });
it('should be defined', async () => {
expect(service).toBeDefined();
const orgTariff = {id: 1, name: 'TimeOfUse' };
repositoryMock.findOneOrFail.mockReturnValue(orgTariff);
expect(await service.findById(orgTariff.id)).toEqual(orgTariff);
expect(repositoryMock.findOneOrFail).toHaveBeenCalledWith(orgTariff.id);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment