From f6aece9aea78ff4d1b9a7f9d41d6f8ef54fc5ff0 Mon Sep 17 00:00:00 2001 From: Federico Brigante Date: Fri, 12 Mar 2021 19:11:47 -0600 Subject: [PATCH] Extract tests to their own `test()` + assert message --- .../content.js | 37 +++++++++++-------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/test/fixtures/detect-existing-browser-api-object/content.js b/test/fixtures/detect-existing-browser-api-object/content.js index 7b320cba..ee2bc3e8 100644 --- a/test/fixtures/detect-existing-browser-api-object/content.js +++ b/test/fixtures/detect-existing-browser-api-object/content.js @@ -1,6 +1,6 @@ /* global originalAPIObjects */ -test("browser api object in content script", async (t) => { +test("browser api object in content script", (t) => { t.ok(browser && browser.runtime, "a global browser API object should be defined"); t.ok(chrome && chrome.runtime, "a global chrome API object should be defined"); @@ -10,27 +10,12 @@ test("browser api object in content script", async (t) => { // On Firefox, window is not the global object for content scripts, and so we expect window.browser to not // be defined. t.equal(window.browser, undefined, "window.browser is expected to be undefined on Firefox"); - - try { - await browser.storage.sync.set({a: 'a'.repeat(10000000)}); - t.fail('It should throw when attempting to set an object over quota'); - } catch (error) { - t.ok(error instanceof Error); - } } else { // Check that the polyfill has created a wrapped API namespace as expected. t.notEqual(browser.runtime, chrome.runtime, "browser.runtime and chrome.runtime should not be equal"); // On chrome, window is the global object and so the polyfilled browser API should // be also equal to window.browser. t.equal(browser, window.browser, "browser and window.browser should be the same object"); - - chrome.storage.local.set({a: 'a'.repeat(10000000)}, () => { - t.ok(chrome.runtime.lastError, 'It should throw when attempting to set an object over quota'); - t.equal(chrome.runtime.lastError.message, 'QUOTA_BYTES quota exceeded'); - t.notOk(chrome.runtime.lastError instanceof Error); - }); - - t.end(); } }); @@ -48,3 +33,23 @@ test("browser api object in background page", async (t) => { t.ok(!reply.windowBrowserIsUnchanged, "window.browser API object should have been defined by the polyfill"); } }); + +test("error types", async (t) => { + t.plan(3); + + if (navigator.userAgent.includes("Firefox/")) { + try { + await browser.storage.sync.set({a: 'a'.repeat(10000000)}); + t.fail('It should throw when attempting to set an object over quota'); + } catch (error) { + t.equal(error.message, 'QUOTA_BYTES quota exceeded'); + t.ok(error instanceof Error); + } + } else { + chrome.storage.local.set({a: 'a'.repeat(10000000)}, () => { + t.ok(chrome.runtime.lastError, 'It should throw when attempting to set an object over quota'); + t.equal(chrome.runtime.lastError.message, 'QUOTA_BYTES quota exceeded'); + t.notOk(chrome.runtime.lastError instanceof Error); + }); + } +});