From b6bdbcf587f9bbda07db18de036e2dcf3d65d075 Mon Sep 17 00:00:00 2001 From: eddie <66155195+just-toby@users.noreply.github.com> Date: Tue, 3 Oct 2023 14:37:27 -0700 Subject: [PATCH] test: mock http requests in jest (#7394) * test: try no coverage for unit tests * test: onlychanged * test: try parallel * fix: no http in unit tests * fix: deduplicate * fix: nock only * fix: maxworkers 2 * fix: remove nock * fix: restore nock * fix: comment * fix: try again with jest-offline * fix: remove jest-offline * test: try 2 again * fix: 100% --- package.json | 1 + src/setupTests.ts | 10 ++++++++++ yarn.lock | 21 ++++++++++++++++++--- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 347e299101..4dfaa26909 100644 --- a/package.json +++ b/package.json @@ -253,6 +253,7 @@ "ms": "^2.1.3", "multicodec": "^3.0.1", "multihashes": "^4.0.2", + "nock": "^13.3.3", "node-vibrant": "^3.2.1-alpha.1", "numbro": "^2.3.6", "polished": "^3.3.2", diff --git a/src/setupTests.ts b/src/setupTests.ts index 12e1506aaa..a024eeb79f 100644 --- a/src/setupTests.ts +++ b/src/setupTests.ts @@ -7,6 +7,7 @@ import 'polyfills' import type { createPopper } from '@popperjs/core' import { useWeb3React } from '@web3-react/core' import failOnConsole from 'jest-fail-on-console' +import { disableNetConnect, restore as restoreNetConnect } from 'nock' import { Readable } from 'stream' import { toBeVisible } from 'test-utils/matchers' import { mocked } from 'test-utils/mocked' @@ -116,6 +117,15 @@ beforeEach(() => { // Mock useWeb3React to return a chainId of 1 by default. mocked(useWeb3React).mockReturnValue({ chainId: 1 } as ReturnType) + + // Disable network connections by default. + disableNetConnect() +}) + +afterEach(() => { + // Without this, nock causes a memory leak and the tests will fail on CI. + // https://github.com/nock/nock/issues/1817 + restoreNetConnect() }) /** diff --git a/yarn.lock b/yarn.lock index 0789991833..e8435e0e72 100644 --- a/yarn.lock +++ b/yarn.lock @@ -15606,6 +15606,16 @@ no-case@^3.0.4: lower-case "^2.0.2" tslib "^2.0.3" +nock@^13.3.3: + version "13.3.3" + resolved "https://registry.yarnpkg.com/nock/-/nock-13.3.3.tgz#179759c07d3f88ad3e794ace885629c1adfd3fe7" + integrity sha512-z+KUlILy9SK/RjpeXDiDUEAq4T94ADPHE3qaRkf66mpEhzc/ytOMm3Bwdrbq6k1tMWkbdujiKim3G2tfQARuJw== + dependencies: + debug "^4.1.0" + json-stringify-safe "^5.0.1" + lodash "^4.17.21" + propagate "^2.0.0" + node-abi@^3.3.0: version "3.47.0" resolved "https://registry.yarnpkg.com/node-abi/-/node-abi-3.47.0.tgz#6cbfa2916805ae25c2b7156ca640131632eb05e8" @@ -17156,6 +17166,11 @@ prop-types@^15.5.10, prop-types@^15.6.0, prop-types@^15.6.1, prop-types@^15.6.2, object-assign "^4.1.1" react-is "^16.13.1" +propagate@^2.0.0: + version "2.0.1" + resolved "https://registry.yarnpkg.com/propagate/-/propagate-2.0.1.tgz#40cdedab18085c792334e64f0ac17256d38f9a45" + integrity sha512-vGrhOavPSTz4QVNuBNdcNXePNdNMaO1xj9yBeH1ScQPjk/rhg9sSlCXPhMkFuaNNW/syTvYqsnbIJxMBfRbbag== + proxy-addr@~2.0.7: version "2.0.7" resolved "https://registry.yarnpkg.com/proxy-addr/-/proxy-addr-2.0.7.tgz#f19fe69ceab311eeb94b42e70e8c2070f9ba1025" @@ -18405,9 +18420,9 @@ selfsigned@^2.0.1, selfsigned@^2.1.1: node-forge "^1" semver@^5.5.0: - version "5.7.1" - resolved "https://registry.yarnpkg.com/semver/-/semver-5.7.1.tgz#a954f931aeba508d307bbf069eff0c01c96116f7" - integrity sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ== + version "5.7.2" + resolved "https://registry.yarnpkg.com/semver/-/semver-5.7.2.tgz#48d55db737c3287cd4835e17fa13feace1c41ef8" + integrity sha512-cBznnQ9KjJqU67B52RMC65CMarK2600WFnbkcaiwWq3xy/5haFJlshgnpjovMVJ+Hff49d8GEn0b87C5pDQ10g== semver@^6.0.0, semver@^6.1.1, semver@^6.1.2, semver@^6.3.0, semver@^6.3.1: version "6.3.1"