Fix flakiness in request lib tests (#42647) (#42711)

* Remove flakiness from 'pollIntervalMs' test by making timing irrelevant.
* Remove flakiness from 'state' tests by increasing wait time.
* Fix flakiness with 'resets the pollIntervalMs' test by increasing the wait time.
This commit is contained in:
CJ Cenizal 2019-08-06 11:24:46 -07:00 committed by GitHub
parent 9dca142391
commit 2dd0c613ae
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -98,20 +98,19 @@ describe('request lib', () => {
describe('path, method, body', () => { describe('path, method, body', () => {
it('is used to send the request', async () => { it('is used to send the request', async () => {
initUseRequest({ ...successRequest }); initUseRequest({ ...successRequest });
await wait(10); await wait(50);
expect(hook.data).toBe(successResponse.data); expect(hook.data).toBe(successResponse.data);
}); });
}); });
// FLAKY: https://github.com/elastic/kibana/issues/42561 describe('pollIntervalMs', () => {
describe.skip('pollIntervalMs', () => {
it('sends another request after the specified time has elapsed', async () => { it('sends another request after the specified time has elapsed', async () => {
initUseRequest({ ...successRequest, pollIntervalMs: 30 }); initUseRequest({ ...successRequest, pollIntervalMs: 10 });
await wait(5); await wait(50);
sinon.assert.calledOnce(sendPost); // We just care that multiple requests have been sent out. We don't check the specific
// timing because that risks introducing flakiness into the tests, and it's unlikely
await wait(40); // we could break the implementation by getting the exact timing wrong.
sinon.assert.calledTwice(sendPost); expect(sendPost.callCount).toBeGreaterThan(1);
// We have to manually clean up or else the interval will continue to fire requests, // We have to manually clean up or else the interval will continue to fire requests,
// interfering with other tests. // interfering with other tests.
@ -132,14 +131,14 @@ describe('request lib', () => {
initUseRequest({ ...successRequest, deserializer }); initUseRequest({ ...successRequest, deserializer });
sinon.assert.notCalled(deserializer); sinon.assert.notCalled(deserializer);
await wait(5); await wait(50);
sinon.assert.calledOnce(deserializer); sinon.assert.calledOnce(deserializer);
sinon.assert.calledWith(deserializer, successResponse.data); sinon.assert.calledWith(deserializer, successResponse.data);
}); });
it('processes data', async () => { it('processes data', async () => {
initUseRequest({ ...successRequest, deserializer: () => 'intercepted' }); initUseRequest({ ...successRequest, deserializer: () => 'intercepted' });
await wait(5); await wait(50);
expect(hook.data).toBe('intercepted'); expect(hook.data).toBe('intercepted');
}); });
}); });
@ -152,7 +151,7 @@ describe('request lib', () => {
expect(hook.isInitialRequest).toBe(true); expect(hook.isInitialRequest).toBe(true);
hook.sendRequest(); hook.sendRequest();
await wait(5); await wait(50);
expect(hook.isInitialRequest).toBe(false); expect(hook.isInitialRequest).toBe(false);
}); });
}); });
@ -162,7 +161,7 @@ describe('request lib', () => {
initUseRequest({ ...successRequest }); initUseRequest({ ...successRequest });
expect(hook.isLoading).toBe(true); expect(hook.isLoading).toBe(true);
await wait(5); await wait(50);
expect(hook.isLoading).toBe(false); expect(hook.isLoading).toBe(false);
}); });
}); });
@ -170,14 +169,13 @@ describe('request lib', () => {
describe('error', () => { describe('error', () => {
it('surfaces errors from requests', async () => { it('surfaces errors from requests', async () => {
initUseRequest({ ...errorRequest }); initUseRequest({ ...errorRequest });
await wait(10); await wait(50);
expect(hook.error).toBe(errorResponse); expect(hook.error).toBe(errorResponse);
}); });
// FLAKY: https://github.com/elastic/kibana/issues/42563 it('persists while a request is in-flight', async () => {
it.skip('persists while a request is in-flight', async () => {
initUseRequest({ ...errorRequest }); initUseRequest({ ...errorRequest });
await wait(5); await wait(50);
hook.sendRequest(); hook.sendRequest();
expect(hook.isLoading).toBe(true); expect(hook.isLoading).toBe(true);
expect(hook.error).toBe(errorResponse); expect(hook.error).toBe(errorResponse);
@ -185,7 +183,7 @@ describe('request lib', () => {
it('is undefined when the request is successful', async () => { it('is undefined when the request is successful', async () => {
initUseRequest({ ...successRequest }); initUseRequest({ ...successRequest });
await wait(10); await wait(50);
expect(hook.isLoading).toBe(false); expect(hook.isLoading).toBe(false);
expect(hook.error).toBeUndefined(); expect(hook.error).toBeUndefined();
}); });
@ -194,30 +192,28 @@ describe('request lib', () => {
describe('data', () => { describe('data', () => {
it('surfaces payloads from requests', async () => { it('surfaces payloads from requests', async () => {
initUseRequest({ ...successRequest }); initUseRequest({ ...successRequest });
await wait(10); await wait(50);
expect(hook.data).toBe(successResponse.data); expect(hook.data).toBe(successResponse.data);
}); });
it('persists while a request is in-flight', async () => { it('persists while a request is in-flight', async () => {
initUseRequest({ ...successRequest }); initUseRequest({ ...successRequest });
await wait(5); await wait(50);
hook.sendRequest(); hook.sendRequest();
expect(hook.isLoading).toBe(true); expect(hook.isLoading).toBe(true);
expect(hook.data).toBe(successResponse.data); expect(hook.data).toBe(successResponse.data);
}); });
// FLAKY: https://github.com/elastic/kibana/issues/42562 it('is undefined when the request fails', async () => {
it.skip('is undefined when the request fails', async () => {
initUseRequest({ ...errorRequest }); initUseRequest({ ...errorRequest });
await wait(10); await wait(50);
expect(hook.isLoading).toBe(false); expect(hook.isLoading).toBe(false);
expect(hook.data).toBeUndefined(); expect(hook.data).toBeUndefined();
}); });
}); });
}); });
// FLAKY: https://github.com/elastic/kibana/issues/42225 describe('callbacks', () => {
describe.skip('callbacks', () => {
describe('sendRequest', () => { describe('sendRequest', () => {
it('sends the request', () => { it('sends the request', () => {
initUseRequest({ ...successRequest }); initUseRequest({ ...successRequest });
@ -227,19 +223,26 @@ describe('request lib', () => {
}); });
it('resets the pollIntervalMs', async () => { it('resets the pollIntervalMs', async () => {
initUseRequest({ ...successRequest, pollIntervalMs: 30 }); initUseRequest({ ...successRequest, pollIntervalMs: 800 });
await wait(5); await wait(200); // 200ms
sinon.assert.calledOnce(sendPost); hook.sendRequest();
expect(sendPost.callCount).toBe(2);
await wait(20); await wait(200); // 400ms
hook.sendRequest(); hook.sendRequest();
// If the request didn't reset the interval, there would have been three requests sent by now. await wait(200); // 600ms
await wait(20); hook.sendRequest();
sinon.assert.calledTwice(sendPost);
await wait(20); await wait(200); // 800ms
sinon.assert.calledThrice(sendPost); hook.sendRequest();
await wait(200); // 1000ms
hook.sendRequest();
// If sendRequest didn't reset the interval, the interval would have triggered another
// request by now, and the callCount would be 7.
expect(sendPost.callCount).toBe(6);
// We have to manually clean up or else the interval will continue to fire requests, // We have to manually clean up or else the interval will continue to fire requests,
// interfering with other tests. // interfering with other tests.