Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Security Solution] Fixes flake test by increasing the timeout for the test from a default of 1 second. #117389

Merged
merged 1 commit into from
Nov 4, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
194 changes: 110 additions & 84 deletions packages/kbn-securitysolution-hook-utils/src/use_async/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ interface TestArgs {
type TestReturn = Promise<unknown>;

describe('useAsync', () => {
/**
* Timeout for both jest tests and for the waitForNextUpdate.
* jest tests default to 5 seconds and waitForNextUpdate defaults to 1 second.
* 20_0000 = 20,000 milliseconds = 20 seconds
*/
const timeout = 20_000;

let fn: jest.Mock<TestReturn, TestArgs[]>;
let args: TestArgs;

Expand All @@ -31,16 +38,20 @@ describe('useAsync', () => {
expect(fn).not.toHaveBeenCalled();
});

it('invokes the function when start is called', async () => {
const { result, waitForNextUpdate } = renderHook(() => useAsync(fn));
it(
'invokes the function when start is called',
async () => {
const { result, waitForNextUpdate } = renderHook(() => useAsync(fn));

act(() => {
result.current.start(args);
});
await waitForNextUpdate();
act(() => {
result.current.start(args);
});
await waitForNextUpdate({ timeout });

expect(fn).toHaveBeenCalled();
});
expect(fn).toHaveBeenCalled();
},
timeout
);

it('invokes the function with start args', async () => {
const { result, waitForNextUpdate } = renderHook(() => useAsync(fn));
Expand All @@ -49,84 +60,99 @@ describe('useAsync', () => {
act(() => {
result.current.start(args);
});
await waitForNextUpdate();
await waitForNextUpdate({ timeout });

expect(fn).toHaveBeenCalledWith(expectedArgs);
});

it('populates result with the resolved value of the fn', async () => {
const { result, waitForNextUpdate } = renderHook(() => useAsync(fn));
fn.mockResolvedValue({ resolved: 'value' });

act(() => {
result.current.start(args);
});
await waitForNextUpdate();

expect(result.current.result).toEqual({ resolved: 'value' });
expect(result.current.error).toBeUndefined();
});

it('populates error if function rejects', async () => {
fn.mockRejectedValue(new Error('whoops'));
const { result, waitForNextUpdate } = renderHook(() => useAsync(fn));

act(() => {
result.current.start(args);
});
await waitForNextUpdate();

expect(result.current.result).toBeUndefined();
expect(result.current.error).toEqual(new Error('whoops'));
});

it('populates the loading state while the function is pending', async () => {
let resolve: () => void;
fn.mockImplementation(() => new Promise<void>((_resolve) => (resolve = _resolve)));

const { result, waitForNextUpdate } = renderHook(() => useAsync(fn));

act(() => {
result.current.start(args);
});

expect(result.current.loading).toBe(true);

act(() => resolve());
await waitForNextUpdate();

expect(result.current.loading).toBe(false);
});

it('multiple start calls reset state', async () => {
let resolve: (result: string) => void;
fn.mockImplementation(() => new Promise((_resolve) => (resolve = _resolve)));

const { result, waitForNextUpdate } = renderHook(() => useAsync(fn));

act(() => {
result.current.start(args);
});

expect(result.current.loading).toBe(true);

act(() => resolve('result'));
await waitForNextUpdate();

expect(result.current.loading).toBe(false);
expect(result.current.result).toBe('result');

act(() => {
result.current.start(args);
});

expect(result.current.loading).toBe(true);
expect(result.current.result).toBe(undefined);

act(() => resolve('result'));
await waitForNextUpdate();

expect(result.current.loading).toBe(false);
expect(result.current.result).toBe('result');
});
it(
'populates result with the resolved value of the fn',
async () => {
const { result, waitForNextUpdate } = renderHook(() => useAsync(fn));
fn.mockResolvedValue({ resolved: 'value' });

act(() => {
result.current.start(args);
});
await waitForNextUpdate({ timeout });

expect(result.current.result).toEqual({ resolved: 'value' });
expect(result.current.error).toBeUndefined();
},
timeout
);

it(
'populates error if function rejects',
async () => {
fn.mockRejectedValue(new Error('whoops'));
const { result, waitForNextUpdate } = renderHook(() => useAsync(fn));

act(() => {
result.current.start(args);
});
await waitForNextUpdate({ timeout });

expect(result.current.result).toBeUndefined();
expect(result.current.error).toEqual(new Error('whoops'));
},
timeout
);

it(
'populates the loading state while the function is pending',
async () => {
let resolve: () => void;
fn.mockImplementation(() => new Promise<void>((_resolve) => (resolve = _resolve)));

const { result, waitForNextUpdate } = renderHook(() => useAsync(fn));

act(() => {
result.current.start(args);
});

expect(result.current.loading).toBe(true);

act(() => resolve());
await waitForNextUpdate({ timeout });

expect(result.current.loading).toBe(false);
},
timeout
);

it(
'multiple start calls reset state',
async () => {
let resolve: (result: string) => void;
fn.mockImplementation(() => new Promise((_resolve) => (resolve = _resolve)));

const { result, waitForNextUpdate } = renderHook(() => useAsync(fn));

act(() => {
result.current.start(args);
});

expect(result.current.loading).toBe(true);

act(() => resolve('result'));
await waitForNextUpdate({ timeout });

expect(result.current.loading).toBe(false);
expect(result.current.result).toBe('result');

act(() => {
result.current.start(args);
});

expect(result.current.loading).toBe(true);
expect(result.current.result).toBe(undefined);
act(() => resolve('result'));
await waitForNextUpdate({ timeout });

expect(result.current.loading).toBe(false);
expect(result.current.result).toBe('result');
},
timeout
);
});