Skip to content

Commit b4c8ba7

Browse files
authored
[EDR Workflows] Speed up MS Defender runscript Jest tests by mocking p-retry (#268311)
## Summary Two tests in `ms_defender_endpoint_actions_client.test.ts` (`should throw error when GET_ACTIONS returns no action details after retry` and `should throw error when GET_ACTIONS call fails`) intermittently exceeded Jest's 5s default. Both exercise the production retry path in `fetchAndValidateRunscriptActionDetails`, which uses `pRetry` with `retries: 5, minTimeout: 300, maxTimeout: 1500, factor: 1.5` — ~4s of real-time sleep before the outer catch returns `Action details not found`. This PR mocks `p-retry` at the file level (delegating to the real implementation by default), and short-circuits it with `mockImplementationOnce(async (fn) => fn(1))` in the two slow tests. Mirrors the existing pattern in `license_watch.test.ts` and `reference_data/helpers.test.ts`. The two `// TODO: Fix this slow test` markers are removed.
1 parent ce27714 commit b4c8ba7

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

x-pack/solutions/security/plugins/security_solution/server/endpoint/services/actions/clients/microsoft/defender/endpoint/ms_defender_endpoint_actions_client.test.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
import { Readable } from 'stream';
9+
import pRetry from 'p-retry';
910
import { MicrosoftDefenderEndpointActionsClient } from './ms_defender_endpoint_actions_client';
1011
import type { ProcessPendingActionsMethodOptions, ResponseActionsClient } from '../../../../..';
1112
import { getActionDetailsById as _getActionDetailsById } from '../../../../action_details_by_id';
@@ -46,7 +47,13 @@ jest.mock('../../../../action_details_by_id', () => {
4647
};
4748
});
4849

50+
jest.mock('p-retry', () => {
51+
const originalPRetry = jest.requireActual('p-retry');
52+
return jest.fn().mockImplementation((fn, options) => originalPRetry(fn, options));
53+
});
54+
4955
const getActionDetailsByIdMock = _getActionDetailsById as jest.Mock;
56+
const pRetryMock = jest.mocked(pRetry);
5057

5158
describe('MS Defender response actions client', () => {
5259
let clientConstructorOptionsMock: MicrosoftDefenderActionsClientOptionsMock;
@@ -603,7 +610,6 @@ describe('MS Defender response actions client', () => {
603610
});
604611
});
605612

606-
// TODO: Fix this slow test
607613
it('should throw error when GET_ACTIONS returns no action details after retry', async () => {
608614
const underlyingClient = (
609615
connectorActionsMock as unknown as { connectorsClient: { execute: jest.Mock } }
@@ -630,6 +636,8 @@ describe('MS Defender response actions client', () => {
630636
}
631637
);
632638

639+
pRetryMock.mockImplementationOnce(async (fn) => fn(1));
640+
633641
await expect(
634642
msClientMock.runscript(
635643
responseActionsClientMock.createRunScriptOptions({
@@ -642,7 +650,6 @@ describe('MS Defender response actions client', () => {
642650
});
643651
});
644652

645-
// TODO: Fix this slow test
646653
it('should throw error when GET_ACTIONS call fails', async () => {
647654
const underlyingClient = (
648655
connectorActionsMock as unknown as { connectorsClient: { execute: jest.Mock } }
@@ -664,6 +671,8 @@ describe('MS Defender response actions client', () => {
664671
}
665672
);
666673

674+
pRetryMock.mockImplementationOnce(async (fn) => fn(1));
675+
667676
await expect(
668677
msClientMock.runscript(
669678
responseActionsClientMock.createRunScriptOptions({

0 commit comments

Comments
 (0)