From eb05b45b70d1704c59b6808f4c78968b578c4182 Mon Sep 17 00:00:00 2001 From: Pierre Ossman Date: Thu, 23 Jan 2020 14:27:37 +0100 Subject: [PATCH] Make afterEach() hooks work when skipping tests Mocha will now run afterEach() hooks when tests are skipped, so we need to make them more robust against things being partially set up. --- tests/test.helper.js | 8 ++++++-- tests/test.keyboard.js | 24 ++++++++++++++++++------ tests/test.localization.js | 4 +++- tests/test.webutil.js | 4 +++- 4 files changed, 30 insertions(+), 10 deletions(-) diff --git a/tests/test.helper.js b/tests/test.helper.js index 02328197..b1f438ee 100644 --- a/tests/test.helper.js +++ b/tests/test.helper.js @@ -86,7 +86,9 @@ describe('Helpers', function () { window.navigator.platform = "Mac x86_64"; }); afterEach(function () { - Object.defineProperty(window, "navigator", origNavigator); + if (origNavigator !== undefined) { + Object.defineProperty(window, "navigator", origNavigator); + } }); it('should respect ContextMenu on modern browser', function () { @@ -150,7 +152,9 @@ describe('Helpers', function () { } }); afterEach(function () { - Object.defineProperty(window, "navigator", origNavigator); + if (origNavigator !== undefined) { + Object.defineProperty(window, "navigator", origNavigator); + } }); it('should ignore printable character key on IE', function () { diff --git a/tests/test.keyboard.js b/tests/test.keyboard.js index b49312dd..3571d39c 100644 --- a/tests/test.keyboard.js +++ b/tests/test.keyboard.js @@ -272,7 +272,9 @@ describe('Key Event Handling', function () { window.navigator.platform = "Mac x86_64"; }); afterEach(function () { - Object.defineProperty(window, "navigator", origNavigator); + if (origNavigator !== undefined) { + Object.defineProperty(window, "navigator", origNavigator); + } }); it('should change Alt to AltGraph', function () { @@ -336,7 +338,9 @@ describe('Key Event Handling', function () { }); afterEach(function () { - Object.defineProperty(window, "navigator", origNavigator); + if (origNavigator !== undefined) { + Object.defineProperty(window, "navigator", origNavigator); + } }); it('should toggle caps lock on key press on iOS', function (done) { @@ -413,8 +417,12 @@ describe('Key Event Handling', function () { this.clock = sinon.useFakeTimers(); }); afterEach(function () { - Object.defineProperty(window, "navigator", origNavigator); - this.clock.restore(); + if (origNavigator !== undefined) { + Object.defineProperty(window, "navigator", origNavigator); + } + if (this.clock !== undefined) { + this.clock.restore(); + } }); it('should supress ControlLeft until it knows if it is AltGr', function () { @@ -559,8 +567,12 @@ describe('Key Event Handling', function () { this.clock = sinon.useFakeTimers(); }); afterEach(function () { - Object.defineProperty(window, "navigator", origNavigator); - this.clock.restore(); + if (origNavigator !== undefined) { + Object.defineProperty(window, "navigator", origNavigator); + } + if (this.clock !== undefined) { + this.clock.restore(); + } }); it('should fake a left Shift keyup', function () { diff --git a/tests/test.localization.js b/tests/test.localization.js index 9570c179..301ab79f 100644 --- a/tests/test.localization.js +++ b/tests/test.localization.js @@ -27,7 +27,9 @@ describe('Localization', function () { window.navigator.languages = []; }); afterEach(function () { - Object.defineProperty(window, "navigator", origNavigator); + if (origNavigator !== undefined) { + Object.defineProperty(window, "navigator", origNavigator); + } }); it('should use English by default', function () { diff --git a/tests/test.webutil.js b/tests/test.webutil.js index 72e19421..2a166ee0 100644 --- a/tests/test.webutil.js +++ b/tests/test.webutil.js @@ -42,7 +42,9 @@ describe('WebUtil', function () { return WebUtil.initSettings(); }); afterEach(function () { - Object.defineProperty(window, "localStorage", origLocalStorage); + if (origLocalStorage !== undefined) { + Object.defineProperty(window, "localStorage", origLocalStorage); + } }); describe('writeSetting', function () {