From b51ba0a27c852f967b922130d01ac7cf2ec11d64 Mon Sep 17 00:00:00 2001 From: Paulo Silva Date: Wed, 9 Oct 2024 17:36:33 -0700 Subject: [PATCH] fix flaky test with timestamp (#195681) ## Summary It fixes the flaky test raised on #195634 by adding the possibility to pass the timestamp to the function. That helps to eliminate flakiness, by passing the same `currentTimestamp` to both the test and the function. Also, it's a simpler approach that doesn't require mocking global objects or using Jest's fake timers, keeping your test straightforward and easy to understand. --- .../create_detection_rule_from_vulnerability.test.ts | 2 +- .../utils/create_detection_rule_from_vulnerability.ts | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/x-pack/plugins/cloud_security_posture/public/pages/vulnerabilities/utils/create_detection_rule_from_vulnerability.test.ts b/x-pack/plugins/cloud_security_posture/public/pages/vulnerabilities/utils/create_detection_rule_from_vulnerability.test.ts index 209ec81168271..7dd0982cc58b5 100644 --- a/x-pack/plugins/cloud_security_posture/public/pages/vulnerabilities/utils/create_detection_rule_from_vulnerability.test.ts +++ b/x-pack/plugins/cloud_security_posture/public/pages/vulnerabilities/utils/create_detection_rule_from_vulnerability.test.ts @@ -89,7 +89,7 @@ describe('CreateDetectionRuleFromVulnerability', () => { } as Vulnerability; const currentTimestamp = new Date().toISOString(); - const query = generateVulnerabilitiesRuleQuery(mockVulnerability); + const query = generateVulnerabilitiesRuleQuery(mockVulnerability, currentTimestamp); expect(query).toEqual( `vulnerability.id: "CVE-2024-00005" AND event.ingested >= "${currentTimestamp}"` ); diff --git a/x-pack/plugins/cloud_security_posture/public/pages/vulnerabilities/utils/create_detection_rule_from_vulnerability.ts b/x-pack/plugins/cloud_security_posture/public/pages/vulnerabilities/utils/create_detection_rule_from_vulnerability.ts index b723c60f9ee3d..804e89fad61d8 100644 --- a/x-pack/plugins/cloud_security_posture/public/pages/vulnerabilities/utils/create_detection_rule_from_vulnerability.ts +++ b/x-pack/plugins/cloud_security_posture/public/pages/vulnerabilities/utils/create_detection_rule_from_vulnerability.ts @@ -53,10 +53,11 @@ export const getVulnerabilityRuleName = (vulnerability: Vulnerability) => { }); }; -export const generateVulnerabilitiesRuleQuery = (vulnerability: Vulnerability) => { - const currentTimestamp = new Date().toISOString(); - - return `vulnerability.id: "${vulnerability.id}" AND event.ingested >= "${currentTimestamp}"`; +export const generateVulnerabilitiesRuleQuery = ( + vulnerability: Vulnerability, + startTimestamp = new Date().toISOString() +) => { + return `vulnerability.id: "${vulnerability.id}" AND event.ingested >= "${startTimestamp}"`; }; const CSP_RULE_TAG = 'Cloud Security';