From 40b118d1ecfa5b032b74a0be28b4b73ce0a0e3cc Mon Sep 17 00:00:00 2001 From: Chris Bobbe Date: Wed, 15 Jul 2020 16:56:45 -0700 Subject: [PATCH] tests: Remove our Lolex wrapper, uninstall Lolex. We've entirely switched over to Jest's "modern" fake timers, which landed in https://github.com/facebook/jest/pull/7776. --- flow-typed/npm/lolex_vx.x.x.js | 39 ------------ package.json | 1 - src/__tests__/lib/lolex.js | 111 --------------------------------- yarn.lock | 7 --- 4 files changed, 158 deletions(-) delete mode 100644 flow-typed/npm/lolex_vx.x.x.js delete mode 100644 src/__tests__/lib/lolex.js diff --git a/flow-typed/npm/lolex_vx.x.x.js b/flow-typed/npm/lolex_vx.x.x.js deleted file mode 100644 index 0c7ee446cf1..00000000000 --- a/flow-typed/npm/lolex_vx.x.x.js +++ /dev/null @@ -1,39 +0,0 @@ -// flow-typed signature: 314c6282bca14b5a4e072a34d665ef62 -// flow-typed version: <>/lolex_v5.1.1/flow_v0.92.0 - -/** - * This is an autogenerated libdef stub for: - * - * 'lolex' - * - * Fill this stub out by replacing all the `any` types. - * - * Once filled out, we encourage you to share your work with the - * community by sending a pull request to: - * https://github.com/flowtype/flow-typed - */ - -declare module 'lolex' { - declare module.exports: any; -} - -/** - * We include stubs for each file inside this npm package in case you need to - * require those files directly. Feel free to delete any files that aren't - * needed. - */ -declare module 'lolex/lolex' { - declare module.exports: any; -} - -declare module 'lolex/src/lolex-src' { - declare module.exports: any; -} - -// Filename aliases -declare module 'lolex/lolex.js' { - declare module.exports: $Exports<'lolex/lolex'>; -} -declare module 'lolex/src/lolex-src.js' { - declare module.exports: $Exports<'lolex/src/lolex-src'>; -} diff --git a/package.json b/package.json index 08fda0227de..0f8f888792a 100644 --- a/package.json +++ b/package.json @@ -124,7 +124,6 @@ "jest-environment-jsdom-global": "^2.0.4", "jest-extended": "^0.11.5", "jetifier": "^1.6.5", - "lolex": "^5.1.1", "metro-react-native-babel-preset": "^0.63.0", "prettier": "^1.18.2", "prettier-eslint": "^12.0.0", diff --git a/src/__tests__/lib/lolex.js b/src/__tests__/lib/lolex.js deleted file mode 100644 index e03cf8809b3..00000000000 --- a/src/__tests__/lib/lolex.js +++ /dev/null @@ -1,111 +0,0 @@ -// @flow strict-local - -import LolexModule from 'lolex'; - -/* - * At present (Jest v24.9.0), Jest does not override Date.now() when using a - * fake timer implementation. This means that any timer-based code relying on - * Date.now() for throttling (etc.) will be very confused. - * - * The good news is that Jest is very close to using Lolex internally -- see, - * e.g., https://github.com/facebook/jest/pull/7776 -- at which point that - * behavior will be available to us via Jest. The bad news, alas, is that it's - * not there yet. - * - * For now, we borrow slices of Jest's planned Lolex-based timer implementation. - */ - -/** - * A Lolex-backed implementation of certain relevant Jest functions. - * - * Carved from the more-complete, not-yet-NPM-available implementation at: - * https://github.com/facebook/jest/blob/9279a3a97/packages/jest-fake-timers/src/FakeTimersLolex.ts - * - * Instantiating one of these will switch Jest over to using Lolex's - * `Date.now()` replacement. Calling `.dispose()` on that instantiation will - * remove that. (Behavior in the presence of multiple Lolex instances is not - * defined. Don't do that.) - * - * Users of this class are recommended to use Jest's setup and teardown - * functions, perhaps as follows: - * - * ``` - * describe('description', () => { - * const lolex: Lolex = new Lolex(); - * - * afterEach(() => { lolex.clearAllTimers(); }); - * afterAll(() => { lolex.dispose(); }); - * - * // ...tests... - * }); - * ``` - */ -export class Lolex { - /** The installed Lolex clock object. (Name also taken from Jest's - implementation, for simplicity's sake. */ - _clock; - - constructor() { - this._clock = LolexModule.install(); - - // Wrap Lolex's setTimeout and setInterval with additional checks. - // - // In Node.js, where Jest runs, a timeout or interval of greater than - // INT32_MAX is reduced to exactly 1. (In browsers, and probably in React - // Native, it is instead truncated to an int32 value.) - // - // This can lead to subtle bugs. Rather than risk this, cause any test which - // would hit this check to fail immediately. - const maxTimer = 2 ** 31 - 1; - const wrapTimerFunction = fn => { - const wrapped = (_cb, delay: number, ...rest) => { - // do not refactor this conditional without considering NaN - if (!(delay <= maxTimer)) { - throw new Error(`timer too large (${delay}; max ${maxTimer})`); - } - // eslint-disable-next-line prefer-rest-params - return fn.apply(this, [_cb, delay, ...rest]); - }; - return wrapped; - }; - - const clock = this._clock; - clock.setTimeout = wrapTimerFunction(clock.setTimeout); - clock.setInterval = wrapTimerFunction(clock.setInterval); - } - - clearAllTimers(): void { - this._clock.reset(); - } - - getTimerCount(): number { - return this._clock.countTimers(); - } - - runOnlyPendingTimers(): void { - this._clock.runToLast(); - } - - advanceTimersByTime(msToRun: number): void { - this._clock.tick(msToRun); - } - - setSystemTime(now?: number): void { - this._clock.setSystemTime(now); - } - - dispose(): void { - this._clock.uninstall(); - } - - /** - * Convenience function; not part of jest-lolex interface. - * - * Per Lolex's implementation, adjusts both the clock and the relative - * timestamps of all timers. This can be used to simulate an environment in - * which timers are entirely stopped while their hosting process is inactive. - */ - unsafeAdvanceOnlyTime(ms: number) { - this.setSystemTime(Date.now() + ms); - } -} diff --git a/yarn.lock b/yarn.lock index 2b13455cc66..5586a32240a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7070,13 +7070,6 @@ loglevel@^1.4.1: resolved "https://registry.yarnpkg.com/loglevel/-/loglevel-1.6.3.tgz#77f2eb64be55a404c9fd04ad16d57c1d6d6b1280" integrity sha512-LoEDv5pgpvWgPF4kNYuIp0qqSJVWak/dML0RY74xlzMZiT9w77teNAwKYKWBTYjlokMirg+o3jBwp+vlLrcfAA== -lolex@^5.1.1: - version "5.1.2" - resolved "https://registry.yarnpkg.com/lolex/-/lolex-5.1.2.tgz#953694d098ce7c07bc5ed6d0e42bc6c0c6d5a367" - integrity sha512-h4hmjAvHTmd+25JSwrtTIuwbKdwg5NzZVRMLn9saij4SZaepCrTCxPr35H/3bjwfMJtN+t3CX8672UIkglz28A== - dependencies: - "@sinonjs/commons" "^1.7.0" - loose-envify@^1.0.0, loose-envify@^1.1.0, loose-envify@^1.3.1, loose-envify@^1.4.0: version "1.4.0" resolved "https://registry.yarnpkg.com/loose-envify/-/loose-envify-1.4.0.tgz#71ee51fa7be4caec1a63839f7e682d8132d30caf"