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.