From a34f9c71f8ec3e832c65f4947ea43bfdcba5105a Mon Sep 17 00:00:00 2001 From: Luca Greco Date: Wed, 12 Apr 2017 17:30:33 +0200 Subject: [PATCH] test: run tests for the webpack and browserify bundled files on travis --- .travis.yml | 5 ++- test/fixtures/bundle-entrypoint.js | 3 ++ test/helpers.js | 32 ------------------ test/run-module-bundlers-smoketests.sh | 11 ++++++ test/setup.js | 9 +++-- test/test-browser-global.js | 37 ++++++++++++++++---- test/test-node-export.js | 47 -------------------------- 7 files changed, 55 insertions(+), 89 deletions(-) create mode 100644 test/fixtures/bundle-entrypoint.js delete mode 100644 test/helpers.js create mode 100755 test/run-module-bundlers-smoketests.sh delete mode 100644 test/test-node-export.js diff --git a/.travis.yml b/.travis.yml index 77f1e94..530ab31 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,7 +7,10 @@ node_js: script: - npm run build - npm run test-coverage -- echo "RE-RUN all tests on the minified file" && npm run test-minified +- echo "RE-RUN tests on the minified file" && npm run test-minified +- echo "RE-RUN tests on the webpack and browserify bundled files" && + npm install -g browserify webpack && + ./test/run-module-bundlers-smoketests.sh after_script: npm run publish-coverage diff --git a/test/fixtures/bundle-entrypoint.js b/test/fixtures/bundle-entrypoint.js new file mode 100644 index 0000000..edb9a57 --- /dev/null +++ b/test/fixtures/bundle-entrypoint.js @@ -0,0 +1,3 @@ +"use strict"; + +window.browser = require("../../"); diff --git a/test/helpers.js b/test/helpers.js deleted file mode 100644 index bf4f958..0000000 --- a/test/helpers.js +++ /dev/null @@ -1,32 +0,0 @@ -"use strict"; - -const {deepEqual, equal, ok} = require("chai").assert; - -module.exports.testCustomProperties = window => { - Object.defineProperty(window.browser, "myns", { - enumerable: true, - configurable: true, - value: {mykey: true}, - }); - - ok("myns" in window.browser, "The custom property exists"); - ok("mykey" in window.browser.myns, - "The content of the custom property exists"); - - deepEqual(window.browser.myns, {mykey: true}, - "The custom property has the expected content"); - - delete window.browser.myns; - - ok(!("myns" in window.browser), - "The deleted custom defined property has been removed"); -}; - -module.exports.testUndefinedProperties = window => { - equal(window.browser.myns.mykey, true, - "Got the expected result from a wrapped property"); - equal(window.browser.myns.nonexistent, undefined, - "Got undefined for non existent property"); - equal(window.browser.nonexistent, undefined, - "Got undefined for non existent namespaces"); -}; diff --git a/test/run-module-bundlers-smoketests.sh b/test/run-module-bundlers-smoketests.sh new file mode 100755 index 0000000..16a2af0 --- /dev/null +++ b/test/run-module-bundlers-smoketests.sh @@ -0,0 +1,11 @@ +echo "\nTest webextension-polyfill bundled with webpack" +echo "===============================================" + +webpack test/fixtures/bundle-entrypoint.js /tmp/webpack-bundle.js +TEST_BUNDLED_POLYFILL=/tmp/webpack-bundle.js npm run test + +echo "\nTest webextension-polyfill bundled with browserify" +echo "==================================================" + +browserify test/fixtures/bundle-entrypoint.js > /tmp/browserify-bundle.js +TEST_BUNDLED_POLYFILL=/tmp/browserify-bundle.js npm run test diff --git a/test/setup.js b/test/setup.js index 43dec4c..fdcf53e 100644 --- a/test/setup.js +++ b/test/setup.js @@ -13,8 +13,13 @@ if (process.env.ENABLE_JSDOM_CONSOLE == "y") { // Path to the browser-polyfill script, relative to the current work dir // where mocha is executed. -const BROWSER_POLYFILL_PATH = process.env.TEST_MINIFIED_POLYFILL ? - "./dist/browser-polyfill.min.js" : "./dist/browser-polyfill.js"; +let BROWSER_POLYFILL_PATH = "./dist/browser-polyfill.js"; + +if (process.env.TEST_MINIFIED_POLYFILL) { + BROWSER_POLYFILL_PATH = "./dist/browser-polyfill.min.js"; +} else if (process.env.TEST_BUNDLED_POLYFILL) { + BROWSER_POLYFILL_PATH = process.env.TEST_BUNDLED_POLYFILL; +} // Create the jsdom window used to run the tests const testDOMWindow = jsdom("", {virtualConsole}).defaultView; diff --git a/test/test-browser-global.js b/test/test-browser-global.js index 70e796c..02d21d6 100644 --- a/test/test-browser-global.js +++ b/test/test-browser-global.js @@ -1,11 +1,9 @@ "use strict"; -const {deepEqual, equal} = require("chai").assert; +const {deepEqual, equal, ok} = require("chai").assert; const {setupTestDOMWindow} = require("./setup"); -const {testCustomProperties, testUndefinedProperties} = require("./helpers"); - describe("browser-polyfill", () => { it("wraps the global chrome namespace with a global browser namespace", () => { const fakeChrome = {}; @@ -19,7 +17,7 @@ describe("browser-polyfill", () => { runtime: {lastError: null}, }; const fakeBrowser = { - mycustomns: {mykey: true}, + mycustomns: {mybrowserkey: true}, }; return setupTestDOMWindow(fakeChrome, fakeBrowser).then(window => { @@ -31,12 +29,37 @@ describe("browser-polyfill", () => { describe("browser wrapper", () => { it("supports custom properties defined using Object.defineProperty", () => { const fakeChrome = {}; - return setupTestDOMWindow(fakeChrome).then(testCustomProperties); + return setupTestDOMWindow(fakeChrome).then(window => { + Object.defineProperty(window.browser, "myns", { + enumerable: true, + configurable: true, + value: {mykey: true}, + }); + + ok("myns" in window.browser, "The custom property exists"); + ok("mykey" in window.browser.myns, + "The content of the custom property exists"); + + deepEqual(window.browser.myns, {mykey: true}, + "The custom property has the expected content"); + + delete window.browser.myns; + + ok(!("myns" in window.browser), + "The deleted custom defined property has been removed"); + }); }); it("returns undefined for property undefined in the target", () => { - const fakeChrome = {myns: {mykey: true}}; - return setupTestDOMWindow(fakeChrome).then(testUndefinedProperties); + const fakeChrome = {myns: {mychromekey: true}}; + return setupTestDOMWindow(fakeChrome).then(window => { + equal(window.browser.myns.mychromekey, true, + "Got the expected result from a wrapped property"); + equal(window.browser.myns.nonexistent, undefined, + "Got undefined for non existent property"); + equal(window.browser.nonexistent, undefined, + "Got undefined for non existent namespaces"); + }); }); }); }); diff --git a/test/test-node-export.js b/test/test-node-export.js deleted file mode 100644 index ded9795..0000000 --- a/test/test-node-export.js +++ /dev/null @@ -1,47 +0,0 @@ -"use strict"; - -const {deepEqual, strictEqual, notStrictEqual, throws} = require("chai").assert; -const {testCustomProperties, testUndefinedProperties} = require("./helpers"); - -describe("node-export", () => { - beforeEach(() => { - delete global.browser; - delete global.chrome; - delete require.cache[require.resolve("../")]; - }); - - it("exports the global browser namespace if it already exists", () => { - global.browser = {key: "value"}; - - const exported = require("../"); - - strictEqual(exported, browser); - }); - - it("exports a wrapper around the global chrome namespace", () => { - global.chrome = {key: "value"}; - - const exported = require("../"); - - deepEqual(exported, chrome); - notStrictEqual(exported, chrome); - }); - - it("throws an error if the global chrome namespace is missing", () => { - throws(() => require("../"), ReferenceError, /chrome is not defined/); - }); - - describe("browser wrapper", () => { - it("supports custom properties defined using Object.defineProperty", () => { - global.chrome = {}; - global.browser = require("../"); - testCustomProperties(global); - }); - - it("returns undefined for property undefined in the target", () => { - global.chrome = {myns: {mykey: true}}; - global.browser = require("../"); - testUndefinedProperties(global); - }); - }); -});