Skip to content

Commit

Permalink
fix flaky test with timestamp (elastic#195681)
Browse files Browse the repository at this point in the history
## Summary

It fixes the flaky test raised on elastic#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.

(cherry picked from commit b51ba0a)
  • Loading branch information
opauloh committed Oct 10, 2024
1 parent 0035e94 commit c562c72
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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}"`
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit c562c72

Please sign in to comment.