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

422
Views
¿Es correcta mi prueba unitaria para el proyecto Nest.js?

Escribí una prueba unitaria para mi proyecto Nest.js. Solo quiero confirmar que esta prueba es correcta.

Estoy probando el archivo de servicio de nido. A continuación se muestra el código:

tareas.servicio.ts

 async getTaskById(id: string): Promise<Task> { const found = await this.taskModel.findById(id); if (!found) { throw new NotFoundException(`Task not found`); } return found; }

tareas.servicio.spec.ts

 const mockTask = () => { return { _id: '613e4135ea46be481c2d88b2', name: 'Task 1', description: 'Go to school', }; }; const tasksServiceMock: Partial<TasksService> = { getTaskById: jest.fn().mockResolvedValue(mockTask()), }; describe('TasksService', () => { let service: TasksService; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ providers: [ TasksService, { provide: TasksService, useValue: tasksServiceMock, }, ], }).compile(); service = module.get<TasksService>(TasksService); }); it('should be defined', () => { expect(service).toBeDefined(); }); describe('getTaskById', () => { it('should get task by ID', async () => { const task = await service.getTaskById(mockTask()._id); expect(task).toEqual(mockTask()); }); it('should throw task Not found error', async () => { tasksServiceMock.getTaskById = jest .fn() .mockRejectedValue(new NotFoundException('Task not found')); expect.assertions(2); try { await service.getTaskById('123456'); } catch (e) { expect(e).toBeInstanceOf(NotFoundException); expect(e.message).toBe('Task not found'); } }); }); });
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Estás cubriendo las pruebas unitarias correctamente. Tengo 2 sugerencias para la sintaxis:

  1. Puede definir mockTask directamente como un objeto en lugar de asignar la función de flecha
  2. Para que el caso de prueba verifique el escenario de error, puede usar una sintaxis más limpia ( https://jestjs.io/docs/asynchronous#asyncawait ) en lugar de try catch
 it("should throw task Not found error", async () => { const mockError = new NotFoundException("Task not found"); tasksServiceMock.getTaskById = jest.fn().mockRejectedValue(mockError); expect.assertions(2); await expect(service.getTaskById("123456")).rejects.toThrowError(mockError); });
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!