Bug 1291056 - New console frontend: Use require-hacker to load mocks in npm tests. r=bgrins
authorLin Clark <lclark@mozilla.com>
Thu, 18 Aug 2016 16:04:30 -0700
changeset 309884 7e2ef1710b5442c06a37a637d29e532ce94b8d3a
parent 309883 0e2c6e417490bdf644fd4faf93d842099b8c56c8
child 309885 daecdd63eb25d2a09b2315bb525e8789c1823005
push id20341
push userbgrinstead@mozilla.com
push dateThu, 18 Aug 2016 23:06:20 +0000
treeherderfx-team@7bf7d0d47b79 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbgrins
bugs1291056
milestone51.0a1
Bug 1291056 - New console frontend: Use require-hacker to load mocks in npm tests. r=bgrins MozReview-Commit-ID: 8ndzzDqCRTB
devtools/client/webconsole/new-console-output/store.js
devtools/client/webconsole/new-console-output/test/components/filter-bar.test.js
devtools/client/webconsole/new-console-output/test/components/filter-button.test.js
devtools/client/webconsole/new-console-output/test/fixtures/L10n.js
devtools/client/webconsole/new-console-output/test/fixtures/WebConsoleUtils.js
devtools/client/webconsole/new-console-output/test/fixtures/l10n.js
devtools/client/webconsole/new-console-output/test/helpers.js
devtools/client/webconsole/new-console-output/test/requireHelper.js
devtools/client/webconsole/new-console-output/utils/messages.js
devtools/client/webconsole/package.json
--- a/devtools/client/webconsole/new-console-output/store.js
+++ b/devtools/client/webconsole/new-console-output/store.js
@@ -2,22 +2,19 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 "use strict";
 
 const {FilterState} = require("devtools/client/webconsole/new-console-output/reducers/filters");
 const {PrefState} = require("devtools/client/webconsole/new-console-output/reducers/prefs");
 const { combineReducers, createStore } = require("devtools/client/shared/vendor/redux");
 const { reducers } = require("./reducers/index");
+const Services = require("Services");
 
