From 1b4883ab157d11add24355b7f7c4c86b359ef909 Mon Sep 17 00:00:00 2001 From: Patrick Hulce Date: Wed, 6 Mar 2019 16:49:46 -0600 Subject: [PATCH] tests: switch to jest.spyOn (#7404) --- lighthouse-core/test/runner-test.js | 72 ++++++++++++++++------------- package.json | 1 - yarn.lock | 58 ----------------------- 3 files changed, 39 insertions(+), 92 deletions(-) diff --git a/lighthouse-core/test/runner-test.js b/lighthouse-core/test/runner-test.js index 18a05dab68..97132acdab 100644 --- a/lighthouse-core/test/runner-test.js +++ b/lighthouse-core/test/runner-test.js @@ -15,27 +15,33 @@ const assetSaver = require('../lib/asset-saver'); const fs = require('fs'); const assert = require('assert'); const path = require('path'); -const sinon = require('sinon'); const rimraf = require('rimraf'); const LHError = require('../lib/lh-error.js'); /* eslint-env jest */ describe('Runner', () => { - const saveArtifactsSpy = sinon.spy(assetSaver, 'saveArtifacts'); - const loadArtifactsSpy = sinon.spy(assetSaver, 'loadArtifacts'); - const gatherRunnerRunSpy = sinon.spy(GatherRunner, 'run'); - const runAuditSpy = sinon.spy(Runner, '_runAudit'); - - function resetSpies() { - saveArtifactsSpy.reset(); - loadArtifactsSpy.reset(); - gatherRunnerRunSpy.reset(); - runAuditSpy.reset(); - } + /** @type {jest.Mock} */ + let saveArtifactsSpy; + /** @type {jest.Mock} */ + let loadArtifactsSpy; + /** @type {jest.Mock} */ + let gatherRunnerRunSpy; + /** @type {jest.Mock} */ + let runAuditSpy; beforeEach(() => { - resetSpies(); + saveArtifactsSpy = jest.spyOn(assetSaver, 'saveArtifacts'); + loadArtifactsSpy = jest.spyOn(assetSaver, 'loadArtifacts'); + gatherRunnerRunSpy = jest.spyOn(GatherRunner, 'run'); + runAuditSpy = jest.spyOn(Runner, '_runAudit'); + }); + + afterEach(() => { + saveArtifactsSpy.mockRestore(); + loadArtifactsSpy.mockRestore(); + gatherRunnerRunSpy.mockRestore(); + runAuditSpy.mockRestore(); }); const basicAuditMeta = { @@ -64,15 +70,15 @@ describe('Runner', () => { it('-G gathers, quits, and doesn\'t run audits', () => { const opts = {url, config: generateConfig({gatherMode: artifactsPath}), driverMock}; return Runner.run(null, opts).then(_ => { - assert.equal(loadArtifactsSpy.called, false, 'loadArtifacts was called'); + expect(loadArtifactsSpy).not.toHaveBeenCalled(); + expect(saveArtifactsSpy).toHaveBeenCalled(); - assert.equal(saveArtifactsSpy.called, true, 'saveArtifacts was not called'); - const saveArtifactArg = saveArtifactsSpy.getCall(0).args[0]; + const saveArtifactArg = saveArtifactsSpy.mock.calls[0][0]; assert.ok(saveArtifactArg.ViewportDimensions); assert.ok(saveArtifactArg.devtoolsLogs.defaultPass.length > 100); - assert.equal(gatherRunnerRunSpy.called, true, 'GatherRunner.run was not called'); - assert.equal(runAuditSpy.called, false, '_runAudit was called'); + expect(gatherRunnerRunSpy).toHaveBeenCalled(); + expect(runAuditSpy).not.toHaveBeenCalled(); assert.ok(fs.existsSync(resolvedPath)); assert.ok(fs.existsSync(`${resolvedPath}/artifacts.json`)); @@ -83,10 +89,10 @@ describe('Runner', () => { it('-A audits from saved artifacts and doesn\'t gather', () => { const opts = {config: generateConfig({auditMode: artifactsPath}), driverMock}; return Runner.run(null, opts).then(_ => { - assert.equal(loadArtifactsSpy.called, true, 'loadArtifacts was not called'); - assert.equal(gatherRunnerRunSpy.called, false, 'GatherRunner.run was called'); - assert.equal(saveArtifactsSpy.called, false, 'saveArtifacts was called'); - assert.equal(runAuditSpy.called, true, '_runAudit was not called'); + expect(loadArtifactsSpy).toHaveBeenCalled(); + expect(gatherRunnerRunSpy).not.toHaveBeenCalled(); + expect(saveArtifactsSpy).not.toHaveBeenCalled(); + expect(runAuditSpy).toHaveBeenCalled(); }); }); @@ -131,20 +137,20 @@ describe('Runner', () => { const settings = {auditMode: artifactsPath, gatherMode: artifactsPath}; const opts = {url, config: generateConfig(settings), driverMock}; return Runner.run(null, opts).then(_ => { - assert.equal(loadArtifactsSpy.called, false, 'loadArtifacts was called'); - assert.equal(gatherRunnerRunSpy.called, true, 'GatherRunner.run was not called'); - assert.equal(saveArtifactsSpy.called, true, 'saveArtifacts was not called'); - assert.equal(runAuditSpy.called, true, '_runAudit was not called'); + expect(loadArtifactsSpy).not.toHaveBeenCalled(); + expect(gatherRunnerRunSpy).toHaveBeenCalled(); + expect(saveArtifactsSpy).toHaveBeenCalled(); + expect(runAuditSpy).toHaveBeenCalled(); }); }); it('non -G/-A run doesn\'t save artifacts to disk', () => { const opts = {url, config: generateConfig(), driverMock}; return Runner.run(null, opts).then(_ => { - assert.equal(loadArtifactsSpy.called, false, 'loadArtifacts was called'); - assert.equal(gatherRunnerRunSpy.called, true, 'GatherRunner.run was not called'); - assert.equal(saveArtifactsSpy.called, false, 'saveArtifacts was called'); - assert.equal(runAuditSpy.called, true, '_runAudit was not called'); + expect(loadArtifactsSpy).not.toHaveBeenCalled(); + expect(gatherRunnerRunSpy).toHaveBeenCalled(); + expect(saveArtifactsSpy).not.toHaveBeenCalled(); + expect(runAuditSpy).toHaveBeenCalled(); }); }); }); @@ -161,7 +167,7 @@ describe('Runner', () => { }); return Runner.run(null, {url, config, driverMock}).then(_ => { - assert.equal(gatherRunnerRunSpy.called, true, 'GatherRunner.run was not called'); + expect(gatherRunnerRunSpy).toHaveBeenCalled(); assert.ok(typeof config.passes[0].gatherers[0] === 'object'); }); }); @@ -416,8 +422,8 @@ describe('Runner', () => { assert.ok(results.lhr.lighthouseVersion); assert.ok(results.lhr.fetchTime); assert.equal(results.lhr.requestedUrl, url); - assert.equal(gatherRunnerRunSpy.called, true, 'GatherRunner.run was not called'); assert.equal(results.lhr.audits['content-width'].id, 'content-width'); + expect(gatherRunnerRunSpy).toHaveBeenCalled(); }); }); @@ -443,10 +449,10 @@ describe('Runner', () => { }); return Runner.run(null, {url, config, driverMock}).then(results => { + expect(gatherRunnerRunSpy).toHaveBeenCalled(); assert.ok(results.lhr.lighthouseVersion); assert.ok(results.lhr.fetchTime); assert.equal(results.lhr.requestedUrl, url); - assert.equal(gatherRunnerRunSpy.called, true, 'GatherRunner.run was not called'); assert.equal(results.lhr.audits['content-width'].id, 'content-width'); assert.equal(results.lhr.audits['content-width'].score, 1); assert.equal(results.lhr.categories.category.score, 1); diff --git a/package.json b/package.json index 0e1b15e962..8d86231dc8 100644 --- a/package.json +++ b/package.json @@ -126,7 +126,6 @@ "prettier": "^1.14.3", "pretty-json-stringify": "^0.0.2", "puppeteer": "^1.10.0", - "sinon": "^2.3.5", "typescript": "3.2.2", "uglify-es": "3.0.15", "url-search-params": "0.6.1", diff --git a/yarn.lock b/yarn.lock index ffd15e8e46..08d35735b3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2558,11 +2558,6 @@ diff@1.4.0: resolved "https://registry.yarnpkg.com/diff/-/diff-1.4.0.tgz#7f28d2eb9ee7b15a97efd89ce63dcfdaa3ccbabf" integrity sha1-fyjS657nsVqX79ic5j3P2qPMur8= -diff@^3.1.0: - version "3.4.0" - resolved "https://registry.yarnpkg.com/diff/-/diff-3.4.0.tgz#b1d85507daf3964828de54b37d0d73ba67dda56c" - integrity sha512-QpVuMTEoJMF7cKzi6bvWhRulU1fZqZnvyVQgNhPaxxuTYwyjn/j1v9falseQ/uXWwPnO56RBfwtg4h/EQXmucA== - diff@^3.2.0: version "3.5.0" resolved "https://registry.yarnpkg.com/diff/-/diff-3.5.0.tgz#800c0dd1e0a8bfbc95835c202ad220fe317e5a12" @@ -3301,13 +3296,6 @@ form-data@~2.3.2: combined-stream "^1.0.6" mime-types "^2.1.12" -formatio@1.2.0: - version "1.2.0" - resolved "https://registry.yarnpkg.com/formatio/-/formatio-1.2.0.tgz#f3b2167d9068c4698a8d51f4f760a39a54d818eb" - integrity sha1-87IWfZBoxGmKjVH092CjmlTYGOs= - dependencies: - samsam "1.x" - fragment-cache@^0.2.1: version "0.2.1" resolved "https://registry.yarnpkg.com/fragment-cache/-/fragment-cache-0.2.1.tgz#4290fad27f13e89be7f33799c6bc5a0abfff0d19" @@ -5389,11 +5377,6 @@ log-driver@^1.2.7: resolved "https://registry.yarnpkg.com/log-driver/-/log-driver-1.2.7.tgz#63b95021f0702fedfa2c9bb0a24e7797d71871d8" integrity sha512-U7KCmLdqsGHBLeWqYlFA0V0Sl6P08EE1ZrmA9cxjUE0WVqT9qnyVDPz1kzpFEP0jdJuFnasWIfSd7fsaNXkpbg== -lolex@^1.6.0: - version "1.6.0" - resolved "https://registry.yarnpkg.com/lolex/-/lolex-1.6.0.tgz#3a9a0283452a47d7439e72731b9e07d7386e49f6" - integrity sha1-OpoCg0UqR9dDnnJzG54H1zhuSfY= - longest@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/longest/-/longest-1.0.1.tgz#30a0b2da38f73770e8294a0d22e6625ed77d0097" @@ -5795,11 +5778,6 @@ nanomatch@^1.2.9: snapdragon "^0.8.1" to-regex "^3.0.1" -native-promise-only@^0.8.1: - version "0.8.1" - resolved "https://registry.yarnpkg.com/native-promise-only/-/native-promise-only-0.8.1.tgz#20a318c30cb45f71fe7adfbf7b21c99c1472ef11" - integrity sha1-IKMYwwy0X3H+et+/eyHJnBRy7xE= - natural-compare@^1.4.0: version "1.4.0" resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7" @@ -6332,13 +6310,6 @@ path-platform@~0.11.15: resolved "https://registry.yarnpkg.com/path-platform/-/path-platform-0.11.15.tgz#e864217f74c36850f0852b78dc7bf7d4a5721bf2" integrity sha1-6GQhf3TDaFDwhSt43Hv31KVyG/I= -path-to-regexp@^1.7.0: - version "1.7.0" - resolved "https://registry.yarnpkg.com/path-to-regexp/-/path-to-regexp-1.7.0.tgz#59fde0f435badacba103a84e9d3bc64e96b9937d" - integrity sha1-Wf3g9DW62suhA6hOnTvGTpa5k30= - dependencies: - isarray "0.0.1" - path-type@^1.0.0: version "1.1.0" resolved "https://registry.yarnpkg.com/path-type/-/path-type-1.1.0.tgz#59c44f7ee491da704da415da5a4070ba4f8fe441" @@ -7082,11 +7053,6 @@ safe-regex@^1.1.0: resolved "https://registry.yarnpkg.com/safer-buffer/-/safer-buffer-2.1.2.tgz#44fa161b0187b9549dd84bb91802f9bd8385cd6a" integrity sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg== -samsam@1.x, samsam@^1.1.3: - version "1.3.0" - resolved "https://registry.yarnpkg.com/samsam/-/samsam-1.3.0.tgz#8d1d9350e25622da30de3e44ba692b5221ab7c50" - integrity sha512-1HwIYD/8UlOtFS3QO3w7ey+SdSDFE4HRNLZoZRYVQefrOY3l17epswImeB1ijgJFQJodIaHcwkp3r/myBjFVbg== - sane@^2.0.0: version "2.5.2" resolved "https://registry.yarnpkg.com/sane/-/sane-2.5.2.tgz#b4dc1861c21b427e929507a3e751e2a2cb8ab3fa" @@ -7244,20 +7210,6 @@ simple-concat@^1.0.0: resolved "https://registry.yarnpkg.com/simple-concat/-/simple-concat-1.0.0.tgz#7344cbb8b6e26fb27d66b2fc86f9f6d5997521c6" integrity sha1-c0TLuLbib7J9ZrL8hvn21Zl1IcY= -sinon@^2.3.5: - version "2.4.1" - resolved "https://registry.yarnpkg.com/sinon/-/sinon-2.4.1.tgz#021fd64b54cb77d9d2fb0d43cdedfae7629c3a36" - integrity sha512-vFTrO9Wt0ECffDYIPSP/E5bBugt0UjcBQOfQUMh66xzkyPEnhl/vM2LRZi2ajuTdkH07sA6DzrM6KvdvGIH8xw== - dependencies: - diff "^3.1.0" - formatio "1.2.0" - lolex "^1.6.0" - native-promise-only "^0.8.1" - path-to-regexp "^1.7.0" - samsam "^1.1.3" - text-encoding "0.6.4" - type-detect "^4.0.0" - sisteransi@^0.1.0: version "0.1.1" resolved "https://registry.yarnpkg.com/sisteransi/-/sisteransi-0.1.1.tgz#5431447d5f7d1675aac667ccd0b865a4994cb3ce" @@ -7807,11 +7759,6 @@ test-exclude@^4.2.0, test-exclude@^4.2.1: read-pkg-up "^1.0.1" require-main-filename "^1.0.1" -text-encoding@0.6.4: - version "0.6.4" - resolved "https://registry.yarnpkg.com/text-encoding/-/text-encoding-0.6.4.tgz#e399a982257a276dae428bb92845cb71bdc26d19" - integrity sha1-45mpgiV6J22uQou5KEXLcb3CbRk= - text-extensions@^1.0.0: version "1.7.0" resolved "https://registry.yarnpkg.com/text-extensions/-/text-extensions-1.7.0.tgz#faaaba2625ed746d568a23e4d0aacd9bf08a8b39" @@ -8012,11 +7959,6 @@ type-check@~0.3.2: dependencies: prelude-ls "~1.1.2" -type-detect@^4.0.0: - version "4.0.3" - resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-4.0.3.tgz#0e3f2670b44099b0b46c284d136a7ef49c74c2ea" - integrity sha1-Dj8mcLRAmbC0bChNE2p+9Jx0wuo= - typedarray@^0.0.6: version "0.0.6" resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"