Jest / 单元测试在返回异步方法的函数上失败

时间:2021-01-27 11:38:34

标签: express testing jestjs

我正在使用 TypeORM。
它在本地运行良好,具有真实的数据库连接。

但是当我用 Jest 运行测试时,测试失败了。
我模拟了我需要的 TypeORM 方法,并期望调用所有这些方法。

测试在 expect(typeorm.getRepository(Covid).save).toHaveBeenCalled();
上失败 它没有被调用。

我不明白为什么这个测试代码没有调用。

有人知道解决办法吗?

这是代码。

describe('getCoronaData', () => {
  it('should get a Covid Status data and return it', async () => {
    typeorm.getRepository = jest.fn().mockReturnValue({
      findOne: jest.fn().mockResolvedValue(null),
      create: jest.fn(),
      save: jest.fn(),
    });

    await getCoronaData();

    expect(typeorm.getRepository).toHaveBeenCalledWith(Covid);
    expect(typeorm.getRepository(Covid).findOne).toHaveBeenCalled();
    expect(typeorm.getRepository(Covid).save).toHaveBeenCalled(); // this expect result is making an error
                                                                  // Expected number of calls: >= 1
                                                                  // Received number of calls:    0
  });
});

export const getCoronaData = async () => {
  try {
    // Just a fake data
    const covidStatus = [
      {
        city: 'city',
        totalCases: '100',
        increasedCases: '100',
        date: 'time',
      },
    ];
    const covidRepository = getRepository(Covid);
    covidStatus.forEach(async (status) => {
      const exist = await covidRepository.findOne({
        where: { city: status.city },
      });
      if (!exist) {
        // expecting save() method at this part to be called, but it fails
        return await covidRepository.save(
          covidRepository.create({
            city: status.city,
            totalCases: status.totalCases,
            increasedCases: status.increasedCases,
            date: status.time,
          })
        );
      } else {
        return await covidRepository.save([
          {
            id: exist.id,
            totalCases: status.totalCases,
            increasedCases: status.increasedCases,
            date: status.time,
          },
        ]);
      }
    });
  } catch (err) {
    console.error(err);
    return;
  }
};

1 个答案:

答案 0 :(得分:0)

自我回答

我发现了测试失败的问题。 forEach() 方法不应与 async/await 一起使用。
所以我只是使用 Promise.all()map()

更改了代码
await Promise.all(
      covidStatus.map(async (status) => {
        const exist = await covidRepository.findOne({
          where: { city: status.city },
        });
        if (!exist) {
          const newData = covidRepository.create({
            city: status.city,
            totalCases: status.totalCases,
            increasedCases: status.increasedCases,
            date: $standardTime,
          });
          return covidRepository.save(newData);
        } else {
          return covidRepository.save([
            {
              id: exist.id,
              totalCases: status.totalCases,
              increasedCases: status.increasedCases,
              date: $standardTime,
            },
          ]);
        }
      })
    );

    for (const status of covidStatus) {
      const exist = await covidRepository.findOne({
        where: { city: status.city },
      });
      if (!exist) {
        const newData = covidRepository.create({
          city: status.city,
          totalCases: status.totalCases,
          increasedCases: status.increasedCases,
          date: $standardTime,
        });
        return covidRepository.save(newData);
      } else {
        return covidRepository.save([
          {
            id: exist.id,
            totalCases: status.totalCases,
            increasedCases: status.increasedCases,
            date: $standardTime,
          },
        ]);
      }
    }

我从这里找到了这个解决方案。

using forEach and async/await, behaves different for node and Jest

相关问题