-function configureStore(Services) {
-  if (!Services) {
-    Services = require("Services");
-  }
-
+function configureStore() {
   const initialState = {
     prefs: new PrefState({
       logLimit: Math.max(Services.prefs.getIntPref("devtools.hud.loglimit"), 1),
     }),
     filters: new FilterState({
       error: Services.prefs.getBoolPref("devtools.webconsole.filter.error"),
       warn: Services.prefs.getBoolPref("devtools.webconsole.filter.warn"),
       info: Services.prefs.getBoolPref("devtools.webconsole.filter.info"),
--- a/devtools/client/webconsole/new-console-output/test/components/filter-bar.test.js
+++ b/devtools/client/webconsole/new-console-output/test/components/filter-bar.test.js
@@ -1,32 +1,29 @@
 /* Any copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/ */
 "use strict";
 
-// Require helper is necessary to load certain modules.
-require("devtools/client/webconsole/new-console-output/test/requireHelper")();
+const expect = require("expect");
+const sinon = require("sinon");
 const { render, mount } = require("enzyme");
 
 const { createFactory } = require("devtools/client/shared/vendor/react");
 const Provider = createFactory(require("react-redux").Provider);
 
 const FilterButton = createFactory(require("devtools/client/webconsole/new-console-output/components/filter-button").FilterButton);
 const FilterBar = createFactory(require("devtools/client/webconsole/new-console-output/components/filter-bar"));
 const { getAllUi } = require("devtools/client/webconsole/new-console-output/selectors/ui");
 const {
   MESSAGES_CLEAR,
   MESSAGE_LEVEL
 } = require("devtools/client/webconsole/new-console-output/constants");
 
 const { setupStore } = require("devtools/client/webconsole/new-console-output/test/helpers");
 
-const expect = require("expect");
-const sinon = require("sinon");
-
 describe("FilterBar component:", () => {
   it("initial render", () => {
     const store = setupStore([]);
 
     const wrapper = render(Provider({store}, FilterBar({})));
     const toolbar = wrapper.find(
       ".devtools-toolbar.webconsole-filterbar-primary"
     );
--- a/devtools/client/webconsole/new-console-output/test/components/filter-button.test.js
+++ b/devtools/client/webconsole/new-console-output/test/components/filter-button.test.js
@@ -1,27 +1,24 @@
 /* Any copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/ */
 "use strict";
 
-// Require helper is necessary to load certain modules.
-require("devtools/client/webconsole/new-console-output/test/requireHelper")();
+const expect = require("expect");
+const sinon = require("sinon");
 const { render, shallow } = require("enzyme");
 
 const { createFactory } = require("devtools/client/shared/vendor/react");
 
 const FilterButton = createFactory(require("devtools/client/webconsole/new-console-output/components/filter-button").FilterButton);
 const {
   FILTER_TOGGLE,
   MESSAGE_LEVEL
 } = require("devtools/client/webconsole/new-console-output/constants");
 
-const expect = require("expect");
-const sinon = require("sinon");
-
 describe("FilterButton component:", () => {
   const props = {
     active: true,
     label: "Error",
     filterKey: MESSAGE_LEVEL.ERROR,
     dispatch: sinon.spy()
   };
 
rename from devtools/client/webconsole/new-console-output/test/fixtures/l10n.js
rename to devtools/client/webconsole/new-console-output/test/fixtures/L10n.js
--- a/devtools/client/webconsole/new-console-output/test/fixtures/l10n.js
+++ b/devtools/client/webconsole/new-console-output/test/fixtures/L10n.js
@@ -1,15 +1,17 @@
 /* Any copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/ */
 
 "use strict";
 
 // @TODO Load the actual strings from webconsole.properties instead.
-module.exports = {
-  getStr: str => {
+class L10n {
+  getStr(str) {
     switch (str) {
       case "severity.error":
         return "Error";
     }
     return str;
   }
-};
+}
+
+module.exports = L10n;
new file mode 100644
--- /dev/null
+++ b/devtools/client/webconsole/new-console-output/test/fixtures/WebConsoleUtils.js
@@ -0,0 +1,14 @@
+/* Any copyright is dedicated to the Public Domain.
+   http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+const L10n = require("devtools/client/webconsole/new-console-output/test/fixtures/l10n");
+
+const Utils = {
+  L10n
+};
+
+module.exports = {
+  Utils
+};
--- a/devtools/client/webconsole/new-console-output/test/helpers.js
+++ b/devtools/client/webconsole/new-console-output/test/helpers.js
@@ -1,24 +1,21 @@
 /* Any copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/ */
 
 "use strict";
 
-require("devtools/client/webconsole/new-console-output/test/requireHelper")();
-
 let ReactDOM = require("devtools/client/shared/vendor/react-dom");
 let React = require("devtools/client/shared/vendor/react");
 var TestUtils = React.addons.TestUtils;
 
 const actions = require("devtools/client/webconsole/new-console-output/actions/messages");
 const { configureStore } = require("devtools/client/webconsole/new-console-output/store");
 const { IdGenerator } = require("devtools/client/webconsole/new-console-output/utils/id-generator");
 const { stubConsoleMessages } = require("devtools/client/webconsole/new-console-output/test/fixtures/stubs");
-const Services = require("devtools/client/webconsole/new-console-output/test/fixtures/Services");
 
 /**
  * Prepare actions for use in testing.
  */
 function setupActions() {
   // Some actions use dependency injection. This helps them avoid using state in
   // a hard-to-test way. We need to inject stubbed versions of these dependencies.
   const wrappedActions = Object.assign({}, actions);
@@ -30,18 +27,17 @@ function setupActions() {
 
   return wrappedActions;
 }
 
 /**
  * Prepare the store for use in testing.
  */
 function setupStore(input) {
-  // Inject the Services stub.
-  const store = configureStore(Services);
+  const store = configureStore();
 
   // Add the messages from the input commands to the store.
   input.forEach((cmd) => {
     store.dispatch(actions.messageAdd(stubConsoleMessages.get(cmd)));
   });
 
   return store;
 }
--- a/devtools/client/webconsole/new-console-output/test/requireHelper.js
+++ b/devtools/client/webconsole/new-console-output/test/requireHelper.js
@@ -1,26 +1,32 @@
 /* Any copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/ */
 "use strict";
 
 const requireHacker = require("require-hacker");
 
-module.exports = () => {
-  try {
-    requireHacker.global_hook("default", path => {
-      switch (path) {
-        case "react-dom/server":
-          return `const React = require('react-dev'); module.exports = React`;
-        case "react-addons-test-utils":
-          return `const React = require('react-dev'); module.exports = React.addons.TestUtils`;
-        case "react":
-          return `const React = require('react-dev'); module.exports = React`;
-        case "devtools/client/shared/vendor/react":
-          return `const React = require('react-dev'); module.exports = React`;
-        case "devtools/client/shared/vendor/react.default":
-          return `const React = require('react-dev'); module.exports = React`;
-      }
-    });
-  } catch (e) {
-    // Do nothing. This means the hook is already registered.
+requireHacker.global_hook("default", path => {
+  switch (path) {
+    // For Enzyme
+    case "react-dom/server":
+      return `const React = require('react-dev'); module.exports = React`;
+    case "react-addons-test-utils":
+      return `const React = require('react-dev'); module.exports = React.addons.TestUtils`;
+    // Use react-dev. This would be handled by browserLoader in Firefox.
+    case "react":
+    case "devtools/client/shared/vendor/react":
+      return `const React = require('react-dev'); module.exports = React`;
+    // For Rep's use of AMD
+    case "devtools/client/shared/vendor/react.default":
+      return `const React = require('react-dev'); module.exports = React`;
   }
-};
+
+  // Some modules depend on Chrome APIs which don't work in mocha. When such a module
+  // is required, replace it with a mock version.
+  switch (path) {
+    case "devtools/client/webconsole/utils":
+      return `module.exports = require("devtools/client/webconsole/new-console-output/test/fixtures/WebConsoleUtils")`;
+    case "Services":
+    case "Services.default":
+      return `module.exports = require("devtools/client/webconsole/new-console-output/test/fixtures/Services")`;
+  }
+});
--- a/devtools/client/webconsole/new-console-output/utils/messages.js
+++ b/devtools/client/webconsole/new-console-output/utils/messages.js
@@ -1,24 +1,19 @@
 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
 /* vim: set ft=javascript ts=2 et sw=2 tw=80: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 "use strict";
 
-let l10n;
-try {
-  const WebConsoleUtils = require("devtools/shared/webconsole/utils").Utils;
-  const STRINGS_URI = "chrome://devtools/locale/webconsole.properties";
-  l10n = new WebConsoleUtils.L10n(STRINGS_URI);
-} catch (e) {
-  l10n = require("devtools/client/webconsole/new-console-output/test/fixtures/l10n");
-}
+const WebConsoleUtils = require("devtools/client/webconsole/utils").Utils;
+const STRINGS_URI = "chrome://devtools/locale/webconsole.properties";
+const l10n = new WebConsoleUtils.L10n(STRINGS_URI);
 
 const {
   MESSAGE_SOURCE,
   MESSAGE_TYPE,
   MESSAGE_LEVEL,
   // Legacy
   CATEGORY_JS,
   CATEGORY_OUTPUT,
--- a/devtools/client/webconsole/package.json
+++ b/devtools/client/webconsole/package.json
@@ -9,11 +9,11 @@
     "expect": "^1.16.0",
     "jsdom": "^9.4.1",
     "jsdom-global": "^2.0.0",
     "mocha": "^2.5.3",
     "require-hacker": "^2.1.4",
     "sinon": "^1.17.5"
   },
   "scripts": {
-    "test": "NODE_PATH=`pwd`/../../../:`pwd`/../../../devtools/client/shared/vendor/ mocha new-console-output/test/**/*.test.js --compilers js:babel-register -r jsdom-global/register"
+    "test": "NODE_PATH=`pwd`/../../../:`pwd`/../../../devtools/client/shared/vendor/ mocha new-console-output/test/**/*.test.js --compilers js:babel-register -r jsdom-global/register -r ./new-console-output/test/requireHelper.js"
   }
 }