jestjs - Meaningful unit tests for TypeORM queries - Stack Overflow

Using TypeORM, I created some rather important queries with key functionality. Example: Only entities t

Using TypeORM, I created some rather important queries with key functionality. Example: Only entities that match the anisationId may be returned.

// Typescript TypeORM code
const norm = await this.normRepository.findOne({
  where: {
    id: normId,
    chapter: {
      framework: {
        anisationId: this.userServiceanisationId,
      },
    },
  },
});

// Unit test
expect(normRepository.findOne).toHaveBeenCalledWith({
  // The same query that I inserted in the code above
});

To me this test does not really test any query logic.

Coming from a .NET EF Core background it would look like this:

// C# EF Core code
var norm = await _dbContext.Norms.FirstAsync(n => 
  n.Id == normId 
  && n.Chapter.Framework.OrganisationId == _userServiceanisationId);

// Unit test
_dbContext.Setup(s => s.Norms).ReturnsDbSet( new List<Norm> {
  // An example norm object, with connected chapter and framework
});

var result = await service.example();

Assert.NotNull(result) // Assert something that the method does.

In the EF Core unit test, some data is prepared on which the logic of the query actually runs.

Is there a way for TypeORM to test the query itself a bit better? What I foresee happening is that I update the query mistakenly, and then just copy paste the same query into the unit test.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742349369a4427198.html

相关推荐

  • jestjs - Meaningful unit tests for TypeORM queries - Stack Overflow

    Using TypeORM, I created some rather important queries with key functionality. Example: Only entities t

    2小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信