Ever had a test pass locally but fail randomly in CI? That's what happened to me when testing timestamp fields.
The issue? Date::now() gets called twice - once in your code, once in your test - and those milliseconds add up.
The fix is simple: freeze time with Date::setTestNow() or $this->freezeTime() before making requests.
No more flaky tests, just reliable assertions.
dev.to/tegos/stop-flaky-tests-freeze-time-in-laravel-testing-1cnj
Back
•
Cool.
One extra practice I’ve found useful is using CarbonImmutable in tests and domain code.
•
In response to @raffi
Yeah, nice.
When you use Date::now(), this can be simply configurable via provider, like Date::use(CarbonImmutable::class).
•