Jest pass in catch and finally block, but doesn't assert the expects inside them

Viewed 222

I'm testing a typescript function where is done a typeorm transaction:

budgetSave.ts

export async function save(budget: Budget, connection: Connection) : Promise<Budget> {
    const queryRunner = connection.createQueryRunner();
    await queryRunner.startTransaction();
    try {
        await queryRunner.manager.softDelete(Budget, {code: budget.code });
        budget = await queryRunner.manager.save(budget);
        await queryRunner.commitTransaction();
    } catch (e) {
        console.error(`Error: + ${e.message}`);
        await queryRunner.rollbackTransaction();
        throw e;
    } finally {
        await queryRunner.release();
    }
    return budget;
}

budgetSave.spec.ts

import { Connection, EntityManager, QueryRunner } from "typeorm";
import { save, handle } from "services/budgetSave";
import { Budget } from "libs/entities/budget";

describe('test should handle price', () => {
    it('should rollback when throws an error', async () => {
      console.error = jest.fn();

      const budget = {
        id: 1,
        code: 'B-123',
        email: 'test@example.com',
        value: 990.90,
        active: true,
        created: Date.now
      } as Budget;
      const errorMessage = 'Error to write in database';
  
      const entityManager: EntityManager = {
        softDelete: jest.fn().mockRejectedValueOnce(new Error(errorMessage))
      } as unknown as EntityManager;
  
      const queryRunner: QueryRunner = {
        startTransaction: jest.fn().mockImplementationOnce(async() => Promise.resolve()),
        rollbackTransaction: jest.fn().mockImplementationOnce(async() => Promise.resolve()),
        release: jest.fn().mockImplementationOnce(async() => Promise.resolve()),
        manager: entityManager
      } as unknown as QueryRunner;
  
      const mockedConnection = {
        createQueryRunner: jest.fn().mockImplementation(() => queryRunner)
      } as unknown as Connection;
  
      expect(async () => {
        await save(budget, mockedConnection);
      }).rejects.toThrow(errorMessage);

      expect(console.error).toBeCalledWith('Error: Error to write in database');
      expect(queryRunner.rollbackTransaction).toBeCalled();
      expect(queryRunner.release).toBeCalled();
  
    });
  });

When I'm testing the error case, and I try assert using expect to be called methods and derivations inside the catch and finally blocks, I found the following error:

● test should handle price › should rollback when throws an error

    expect(jest.fn()).toBeCalledWith(...expected)

    Expected: "Error: Error to write in database"

    Number of calls: 0

      41 |       }).rejects.toThrow(errorMessage);
      42 |
    > 43 |       expect(console.error).toBeCalledWith('Error: Error to write in database');
         |                             ^
      44 |       expect(queryRunner.rollbackTransaction).toBeCalled();
      45 |       expect(queryRunner.release).toBeCalled();
      46 |   

      at Object.<anonymous> (tests/services/budgetSave..spec.ts:43:29)

To run the tests I used the following line:

$ yarn jest --coverage

When I comment that line (43), I have an error that the next line won't called. The same happens with the expect at line 45. If I comment all these expectations the test pass and enter in these blocks as it's possible to see in this capture: Coverage test pass by catch and finally block

I'm wondering what's wrong in these expectations that won't called even the coverage report indicating that pass by there.

0 Answers
Related