You can use mocks
export const mockTransactions: any = {
runQuery: jest.fn(),
runTransaction: jest.fn(),
entityManager: jest.fn(),
}
export const mockEntityManager: any = {
query: jest.fn(),
createQueryBuilder: jest.fn(),
}
export const mockUser = {
user_id: 2,
first_name: null,
last_name: null,
email: '[email protected]',
is_admin: true,
last_login: '2024-04-04T05:39:19.002Z',
is_active: true,
created_on: new Date(),
is_persist: null,
alert_on_delivery_error: null,
alert_on_order_expiration: null,
login_ip: null,
register_ip: null,
registered_on: '2024-02-12T07:39:26.192Z',
deleted: false,
deleted_on: null,
is_superuser: null,
is_reseller: null,
reseller_uuid: null
}
export const mockBullQueue: any = {
add: jest.fn(),
process: jest.fn(),
}
export const mockService = (name: string) => {
return {
provide: name,
useValue: {
create: jest.fn(),
find: jest.fn(),
findOne: jest.fn(),
remove: jest.fn(),
update: jest.fn(),
}
}
}
export const mockRepo = (name: string) => {
return {
provide: name,
useValue: {
create: jest.fn(),
findAll: jest.fn(),
findOneById: jest.fn(),
findByCondition: jest.fn(),
remove: jest.fn(),
update: jest.fn(),
findWithRelations: jest.fn(),
deleteWhere: jest.fn(),
}
}
}
test example
import { SmsService } from './sms.service';
import { Test } from '@nestjs/testing';
import { getQueueToken } from '@nestjs/bull';
import { DataSource, EntityManager } from 'typeorm';
import { Transactions } from 'src/config/ormconfig';
import { JwtModule } from '../jwt/jwt.module';
import { mockBullQueue, mockEntityManager, mockRepo, mockService, mockTransactions } from '../../../test.helpers';
const mockUuid = '82d47dbf-e798-4674-ae43-b7e1e09125d3';
const mockSms = {
uuid: '82d47dbf-e798-4674-ae43-b7e1e09125d3',
to: '1232123223',
from_id: '1234634221',
created_on: new Date(),
msg: 'string',
file: null,
code: 0,
user_id: null,
order_uuid: null,
app_uuid: null,
send_time: new Date(),
delivered_on: null,
user: null,
};
describe('Sms Service', () => {
let smsService: SmsService;
beforeEach(async () => {
const moduleRef = await Test.createTestingModule({
providers: [
SmsService,
{
provide: 'SmsRepositoryInterface',
useValue: {
create: jest.fn().mockResolvedValue(mockSms),
findAll: jest.fn().mockResolvedValue([mockSms, 1]),
findOneById: jest.fn().mockResolvedValue(mockSms),
findByCondition: jest.fn().mockResolvedValue(mockSms),
remove: jest.fn(),
update: jest.fn(),
findWithRelations: jest.fn(),
deleteWhere: jest.fn(),
},
},
{
provide: getQueueToken('job-queue'),
useValue: mockBullQueue,
},
{
provide: Transactions,
useValue: mockTransactions,
},
{
provide: EntityManager,
useValue: mockEntityManager
},
{
provide: DataSource,
useValue: {
options: {},
},
},
mockService('OrderNumberServiceInterface'),
mockRepo('OrderNumberRepositoryInterface'),
mockService('UserServiceInterface')
],
imports: [JwtModule],
}).compile();
smsService = moduleRef.get<SmsService>(SmsService);
});
it('Should be defined', () => {
expect(smsService).toBeDefined();
});
it('SmsService.find()', async () => {
const expectedOutput = [mockSms, 1];
const result = await smsService.find({ uuid: mockUuid });
expect(result).toEqual(expectedOutput);
});
it('SmsService.findOne()', async () => {
const result = await smsService.findOne({ uuid: mockUuid });
expect(result).toEqual(mockSms);
});
});