Currently, I am in the process of mocking the findAll function of my service. To achieve this, I have to decide whether to mock the repository function findAndCount
within myEntityRepository or the paginate
function of the nestjs-typeorm-paginate node module. The findAll
function is responsible for retrieving a list of records from a table and paginating them using the NodeModule nestjs-typeorm-paginate.
Attempt 1: Mocking myEntityRepository However, this attempt failed with the following error and traceback:
TypeError: queryBuilder.limit is not a function
at ../node_modules/nestjs-typeorm-paginate/dist/paginate.js:119:28
at ../node_modules/nestjs-typeorm-paginate/dist/paginate.js:8:71
at Object.<anonymous>.__awaiter (../node_modules/nestjs-typeorm-paginate/dist/paginate.js:4:12)
at paginateQueryBuilder (../node_modules/nestjs-typeorm-paginate/dist/paginate.js:115:12)
at Object.<anonymous> (../node_modules/nestjs-typeorm-paginate/dist/paginate.js:22:15)
at ../node_modules/nestjs-typeorm-paginate/dist/paginate.js:8:71
my.service.ts
import { IPaginationOptions, paginate, Pagination } from 'nestjs-typeorm-paginate'
export class MyService {
constructor(@InjectRepository(MyEntity) private myEntityRepository: Repository<MyEntity>) { }
async findAll(options: IPaginationOptions): Promise<Pagination<MyEntity>> {
try {
return await paginate<MyEntity>(this.myEntityRepository, options)
} catch (error) {
throw error
}
}
}
my.service.spec.ts
describe('MyService Basic GET findAll test cases', () => {
let service: MyService
let repositoryMock: MockType<Repository<MyEntity>>
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [MyService,
{
provide: getRepositoryToken(MyEntity), useFactory: repositoryMockFactory
}
],
}).compile()
service = module.get<MyService>(MyService)
repositoryMock = module.get(getRepositoryToken(MyEntity))
const itemList = [{
id: 1,
my_field: 'a1',
}, {
id: 2,
my_field: 'a2',
}, ]
it('should findAll() the MyEntity', async () => {
expect((await service.findAll(options)).items.length).toBe(itemsList.length)
})
})
const repositoryMockFactory: () => MockType<Repository<MyEntity>> = jest.fn(() => ({
find: jest.fn(entity => entity),
findAndCount: jest.fn(entity => entity),
}))
Attempt 2: Mocking paginate I then attempted to mock the paginate method, but encountered another error:
TypeError: Cannot redefine property: paginate at Function.defineProperty (<anonymous>)
my.service.spec.ts with Pagination mock changes)
import * as nestjsTypeormPaginate from 'nestjs-typeorm-paginate' // imported at top
....
....
it('should findAll() the MyEntity', async () => {
const queryDto: QueryMyEntityDto = { customerId: 1 }
const options: IPaginationOptions = { page: 1, limit: 10 }
let paginationMock = jest.spyOn(nestjsTypeormPaginate, 'paginate')
paginationMock.mockImplementation((dto, options) => Promise.resolve({
items: itemList.slice(0, 2),
meta: {
itemCount: 2,
totalItems: 2,
totalPages: 1,
currentPage: 1,
}
}))
repositoryMock.find.mockReturnValue(itemList)
expect((await service.findAll(options)).items.length).toBe(itemsList.length)
})
...
Before posting this question: I explored the following resources:
- Utilizing `jest.spyOn` on an exported function from a Node module
- How to mock an imported named function in Jest when the module is unmocked
- https://github.com/nestjsx/nestjs-typeorm-paginate/issues/143