Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

244
Views
¿Cómo puedo probar la función llamada dentro de otra función en Nestjs?

estoy escribiendo funciones de prueba en nestjs y soy nuevo en esta tarea.

Principalmente tuve problemas con typeorm. pero mi problema actual es que no llamo a la función de paginación en la clase PaginateLib dentro de la función que escribí en el servicio. Probé la función userList directamente antes, pero dio un error de paginación indefinida. Ahora usé la función de simulación y paginación en la prueba. todavía da un error indefinido.

Aquí está mi código de admin.service

 public async userList(page, limit): Promise<any> { const opt = { relations: ['userInfo'], }; const userData = await this.paginateLib.paginate( getRepository(Users), opt, page, limit, ); return userData; }

Aquí está el código de la función Paginate en la clase PaginateLib. Estoy acortando esto porque no creo que sea muy necesario.

 export class PaginateLib { async paginate(repo, opt, page, limit) { try { page = Number(page); limit = Number(limit); const items = (Number(page) - 1) * Number(limit); const [data, count] = await repo.findAndCount({ ...opt, skip: Number(items), take: Number(limit), }); if (count <= items) { return []; }

y aquí está mi código de prueba

 class PaginateMock { paginate(repo: any, opt: any, page: number, limit: number) { return []; } } describe('AdminService', () => { let service: AdminService; let connection: Connection; let module: TestingModule; let services: PaginateLib; beforeAll(async () => { const ApiServiceProvider = { provide: PaginateLib, useClass: PaginateMock, }, module = await Test.createTestingModule({ imports: [ TypeOrmModule.forRoot({ type: '****', host: '****', port: ****, username: '****', password: '****', database: '****', entities: [__dirname + '/../../**/*.entity.ts'], synchronize: true, }), ], providers: [ AdminService, ApiServiceProvider, ], }).compile(); service = module.get<AdminService>(AdminService); services = module.get<PaginateLib>(PaginateLib); }); // afterAll(async () => { // await module.close(); // }); describe('User Info', () => { it('should be get user list', async () => { const paginateSpy = jest.spyOn(services, 'paginate'); expect(paginateSpy).toHaveBeenCalled(); expect(true).toBe(service.userList(1, 1)); }); }); });

Intenté cosas diferentes muchas veces. pero el error que obtengo es indefinido. ¿Qué tengo que hacer? ¿Alguien tiene una idea? Gracias.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Prueba esto:

 describe('User Info', () => { it('should be get user list', async () => { const paginateSpy = jest.spyOn(services, 'paginate'); const userData = await service.userList(1, 1); expect(paginateSpy).toHaveBeenCalled(); expect(userData).toBe(); // Don't know what userData actually looks like. So this is up to you }); });
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!