From da282f465e7dcff4e7e862e8230bc576065d190d Mon Sep 17 00:00:00 2001 From: Mark Stacey Date: Thu, 18 Feb 2021 00:55:43 -0330 Subject: [PATCH] Update `sinon` and `@types/sinon` to latest versions (#352) `sinon` has been updated from v7 to v9. There have been no breaking changes that affect our usage. The types we were using were _far_ behind though, so updating those has revealed pre-existing mistakes in how we were using `sinon` in a few places. For one of the type errors, I replaced a `window.fetch` mock with `nock`, as that seemed like the simplest way to fix the type error. --- package.json | 4 +- tests/AssetsDetectionController.test.ts | 6 +- tests/CurrencyRateController.test.ts | 2 +- tests/PhishingController.test.ts | 36 +++++--- tests/TokenRatesController.test.ts | 2 +- yarn.lock | 109 +++++++++++------------- 6 files changed, 80 insertions(+), 79 deletions(-) diff --git a/package.json b/package.json index 69b9de4413..1bb8940e78 100644 --- a/package.json +++ b/package.json @@ -62,7 +62,7 @@ "@metamask/eslint-config": "^4.1.0", "@types/jest": "^22.2.3", "@types/node": "^10.1.4", - "@types/sinon": "^4.3.3", + "@types/sinon": "^9.0.10", "@types/web3": "^1.0.6", "@typescript-eslint/eslint-plugin": "^4.1.0", "@typescript-eslint/parser": "^4.1.0", @@ -76,7 +76,7 @@ "lint-staged": "^6.1.0", "nock": "^13.0.7", "prettier": "^2.1.1", - "sinon": "^7.4.1", + "sinon": "^9.2.4", "ts-jest": "^26.3.0", "typedoc": "^0.20.24", "typescript": "^4.0.3" diff --git a/tests/AssetsDetectionController.test.ts b/tests/AssetsDetectionController.test.ts index eea7b30dbc..a85e8356cb 100644 --- a/tests/AssetsDetectionController.test.ts +++ b/tests/AssetsDetectionController.test.ts @@ -328,7 +328,7 @@ describe('AssetsDetectionController', () => { assetsDetection.configure({ networkType: MAINNET, selectedAddress: '0x1' }); sandbox .stub(assetsContract, 'getBalancesInSingleCall') - .returns({ '0x6810e776880C02933D47DB1b9fc05908e5386b96': new BN(1) }); + .resolves({ '0x6810e776880C02933D47DB1b9fc05908e5386b96': new BN(1) }); await assetsDetection.detectTokens(); expect(assets.state.tokens).toEqual([ { @@ -343,7 +343,7 @@ describe('AssetsDetectionController', () => { assetsDetection.configure({ networkType: MAINNET, selectedAddress: '0x1' }); sandbox .stub(assetsContract, 'getBalancesInSingleCall') - .returns({ '0x6810e776880C02933D47DB1b9fc05908e5386b96': new BN(1) }); + .resolves({ '0x6810e776880C02933D47DB1b9fc05908e5386b96': new BN(1) }); await assetsDetection.detectTokens(); assets.removeAndIgnoreToken('0x6810e776880C02933D47DB1b9fc05908e5386b96'); @@ -355,7 +355,7 @@ describe('AssetsDetectionController', () => { assetsDetection.configure({ networkType: MAINNET }); sandbox .stub(assetsContract, 'getBalancesInSingleCall') - .returns({ '0x6810e776880C02933D47DB1b9fc05908e5386b96': new BN(1) }); + .resolves({ '0x6810e776880C02933D47DB1b9fc05908e5386b96': new BN(1) }); await assetsDetection.detectTokens(); expect(assets.state.tokens).toEqual([]); }); diff --git a/tests/CurrencyRateController.test.ts b/tests/CurrencyRateController.test.ts index 7a25de1155..63122b9466 100644 --- a/tests/CurrencyRateController.test.ts +++ b/tests/CurrencyRateController.test.ts @@ -56,7 +56,7 @@ describe('CurrencyRateController', () => { it('should poll and update rate in the right interval', () => { return new Promise((resolve) => { const controller = new CurrencyRateController({ interval: 100 }); - const mock = stub(controller, 'fetchExchangeRate').resolves({}); + const mock = stub(controller, 'fetchExchangeRate'); setTimeout(() => { expect(mock.called).toBe(true); expect(mock.calledTwice).toBe(false); diff --git a/tests/PhishingController.test.ts b/tests/PhishingController.test.ts index 2be5e0473e..073937268c 100644 --- a/tests/PhishingController.test.ts +++ b/tests/PhishingController.test.ts @@ -1,7 +1,12 @@ import { stub } from 'sinon'; +import * as nock from 'nock'; import PhishingController from '../src/third-party/PhishingController'; describe('PhishingController', () => { + afterEach(() => { + nock.cleanAll(); + }); + it('should set default state', () => { const controller = new PhishingController(); expect(controller.state.phishing).toHaveProperty('blacklist'); @@ -60,19 +65,16 @@ describe('PhishingController', () => { }); it('should not update rates if disabled', async () => { - const mock = stub(window, 'fetch'); - mock.resolves({ - json: () => ({}), - }); + nock('https://cdn.jsdelivr.net') + .get('/gh/MetaMask/eth-phishing-detect@master/src/config.json') + .replyWithError('Network error') + .persist(); const controller = new PhishingController({ disabled: true, interval: 10, }); - await controller.updatePhishingLists(); - - expect(mock.called).toBe(false); - mock.restore(); + expect(async () => await controller.updatePhishingLists()).not.toThrow(); }); it('should verify approved domain', () => { @@ -88,20 +90,26 @@ describe('PhishingController', () => { }); it('should not update phishing lists if fetch returns 304', async () => { - const mock = stub(window, 'fetch'); - mock.resolves({ status: 304 }); + nock('https://cdn.jsdelivr.net') + .get('/gh/MetaMask/eth-phishing-detect@master/src/config.json') + .reply(304) + .persist(); const controller = new PhishingController(); const oldState = controller.state.phishing; + await controller.updatePhishingLists(); + expect(controller.state.phishing).toBe(oldState); - mock.restore(); }); it('should not update phishing lists if fetch returns error', async () => { - const mock = stub(window, 'fetch'); - mock.resolves({ status: 500 }); + nock('https://cdn.jsdelivr.net') + .get('/gh/MetaMask/eth-phishing-detect@master/src/config.json') + .reply(500) + .persist(); + const controller = new PhishingController(); + await expect(controller.updatePhishingLists()).rejects.toThrow(/Fetch failed with status '500'/u); - mock.restore(); }); }); diff --git a/tests/TokenRatesController.test.ts b/tests/TokenRatesController.test.ts index 24e31c89a7..7e1c837c11 100644 --- a/tests/TokenRatesController.test.ts +++ b/tests/TokenRatesController.test.ts @@ -114,7 +114,7 @@ describe('TokenRatesController', () => { it('should handle balance not found in API', async () => { const controller = new TokenRatesController({ interval: 10 }); - stub(controller, 'fetchExchangeRate').returns({ error: 'Not Found', message: 'Not Found' }); + stub(controller, 'fetchExchangeRate').throws({ error: 'Not Found', message: 'Not Found' }); expect(controller.state.contractExchangeRates).toEqual({}); controller.tokens = [{ address: 'bar', decimals: 0, symbol: '' }]; const mock = stub(controller, 'updateExchangeRates'); diff --git a/yarn.lock b/yarn.lock index 7255b64622..351c6ced5a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -677,10 +677,10 @@ "@nodelib/fs.scandir" "2.1.3" fastq "^1.6.0" -"@sinonjs/commons@^1", "@sinonjs/commons@^1.0.2", "@sinonjs/commons@^1.4.0": - version "1.4.0" - resolved "https://registry.yarnpkg.com/@sinonjs/commons/-/commons-1.4.0.tgz#7b3ec2d96af481d7a0321252e7b1c94724ec5a78" - integrity sha512-9jHK3YF/8HtJ9wCAbG+j8cD0i0+ATS9A7gXFqS36TblLPNy6rEEc+SB0imo91eCboGaBYGV/MT1/br/J+EE7Tw== +"@sinonjs/commons@^1.6.0", "@sinonjs/commons@^1.8.1": + version "1.8.2" + resolved "https://registry.yarnpkg.com/@sinonjs/commons/-/commons-1.8.2.tgz#858f5c4b48d80778fde4b9d541f27edc0d56488b" + integrity sha512-sruwd86RJHdsVf/AtBoijDmUqJp3B6hF/DGC23C+JaegnDHaZyewCjoVGTdg3J0uz3Zs7NnIT05OBOmML72lQw== dependencies: type-detect "4.0.8" @@ -691,29 +691,21 @@ dependencies: type-detect "4.0.8" -"@sinonjs/fake-timers@^6.0.1": +"@sinonjs/fake-timers@^6.0.0", "@sinonjs/fake-timers@^6.0.1": version "6.0.1" resolved "https://registry.yarnpkg.com/@sinonjs/fake-timers/-/fake-timers-6.0.1.tgz#293674fccb3262ac782c7aadfdeca86b10c75c40" integrity sha512-MZPUxrmFubI36XS1DI3qmI0YdN1gks62JtFZvxR67ljjSNCeK6U08Zx4msEWOXuofgqUt6zPHSi1H9fbjR/NRA== dependencies: "@sinonjs/commons" "^1.7.0" -"@sinonjs/formatio@^3.2.1": - version "3.2.1" - resolved "https://registry.yarnpkg.com/@sinonjs/formatio/-/formatio-3.2.1.tgz#52310f2f9bcbc67bdac18c94ad4901b95fde267e" - integrity sha512-tsHvOB24rvyvV2+zKMmPkZ7dXX6LSLKZ7aOtXY6Edklp0uRcgGpOsQTTGTcWViFyx4uhWc6GV8QdnALbIbIdeQ== - dependencies: - "@sinonjs/commons" "^1" - "@sinonjs/samsam" "^3.1.0" - -"@sinonjs/samsam@^3.1.0", "@sinonjs/samsam@^3.3.2": - version "3.3.2" - resolved "https://registry.yarnpkg.com/@sinonjs/samsam/-/samsam-3.3.2.tgz#63942e3d5eb0b79f6de3bef9abfad15fb4b6401b" - integrity sha512-ILO/rR8LfAb60Y1Yfp9vxfYAASK43NFC2mLzpvLUbCQY/Qu8YwReboseu8aheCEkyElZF2L2T9mHcR2bgdvZyA== +"@sinonjs/samsam@^5.3.1": + version "5.3.1" + resolved "https://registry.yarnpkg.com/@sinonjs/samsam/-/samsam-5.3.1.tgz#375a45fe6ed4e92fca2fb920e007c48232a6507f" + integrity sha512-1Hc0b1TtyfBu8ixF/tpfSHTVWKwCBLY4QJbkgnE7HcwyvT2xArDxb4K7dMgqRm3szI+LJbzmW/s4xxEhv6hwDg== dependencies: - "@sinonjs/commons" "^1.0.2" - array-from "^2.1.1" - lodash "^4.17.11" + "@sinonjs/commons" "^1.6.0" + lodash.get "^4.4.2" + type-detect "^4.0.8" "@sinonjs/text-encoding@^0.7.1": version "0.7.1" @@ -858,10 +850,17 @@ dependencies: "@types/node" "*" -"@types/sinon@^4.3.3": - version "4.3.3" - resolved "https://registry.yarnpkg.com/@types/sinon/-/sinon-4.3.3.tgz#97cbbfddc3282b5fd40c7abf80b99db426fd4237" - integrity sha512-Tt7w/ylBS/OEAlSCwzB0Db1KbxnkycP/1UkQpbvKFYoUuRn4uYsC3xh5TRPrOjTy0i8TIkSz1JdNL4GPVdf3KQ== +"@types/sinon@^9.0.10": + version "9.0.10" + resolved "https://registry.yarnpkg.com/@types/sinon/-/sinon-9.0.10.tgz#7fb9bcb6794262482859cab66d59132fca18fcf7" + integrity sha512-/faDC0erR06wMdybwI/uR8wEKV/E83T0k4sepIpB7gXuy2gzx2xiOjmztq6a2Y6rIGJ04D+6UU0VBmWy+4HEMA== + dependencies: + "@types/sinonjs__fake-timers" "*" + +"@types/sinonjs__fake-timers@*": + version "6.0.2" + resolved "https://registry.yarnpkg.com/@types/sinonjs__fake-timers/-/sinonjs__fake-timers-6.0.2.tgz#3a84cf5ec3249439015e14049bd3161419bf9eae" + integrity sha512-dIPoZ3g5gcx9zZEszaxLSVTvMReD3xxyyDnQUjA6IYDG9Ba2AV0otMPs+77sG9ojB4Qr2N2Vk5RnKeuA0X/0bg== "@types/stack-utils@^1.0.1": version "1.0.1" @@ -1206,11 +1205,6 @@ array-equal@^1.0.0: resolved "https://registry.yarnpkg.com/array-equal/-/array-equal-1.0.0.tgz#8c2a5ef2472fd9ea742b04c77a75093ba2757c93" integrity sha1-jCpe8kcv2ep0KwTHenUJO6J1fJM= -array-from@^2.1.1: - version "2.1.1" - resolved "https://registry.yarnpkg.com/array-from/-/array-from-2.1.1.tgz#cfe9d8c26628b9dc5aecc62a9f5d8f1f352c1195" - integrity sha1-z+nYwmYoudxa7MYqn12PHzUsEZU= - array-includes@^3.1.1: version "3.1.1" resolved "https://registry.yarnpkg.com/array-includes/-/array-includes-3.1.1.tgz#cdd67e6852bdf9c1215460786732255ed2459348" @@ -2111,10 +2105,10 @@ diff-sequences@^26.3.0: resolved "https://registry.yarnpkg.com/diff-sequences/-/diff-sequences-26.3.0.tgz#62a59b1b29ab7fd27cef2a33ae52abe73042d0a2" integrity sha512-5j5vdRcw3CNctePNYN0Wy2e/JbWT6cAYnXv5OuqPhDpyCGc0uLu2TK0zOCJWNB9kOIfYMSpIulRaDgIi4HJ6Ig== -diff@^3.5.0: - version "3.5.0" - resolved "https://registry.yarnpkg.com/diff/-/diff-3.5.0.tgz#800c0dd1e0a8bfbc95835c202ad220fe317e5a12" - integrity sha512-A46qtFgd+g7pDZinpnwiRJtxbC1hpgf0uzP3iG89scHk0AUC7A1TGxf5OiiOUv/JMZR8GOt8hL900hV0bOy5xA== +diff@^4.0.2: + version "4.0.2" + resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.2.tgz#60f3aecb89d5fae520c11aa19efc2bb982aade7d" + integrity sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A== dir-glob@^3.0.1: version "3.0.1" @@ -4907,6 +4901,11 @@ locate-path@^5.0.0: dependencies: p-locate "^4.1.0" +lodash.get@^4.4.2: + version "4.4.2" + resolved "https://registry.yarnpkg.com/lodash.get/-/lodash.get-4.4.2.tgz#2d177f652fa31e939b4438d5341499dfa3825e99" + integrity sha1-LRd/ZS+jHpObRDjVNBSZ36OCXpk= + lodash.memoize@4.x: version "4.1.2" resolved "https://registry.yarnpkg.com/lodash.memoize/-/lodash.memoize-4.1.2.tgz#bcc6c49a42a2840ed997f323eada5ecd182e0bfe" @@ -4922,7 +4921,7 @@ lodash.sortby@^4.7.0: resolved "https://registry.yarnpkg.com/lodash.sortby/-/lodash.sortby-4.7.0.tgz#edd14c824e2cc9c1e0b0a1b42bb5210516a42438" integrity sha1-7dFMgk4sycHgsKG0K7UhBRakJDg= -lodash@^4.17.11, lodash@^4.17.13, lodash@^4.17.14, lodash@^4.17.15, lodash@^4.17.4: +lodash@^4.17.13, lodash@^4.17.14, lodash@^4.17.15, lodash@^4.17.4: version "4.17.19" resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.19.tgz#e48ddedbe30b3321783c5b4301fbd353bc1e4a4b" integrity sha512-JNvd8XER9GQX0v2qJgsaN/mzFCNA5BRe/j8JN9d+tWyGLSodKQHKFicdwNYzWwI3wjRnaKPsGj1XkBjx/F96DQ== @@ -4959,11 +4958,6 @@ loglevel@^1.5.0: resolved "https://registry.yarnpkg.com/loglevel/-/loglevel-1.6.4.tgz#f408f4f006db8354d0577dcf6d33485b3cb90d56" integrity sha512-p0b6mOGKcGa+7nnmKbpzR6qloPbrgLcnio++E+14Vo/XffOGwZtRpUhr8dTH/x2oCMmEoIU0Zwm3ZauhvYD17g== -lolex@^4.1.0, lolex@^4.2.0: - version "4.2.0" - resolved "https://registry.yarnpkg.com/lolex/-/lolex-4.2.0.tgz#ddbd7f6213ca1ea5826901ab1222b65d714b3cd7" - integrity sha512-gKO5uExCXvSm6zbF562EvM+rd1kQDnB9AZBbiQVzf1ZmdDpxUSvpnAaVOP83N/31mRK8Ml8/VE8DMvsAZQ+7wg== - lolex@^5.0.0: version "5.1.2" resolved "https://registry.yarnpkg.com/lolex/-/lolex-5.1.2.tgz#953694d098ce7c07bc5ed6d0e42bc6c0c6d5a367" @@ -5227,15 +5221,15 @@ nice-try@^1.0.4: resolved "https://registry.yarnpkg.com/nice-try/-/nice-try-1.0.5.tgz#a3378a7696ce7d223e88fc9b764bd7ef1089e366" integrity sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ== -nise@^1.5.1: - version "1.5.1" - resolved "https://registry.yarnpkg.com/nise/-/nise-1.5.1.tgz#de61d99a1d3b46b5233be4531569b9a8e27372b2" - integrity sha512-edFWm0fsFG2n318rfEnKlTZTkjlbVOFF9XIA+fj+Ed+Qz1laYW2lobwavWoMzGrYDHH1EpiNJgDfvGnkZztR/g== +nise@^4.0.4: + version "4.0.4" + resolved "https://registry.yarnpkg.com/nise/-/nise-4.0.4.tgz#d73dea3e5731e6561992b8f570be9e363c4512dd" + integrity sha512-bTTRUNlemx6deJa+ZyoCUTRvH3liK5+N6VQZ4NIw90AgDXY6iPnsqplNFf6STcj+ePk0H/xqxnP75Lr0J0Fq3A== dependencies: - "@sinonjs/formatio" "^3.2.1" + "@sinonjs/commons" "^1.7.0" + "@sinonjs/fake-timers" "^6.0.0" "@sinonjs/text-encoding" "^0.7.1" just-extend "^4.0.2" - lolex "^4.1.0" path-to-regexp "^1.7.0" nock@^13.0.7: @@ -6421,18 +6415,17 @@ single-call-balance-checker-abi@^1.0.0: resolved "https://registry.yarnpkg.com/single-call-balance-checker-abi/-/single-call-balance-checker-abi-1.0.0.tgz#b369009fd4cc6214968cdba650ad93986315d92d" integrity sha512-T5fRBJHmGEMe76JFGB36gcZnOh1ip2S7Qsp7cwmwrfMRjadxTe02zJHtXERpnQf2yvSqNWRxvae5f6e8v4rhng== -sinon@^7.4.1: - version "7.4.1" - resolved "https://registry.yarnpkg.com/sinon/-/sinon-7.4.1.tgz#bcd0c63953893e87fa0cc502f52489c32a83d4d9" - integrity sha512-7s9buHGHN/jqoy/v4bJgmt0m1XEkCEd/tqdHXumpBp0JSujaT4Ng84JU5wDdK4E85ZMq78NuDe0I3NAqXY8TFg== +sinon@^9.2.4: + version "9.2.4" + resolved "https://registry.yarnpkg.com/sinon/-/sinon-9.2.4.tgz#e55af4d3b174a4443a8762fa8421c2976683752b" + integrity sha512-zljcULZQsJxVra28qIAL6ow1Z9tpattkCTEJR4RBP3TGc00FcttsP5pK284Nas5WjMZU5Yzy3kAIp3B3KRf5Yg== dependencies: - "@sinonjs/commons" "^1.4.0" - "@sinonjs/formatio" "^3.2.1" - "@sinonjs/samsam" "^3.3.2" - diff "^3.5.0" - lolex "^4.2.0" - nise "^1.5.1" - supports-color "^5.5.0" + "@sinonjs/commons" "^1.8.1" + "@sinonjs/fake-timers" "^6.0.1" + "@sinonjs/samsam" "^5.3.1" + diff "^4.0.2" + nise "^4.0.4" + supports-color "^7.1.0" sisteransi@^1.0.3: version "1.0.3" @@ -6740,7 +6733,7 @@ supports-color@^2.0.0: resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-2.0.0.tgz#535d045ce6b6363fa40117084629995e9df324c7" integrity sha1-U10EXOa2Nj+kARcIRimZXp3zJMc= -supports-color@^5.3.0, supports-color@^5.5.0: +supports-color@^5.3.0: version "5.5.0" resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-5.5.0.tgz#e2e69a44ac8772f78a1ec0b35b689df6530efc8f" integrity sha512-QjVjwdXIt408MIiAqCX4oUKsgU2EqAGzs2Ppkm4aQYbjm+ZEWEcW4SfFNTr4uMNZma0ey4f5lgLrkB0aX0QMow== @@ -6988,7 +6981,7 @@ type-check@~0.3.2: dependencies: prelude-ls "~1.1.2" -type-detect@4.0.8: +type-detect@4.0.8, type-detect@^4.0.8: version "4.0.8" resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-4.0.8.tgz#7646fb5f18871cfbb7749e69bd39a6388eb7450c" integrity sha512-0fr/mIH1dlO+x7TlcMy+bIDqKPsw/70tVyeHW787goQjhmqaZe10uwLujubK9q9Lg6Fiho1KUKDYz0Z7k7g5/g==