Refactor tests to not rely on globals

This commit is contained in:
stoically 2020-01-24 08:16:15 +01:00
parent 53baee1d5c
commit 405e605ba3
No known key found for this signature in database
GPG key ID: 3026ED2F90EE0DE9
10 changed files with 213 additions and 311 deletions

View file

@ -19,14 +19,14 @@
"json": "^9.0.6",
"mocha": "^6.2.2",
"npm-run-all": "^4.0.0",
"nyc": "^14.1.1",
"nyc": "^15.0.0",
"sinon": "^7.5.0",
"sinon-chai": "^3.3.0",
"stylelint": "^7.9.0",
"stylelint-config-standard": "^16.0.0",
"stylelint-order": "^0.3.0",
"web-ext": "^2.9.3",
"webextensions-jsdom": "^1.1.0"
"webextensions-jsdom": "^1.2.1"
},
"homepage": "https://github.com/mozilla/multi-account-containers#readme",
"license": "MPL-2.0",
@ -44,9 +44,8 @@
"lint:js": "eslint .",
"package": "rm -rf src/web-ext-artifacts && npm run build && mv src/web-ext-artifacts/firefox_multi-account_containers-*.zip addon.xpi",
"test": "npm run lint && npm run coverage",
"mocha": "mocha ./test/setup.js test/**/*.test.js",
"mochaSingle": "mocha ./test/setup.js",
"test-watch": "mocha ./test/setup.js test/**/*.test.js --watch",
"coverage": "nyc --reporter=html --reporter=text mocha ./test/setup.js test/**/*.test.js --timeout 60000"
"test:once": "mocha test/**/*.test.js",
"test:watch": "npm run test:once -- --watch",
"coverage": "nyc --reporter=html --reporter=text mocha test/**/*.test.js --timeout 60000"
}
}

View file

