I have found some weird issues while running tests in our CI. The tests are flaky even tho we are mocking our function to return a fixed date. Running the same code returns a different value for millisecondsSinceEpoch
Here you can see the bit of code that causes the issue:
String fileName = "${event.gymName.replaceAll(RegExp(r'\s'), "-")}-${dateUtil.getCurrentDateTime().millisecondsSinceEpoch}.png";
String gymImage = await storageRepository.uploadImage(croppedFile, fileName);
and this is the test that fail in the CI but not in my local (only relevant bit):
group("on UploadGymImage event", () {
final testDate = DateTime(2021, 1, 11);
var newImageUrl = "https://gym.img/abc.png";
setUp(() {
when(mockStorageRepository.uploadImage(fakeImage, argThat(endsWith(".png"))))
.thenAnswer((_) => Future.value(newImageUrl));
when(mockDateUtil.getCurrentDateTime()).thenReturn(testDate);
});
tearDown(() async {
verify(mockStorageRepository.uploadImage(
fakeImage,
"a-gym-1610319600000.png",
));
verify(mockDateUtil.getCurrentDateTime());
});
});
The documentation for millisecondsSinceEpoch says:
/// The number of milliseconds since
/// the "Unix epoch" 1970-01-01T00:00:00Z (UTC).
///
/// This value is independent of the time zone.
///
/// This value is at most
/// 8,640,000,000,000,000ms (100,000,000 days) from the Unix epoch.
/// In other words: `millisecondsSinceEpoch.abs() <= 8640000000000000`.
So I thought that by returning a fixed date in my mock, there would not be an issue running this test anywhere.
Can someone explain to me why is so?
note: the CI returns 1610323200000 my local instead 1610319600000