Back

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


1

116

  • No matching results...
  • Searching...

/ 1000

Poll Options

Cool.

One extra practice I’ve found useful is using CarbonImmutable in tests and domain code.

1

47

Yeah, nice.
When you use Date::now(), this can be simply configurable via provider, like Date::use(CarbonImmutable::class).

1

70

Ah, good call.

54