@ -6,15 +6,7 @@ module.exports = {
"parserOptions": {
"ecmaVersion": 2018
},
globals: {
"sinon": false,
"expect": false,
"nextTick": false,
"buildDom": false,
"buildBackgroundDom": false,
"background": false,
"buildPopupDom": false,
"popup": false,
"helper": false
"rules": {
"no-restricted-globals": ["error", "browser"]
}
}

View file

@ -2,15 +2,16 @@ if (!process.listenerCount("unhandledRejection")) {
// eslint-disable-next-line no-console
process.on("unhandledRejection", r => console.log(r));
}
const path = require("path");
const chai = require("chai");
const sinonChai = require("sinon-chai");
const crypto = require("crypto");
global.sinon = require("sinon");
global.expect = chai.expect;
const sinon = require("sinon");
const expect = chai.expect;
chai.should();
chai.use(sinonChai);
global.nextTick = () => {
const nextTick = () => {
return new Promise(resolve => {
setTimeout(() => {
process.nextTick(resolve);
@ -18,12 +19,10 @@ global.nextTick = () => {
});
};
global.helper = require("./helper");
const webExtensionsJSDOM = require("webextensions-jsdom");
const manifestPath = path.resolve(path.join(__dirname, "../src/manifest.json"));
global.buildDom = async ({background = {}, popup = {}}) => {
const buildDom = async ({background = {}, popup = {}}) => {
background = {
...background,
jsdom: {
@ -53,33 +52,60 @@ global.buildDom = async ({background = {}, popup = {}}) => {
popup
});
// eslint-disable-next-line require-atomic-updates
global.background = webExtension.background;
// eslint-disable-next-line require-atomic-updates
global.popup = webExtension.popup;
webExtension.browser = webExtension.background.browser;
return webExtension;
};
global.buildBackgroundDom = async background => {
await global.buildDom({
const buildBackgroundDom = background => {
return buildDom({
background,
popup: false
});
};
global.buildPopupDom = async popup => {
await global.buildDom({
const buildPopupDom = popup => {
return buildDom({
popup,
background: false
});
};
global.afterEach(() => {
if (global.background) {
global.background.destroy();
const initializeWithTab = async (details = {
cookieStoreId: "firefox-default"
}) => {
let tab;
const webExtension = await buildDom({
background: {
async afterBuild(background) {
tab = await background.browser.tabs._create(details);
}
if (global.popup) {
global.popup.destroy();
},
popup: {
jsdom: {
beforeParse(window) {
window.browser.storage.local.set({
"browserActionBadgesClicked": [],
"onboarding-stage": 6,
"achievements": [],
"syncEnabled": true
});
window.browser.storage.local.set.resetHistory();
window.browser.storage.sync.clear();
}
});
}
}
});
webExtension.tab = tab;
return webExtension;
};
module.exports = {
buildDom,
buildBackgroundDom,
buildPopupDom,
initializeWithTab,
sinon,
expect,
nextTick,
};

View file

@ -1,25 +1,30 @@
describe("Assignment Feature", () => {
const {initializeWithTab} = require("../common");
describe("Assignment Feature", function () {
const url = "http://example.com";
let activeTab;
beforeEach(async () => {
activeTab = await helper.browser.initializeWithTab({
beforeEach(async function () {
this.webExt = await initializeWithTab({
cookieStoreId: "firefox-container-1",
url
});
});
describe("click the 'Always open in' checkbox in the popup", () => {
beforeEach(async () => {
// popup click to set assignment for activeTab.url
await helper.popup.clickElementById("container-page-assigned");
afterEach(function () {
this.webExt.destroy();
});
describe("open new Tab with the assigned URL in the default container", () => {
describe("click the 'Always open in' checkbox in the popup", function () {
beforeEach(async function () {
// popup click to set assignment for activeTab.url
await this.webExt.popup.helper.clickElementById("container-page-assigned");
});
describe("open new Tab with the assigned URL in the default container", function () {
let newTab;
beforeEach(async () => {
beforeEach(async function () {
// new Tab opening activeTab.url in default container
newTab = await helper.browser.openNewTab({
newTab = await this.webExt.background.browser.tabs._create({
cookieStoreId: "firefox-default",
url
}, {
@ -29,12 +34,12 @@ describe("Assignment Feature", () => {
});
});
it("should open the confirm page", async () => {
it("should open the confirm page", async function () {
// should have created a new tab with the confirm page
background.browser.tabs.create.should.have.been.calledWithMatch({
this.webExt.background.browser.tabs.create.should.have.been.calledWithMatch({
url: "moz-extension://fake/confirm-page.html?" +
`url=${encodeURIComponent(url)}` +
`&cookieStoreId=${activeTab.cookieStoreId}`,
`&cookieStoreId=${this.webExt.tab.cookieStoreId}`,
cookieStoreId: undefined,
openerTabId: null,
index: 2,
@ -42,29 +47,29 @@ describe("Assignment Feature", () => {
});
});
it("should remove the new Tab that got opened in the default container", () => {
background.browser.tabs.remove.should.have.been.calledWith(newTab.id);
it("should remove the new Tab that got opened in the default container", function () {
this.webExt.background.browser.tabs.remove.should.have.been.calledWith(newTab.id);
});
});
describe("click the 'Always open in' checkbox in the popup again", () => {
beforeEach(async () => {
describe("click the 'Always open in' checkbox in the popup again", function () {
beforeEach(async function () {
// popup click to remove assignment for activeTab.url
await helper.popup.clickElementById("container-page-assigned");
await this.webExt.popup.helper.clickElementById("container-page-assigned");
});
describe("open new Tab with the no longer assigned URL in the default container", () => {
beforeEach(async () => {
describe("open new Tab with the no longer assigned URL in the default container", function () {
beforeEach(async function () {
// new Tab opening activeTab.url in default container
await helper.browser.openNewTab({
await this.webExt.background.browser.tabs._create({
cookieStoreId: "firefox-default",
url
});
});
it("should not open the confirm page", async () => {
it("should not open the confirm page", async function () {
// should not have created a new tab
background.browser.tabs.create.should.not.have.been.called;
this.webExt.background.browser.tabs.create.should.not.have.been.called;
});
});
});

View file

@ -1,27 +1,33 @@
describe("Containers Management", () => {
beforeEach(async () => {
await helper.browser.initializeWithTab();
const {initializeWithTab} = require("../common");
describe("Containers Management", function () {
beforeEach(async function () {
this.webExt = await initializeWithTab();
});
describe("creating a new container", () => {
beforeEach(async () => {
await helper.popup.clickElementById("container-add-link");
await helper.popup.clickElementById("edit-container-ok-link");
afterEach(function () {
this.webExt.destroy();
});
it("should create it in the browser as well", () => {
background.browser.contextualIdentities.create.should.have.been.calledOnce;
describe("creating a new container", function () {
beforeEach(async function () {
await this.webExt.popup.helper.clickElementById("container-add-link");
await this.webExt.popup.helper.clickElementById("edit-container-ok-link");
});
describe("removing it afterwards", () => {
beforeEach(async () => {
await helper.popup.clickElementById("edit-containers-link");
await helper.popup.clickLastMatchingElementByQuerySelector(".delete-container-icon");
await helper.popup.clickElementById("delete-container-ok-link");
it("should create it in the browser as well", function () {
this.webExt.background.browser.contextualIdentities.create.should.have.been.calledOnce;
});
it("should remove it in the browser as well", () => {
background.browser.contextualIdentities.remove.should.have.been.calledOnce;
describe("removing it afterwards", function () {
beforeEach(async function () {
await this.webExt.popup.helper.clickElementById("edit-containers-link");
await this.webExt.popup.helper.clickElementByQuerySelectorAll(".delete-container-icon", "last");
await this.webExt.popup.helper.clickElementById("delete-container-ok-link");
});
it("should remove it in the browser as well", function () {
this.webExt.background.browser.contextualIdentities.remove.should.have.been.calledOnce;
});
});
});

View file

@ -1,17 +1,24 @@
describe("External Webextensions", () => {
const {expect, initializeWithTab} = require("../common");
describe("External Webextensions", function () {
const url = "http://example.com";
beforeEach(async () => {
await helper.browser.initializeWithTab({
beforeEach(async function () {
this.webExt = await initializeWithTab({
cookieStoreId: "firefox-container-1",
url
});
await helper.popup.clickElementById("container-page-assigned");
await this.webExt.popup.helper.clickElementById("container-page-assigned");
});
describe("with contextualIdentities permissions", () => {
it("should be able to get assignments", async () => {
background.browser.management.get.resolves({
afterEach(function () {
this.webExt.destroy();
});
describe("with contextualIdentities permissions", function () {
it("should be able to get assignments", async function () {
this.webExt.background.browser.management.get.resolves({
permissions: ["contextualIdentities"]
});
@ -23,7 +30,7 @@ describe("External Webextensions", () => {
id: "external-webextension"
};
const [promise] = background.browser.runtime.onMessageExternal.addListener.yield(message, sender);
const [promise] = this.webExt.background.browser.runtime.onMessageExternal.addListener.yield(message, sender);
const answer = await promise;
expect(answer.userContextId === "1").to.be.true;
expect(answer.neverAsk === false).to.be.true;
@ -33,9 +40,9 @@ describe("External Webextensions", () => {
});
});
describe("without contextualIdentities permissions", () => {
it("should throw an error", async () => {
background.browser.management.get.resolves({
describe("without contextualIdentities permissions", function () {
it("should throw an error", async function () {
this.webExt.background.browser.management.get.resolves({
permissions: []
});
@ -47,7 +54,7 @@ describe("External Webextensions", () => {
id: "external-webextension"
};
const [promise] = background.browser.runtime.onMessageExternal.addListener.yield(message, sender);
const [promise] = this.webExt.background.browser.runtime.onMessageExternal.addListener.yield(message, sender);
return promise.catch(error => {
expect(error.message).to.equal("Missing contextualIdentities permission");
});

View file

@ -1,64 +0,0 @@
describe("Sync", () => {
it("should init sync on startup", async () => {
const tab = await helper.browser.initializeWithTab();
console.log(await background.browser.storage.local.get());
const mozContainer = await background.browser.contextualIdentities.create({
name: "Mozilla",
color: "red",
icon: "briefcase",
});
await background.browser.contextualIdentities.update("firefox-container-2", {color:"purple"});
await background.browser.contextualIdentities.update("firefox-container-4", {icon:"pet"});
await Promise.all([
{
userContextId: "1",
url: "https://twitter.com",
},
{
userContextId: "2",
url: "https://www.facebook.com",
},
{
userContextId: "4",
url: "https://www.linkedin.com",
neverAsk: true,
},
{
userContextId: mozContainer.cookieStoreId.replace("firefox-container-", ""),
url: "https://developer.mozilla.org",
neverAsk: true,
}
].map(async (assign) => {
await background.browser.tabs.update(tab.id, {
cookieStoreId: `firefox-container-${assign.userContextId}`
});
await background.browser.runtime.onMessage.addListener.yield({
method: "setOrRemoveAssignment",
tabId: tab.id,
url: assign.url,
userContextId: assign.userContextId,
value: !true
});
if (assign.neverAsk) {
await nextTick();
await background.browser.runtime.onMessage.addListener.yield({
method: "neverAsk",
neverAsk: true,
pageUrl: assign.url,
});
}
}));
// await background.browser.storage.onChanged.addListener.yield();
await nextTick();
const sync = await background.browser.storage.sync.get();
console.log("sync", sync);
// expect(sync.length).to.equal(4);
});
});

View file

@ -1,81 +0,0 @@
module.exports = {
browser: {
async initializeWithTab(details = {
cookieStoreId: "firefox-default"
}) {
let tab;
await buildDom({
background: {
async afterBuild(background) {
tab = await background.browser.tabs._create(details);
}
},
popup: {
jsdom: {
beforeParse(window) {
window.browser.storage.local.set({
"browserActionBadgesClicked": [],
"onboarding-stage": 6,
"achievements": [],
"syncEnabled": true
});
window.browser.storage.local.set.resetHistory();
window.browser.storage.sync.clear();
}
}
}
});
return tab;
},
async openNewTab(tab, options = {}) {
return background.browser.tabs._create(tab, options);
},
async initSyncTest(details = {}) {
if (!details.cookieStoreId) details.cookieStoreId = "firefox-default";
if (!details.localStorage) {
details.localStorage = {
"browserActionBadgesClicked": [],
"onboarding-stage": 6,
"achievements": [],
"syncEnabled": true
};
}
if (!details.syncStorage) details.syncStorage = {};
let tab;
await buildDom({
background: {
async afterBuild(background) {
tab = await background.browser.tabs._create({ cookieStoreId: details.cookieStoreId });
}
},
popup: {
jsdom: {
beforeParse(window) {
window.browser.storage.clear();
window.browser.storage.local.set(details.localStorage);
window.browser.storage.local.set.resetHistory();
window.browser.storage.sync.clear();
window.browser.storage.sync.set(details.syncStorage);
window.browser.storage.sync.set.resetHistory();
}
},
}
});
return tab;
},
},
popup: {
async clickElementById(id) {
await popup.helper.clickElementById(id);
},
async clickLastMatchingElementByQuerySelector(querySelector) {
await popup.helper.clickElementByQuerySelectorAll(querySelector, "last");
}
}
};

View file

@ -1,16 +1,19 @@
describe("#1168", () => {
describe("when navigation happens too slow after opening new tab to a page which then redirects", () => {
let clock, tab;
const {expect, sinon, initializeWithTab} = require("../common");
beforeEach(async () => {
await helper.browser.initializeWithTab({
describe("#1168", function () {
describe("when navigation happens too slow after opening new tab to a page which then redirects", function () {
let clock, tab, background;
beforeEach(async function () {
this.webExt = await initializeWithTab({
cookieStoreId: "firefox-container-1",
url: "https://bugzilla.mozilla.org"
});
await helper.popup.clickElementById("container-page-assigned");
await this.webExt.popup.helper.clickElementById("container-page-assigned");
clock = sinon.useFakeTimers();
tab = await helper.browser.openNewTab({});
tab = await this.webExt.browser.tabs._create({});
clock.tick(2000);
@ -20,15 +23,16 @@ describe("#1168", () => {
]);
});
afterEach(function () {
this.webExt.destroy();
clock.restore();
});
// Not solved yet
// See: https://github.com/mozilla/multi-account-containers/issues/1168#issuecomment-378394091
it.skip("should remove the old tab", async () => {
it.skip("should remove the old tab", async function () {
expect(background.browser.tabs.create).to.have.been.calledOnce;
expect(background.browser.tabs.remove).to.have.been.calledWith(tab.id);
});
afterEach(() => {
clock.restore();
});
});
});

View file

@ -1,15 +1,18 @@
describe("#940", () => {
describe("when other onBeforeRequestHandlers are faster and redirect with the same requestId", () => {
it("should not open two confirm pages", async () => {
await helper.browser.initializeWithTab({
const {expect, sinon, initializeWithTab} = require("../common");
describe("#940", function () {
describe("when other onBeforeRequestHandlers are faster and redirect with the same requestId", function () {
it("should not open two confirm pages", async function () {
const webExtension = await initializeWithTab({
cookieStoreId: "firefox-container-1",
url: "http://example.com"
});
await helper.popup.clickElementById("container-page-assigned");
await webExtension.popup.helper.clickElementById("container-page-assigned");
const responses = {};
await helper.browser.openNewTab({
url: "http://example.com"
await webExtension.background.browser.tabs._create({
url: "https://example.com"
}, {
options: {
webRequestRedirects: ["https://example.com"],
@ -23,16 +26,25 @@ describe("#940", () => {
expect(result).to.deep.equal({
cancel: true
});
background.browser.tabs.create.should.have.been.calledOnce;
webExtension.browser.tabs.create.should.have.been.calledOnce;
webExtension.destroy();
});
});
describe("when redirects change requestId midflight", () => {
let newTab;
const newTabResponses = {};
const redirectedRequest = async (options = {}) => {
describe("when redirects change requestId midflight", function () {
beforeEach(async function () {
this.webExt = await initializeWithTab({
cookieStoreId: "firefox-container-1",
url: "https://www.youtube.com"
});
await this.webExt.popup.helper.clickElementById("container-page-assigned");
global.clock = sinon.useFakeTimers();
newTab = await helper.browser.openNewTab({
this.redirectedRequest = async (options = {}) => {
const newTabResponses = {};
const newTab = await this.webExt.browser.tabs._create({
url: "http://youtube.com"
}, {
options: Object.assign({
@ -51,18 +63,18 @@ describe("#940", () => {
}, options),
responses: newTabResponses
});
return [newTabResponses, newTab];
};
beforeEach(async () => {
await helper.browser.initializeWithTab({
cookieStoreId: "firefox-container-1",
url: "https://www.youtube.com"
});
await helper.popup.clickElementById("container-page-assigned");
});
it("should not open two confirm pages", async () => {
await redirectedRequest();
afterEach(function () {
this.webExt.destroy();
global.clock.restore();
});
it("should not open two confirm pages", async function () {
const [newTabResponses] = await this.redirectedRequest();
// http://youtube.com is not assigned, no cancel, no reopening
expect(await newTabResponses.webRequest.onBeforeRequest[0]).to.deep.equal({});
@ -80,17 +92,17 @@ describe("#940", () => {
cancel: true
});
background.browser.tabs.create.should.have.been.calledOnce;
this.webExt.background.browser.tabs.create.should.have.been.calledOnce;
});
it("should uncancel after webRequest.onCompleted", async () => {
await redirectedRequest();
it("should uncancel after webRequest.onCompleted", async function () {
const [newTabResponses, newTab] = await this.redirectedRequest();
// remove onCompleted listeners because in the real world this request would never complete
// and thus might trigger unexpected behavior because the tab gets removed when reopening
background.browser.webRequest.onCompleted.addListener = sinon.stub();
background.browser.tabs.create.resetHistory();
this.webExt.background.browser.webRequest.onCompleted.addListener = sinon.stub();
this.webExt.background.browser.tabs.create.resetHistory();
// we create a tab with the same id and use the same request id to see if uncanceled
await helper.browser.openNewTab({
await this.webExt.browser.tabs._create({
id: newTab.id,
url: "https://www.youtube.com"
}, {
@ -101,14 +113,14 @@ describe("#940", () => {
}
});
background.browser.tabs.create.should.have.been.calledOnce;
this.webExt.background.browser.tabs.create.should.have.been.calledOnce;
});
it("should uncancel after webRequest.onErrorOccurred", async () => {
await redirectedRequest();
background.browser.tabs.create.resetHistory();
it("should uncancel after webRequest.onErrorOccurred", async function () {
const [newTabResponses, newTab] = await this.redirectedRequest();
this.webExt.background.browser.tabs.create.resetHistory();
// we create a tab with the same id and use the same request id to see if uncanceled
await helper.browser.openNewTab({
await this.webExt.browser.tabs._create({
id: newTab.id,
url: "https://www.youtube.com"
}, {
@ -120,18 +132,18 @@ describe("#940", () => {
}
});
background.browser.tabs.create.should.have.been.calledOnce;
this.webExt.background.browser.tabs.create.should.have.been.calledOnce;
});
it("should uncancel after 2 seconds", async () => {
await redirectedRequest({
it("should uncancel after 2 seconds", async function () {
const [newTabResponses, newTab] = await this.redirectedRequest({
webRequestDontYield: ["onCompleted", "onErrorOccurred"]
});
global.clock.tick(2000);
background.browser.tabs.create.resetHistory();
this.webExt.background.browser.tabs.create.resetHistory();
// we create a tab with the same id and use the same request id to see if uncanceled
await helper.browser.openNewTab({
await this.webExt.browser.tabs._create({
id: newTab.id,
url: "https://www.youtube.com"
}, {
@ -143,13 +155,13 @@ describe("#940", () => {
}
});
background.browser.tabs.create.should.have.been.calledOnce;
this.webExt.background.browser.tabs.create.should.have.been.calledOnce;
});
it("should not influence the canceled url in other tabs", async () => {
await redirectedRequest();
background.browser.tabs.create.resetHistory();
await helper.browser.openNewTab({
it("should not influence the canceled url in other tabs", async function () {
await this.redirectedRequest();
this.webExt.background.browser.tabs.create.resetHistory();
await this.webExt.browser.tabs._create({
cookieStoreId: "firefox-default",
url: "https://www.youtube.com"
}, {
@ -158,11 +170,7 @@ describe("#940", () => {
}
});
background.browser.tabs.create.should.have.been.calledOnce;
});
afterEach(() => {
global.clock.restore();
this.webExt.background.browser.tabs.create.should.have.been.calledOnce;
});
});
});