Skip to content

Commit

Permalink
Pressability: Eliminate Unit Test Timing Flakiness
Browse files Browse the repository at this point in the history
Summary:
Currently, it is possible in one of the "minimum press duration" unit tests for certain instructions to take longer than expected, skewing the return value of `Date.now()` by at least 10ms.

This changes the unit test to mock `Date.now()` more accurately so that the test is no longer flakey.

Changelog: [Internal]

Reviewed By: jacdebug

Differential Revision: D39804152

fbshipit-source-id: ab62fd1921bd015d969da9595bd3267c38c6e59c
  • Loading branch information
yungsters authored and facebook-github-bot committed Sep 26, 2022
1 parent bf55a3a commit 8edf4e9
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion Libraries/Pressability/__tests__/Pressability-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ const createMockPressEvent = (
describe('Pressability', () => {
beforeEach(() => {
jest.resetModules();
jest.restoreAllMocks();
jest.spyOn(Date, 'now');
jest.spyOn(HoverState, 'isHoverEnabled');
});
Expand Down Expand Up @@ -667,15 +668,20 @@ describe('Pressability', () => {
handlers.onResponderMove(createMockPressEvent('onResponderMove'));
jest.runOnlyPendingTimers();
expect(config.onPressIn).toBeCalled();

// WORKAROUND: Jest does not advance `Date.now()`.
const touchActivateTime = Date.now();
expect(Date.now).toHaveBeenCalledTimes(1);
const touchActivateTime = Date.now.mock.results[0].value;
jest.advanceTimersByTime(120);
Date.now.mockReturnValue(touchActivateTime + 120);
handlers.onResponderRelease(createMockPressEvent('onResponderRelease'));

expect(config.onPressOut).not.toBeCalled();
jest.advanceTimersByTime(10);
Date.now.mockReturnValue(touchActivateTime + 130);
expect(config.onPressOut).toBeCalled();

Date.now.mockRestore();
});

it('is called synchronously if minimum press duration is 0ms', () => {
Expand Down

0 comments on commit 8edf4e9

Please sign in to comment.