diff --git a/changelog/dev-fix-unstable-test-WC_Payments_Incentives_Service_Test b/changelog/dev-fix-unstable-test-WC_Payments_Incentives_Service_Test new file mode 100644 index 00000000000..49b420d9920 --- /dev/null +++ b/changelog/dev-fix-unstable-test-WC_Payments_Incentives_Service_Test @@ -0,0 +1,5 @@ +Significance: patch +Type: dev +Comment: Fix the flaky tests due to using PHP time() in production vs test. + + diff --git a/tests/unit/test-class-wc-payments-incentives-service.php b/tests/unit/test-class-wc-payments-incentives-service.php index 9c8921289ab..9b12a949853 100644 --- a/tests/unit/test-class-wc-payments-incentives-service.php +++ b/tests/unit/test-class-wc-payments-incentives-service.php @@ -406,7 +406,9 @@ function ( $value, $expiration ) { $this->assertSame( 'yes', $value ); // Ensure the cache is set to expire in 90 days - 30 days = 60 days. - $this->assertSame( 60 * DAY_IN_SECONDS, $expiration ); + $expected_expiration = 60 * DAY_IN_SECONDS; + // Allowing 5-second difference to avoid flaky tests due to time() precision. + $this->assertLessThanOrEqual( 5, abs( $expected_expiration - $expiration ), 'Expiration time should be within 5 second of expected value' ); return $value; },