summaryrefslogtreecommitdiffstats
path: root/devtools/client/webconsole/test/node/mocha-test-setup.js
blob: b620e047b9d6081dd53696799e964304e7aa806d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

/* eslint-env node */

"use strict";

require("@babel/register")({
  // by default everything is ignored
  ignore: [/node_modules/],
});

const mcRoot = `${__dirname}/../../../../../`;
const getModule = mcPath =>
  `module.exports = require("${(mcRoot + mcPath).replace(/\\/gi, "/")}");`;

const { pref } = require("devtools-services");
pref("devtools.debugger.remote-timeout", 10000);
pref("devtools.hud.loglimit", 10000);
pref("devtools.webconsole.filter.error", true);
pref("devtools.webconsole.filter.warn", true);
pref("devtools.webconsole.filter.info", true);
pref("devtools.webconsole.filter.log", true);
pref("devtools.webconsole.filter.debug", true);
pref("devtools.webconsole.filter.css", false);
pref("devtools.webconsole.filter.net", false);
pref("devtools.webconsole.filter.netxhr", false);
pref("devtools.webconsole.inputHistoryCount", 300);
pref("devtools.webconsole.persistlog", false);
pref("devtools.webconsole.timestampMessages", false);
pref("devtools.webconsole.sidebarToggle", true);
pref("devtools.webconsole.groupWarningMessages", false);
pref("devtools.webconsole.input.editor", false);
pref("devtools.webconsole.input.autocomplete", true);
pref("devtools.webconsole.input.eagerEvaluation", true);
pref("devtools.browserconsole.contentMessages", true);
pref("devtools.webconsole.input.editorWidth", 800);
pref("devtools.webconsole.input.editorOnboarding", true);
pref("devtools.webconsole.input.context", false);
pref("devtools.contenttoolbox.webconsole.input.context", false);

global.loader = {
  lazyServiceGetter: () => {},
  lazyGetter: (context, name, fn) => {
    try {
      global[name] = fn();
    } catch (_) {}
  },
  lazyRequireGetter: (context, names, path, destruct) => {
    const excluded = [
      "Debugger",
      "devtools/shared/event-emitter",
      "devtools/client/shared/autocomplete-popup",
      "devtools/client/framework/devtools",
      "devtools/client/shared/keycodes",
      "devtools/client/shared/sourceeditor/editor",
      "devtools/client/shared/telemetry",
      "devtools/client/shared/save-screenshot",
      "devtools/client/shared/focus",
    ];
    if (!excluded.includes(path)) {
      if (!Array.isArray(names)) {
        names = [names];
      }

      for (const name of names) {
        const module = require(path);
        global[name] = destruct ? module[name] : module;
      }
    }
  },
};

// Setting up globals used in some modules.
global.isWorker = false;
global.indexedDB = { open: () => ({}) };

// URLSearchParams was added to the global object in Node 10.0.0. To not cause any issue
// with prior versions, we add it to the global object if it is not defined there.
if (!global.URLSearchParams) {
  global.URLSearchParams = require("url").URLSearchParams;
}

if (!global.ResizeObserver) {
  global.ResizeObserver = class ResizeObserver {
    observe() {}
    unobserve() {}
    disconnect() {}
  };
}

// Mock ChromeUtils.
global.ChromeUtils = {
  import: () => {},
  defineModuleGetter: () => {},
};

global.define = function() {};

// Used for the HTMLTooltip component.
// And set "isSystemPrincipal: false" because can't support XUL element in node.
global.document.nodePrincipal = {
  isSystemPrincipal: false,
};

// Point to vendored-in files and mocks when needed.
const requireHacker = require("require-hacker");
requireHacker.global_hook("default", (path, module) => {
  const paths = {
    "acorn/acorn": () => getModule("devtools/shared/acorn/acorn"),

    // For Enzyme
    "react-dom": () => getModule("devtools/client/shared/vendor/react-dom"),
    "react-dom/server": () =>
      getModule("devtools/client/shared/vendor/react-dom-server"),
    "react-dom/test-utils": () =>
      getModule("devtools/client/shared/vendor/react-dom-test-utils-dev"),
    "react-redux": () => getModule("devtools/client/shared/vendor/react-redux"),
    // Use react-dev. This would be handled by browserLoader in Firefox.
    react: () => getModule("devtools/client/shared/vendor/react-dev"),
    "devtools/client/shared/vendor/react": () =>
      getModule("devtools/client/shared/vendor/react-dev"),
    "chrome://mochitests/content/browser/devtools/client/webconsole/test/browser/stub-generator-helpers": () =>
      getModule(
        "devtools/client/webconsole/test/browser/stub-generator-helpers"
      ),

    chrome: () =>
      `module.exports = { Cc: {}, Ci: {}, Cu: { now: () => {}}, components: {stack: {caller: ""}} }`,
    ChromeUtils: () =>
      `module.exports = { addProfilerMarker: () => {}, import: () => ({}) }`,
    // Some modules depend on Chrome APIs which don't work in mocha. When such a module
    // is required, replace it with a mock version.
    Services: () => `module.exports = require("devtools-services")`,
    "devtools/server/devtools-server": () =>
      `module.exports = {DevToolsServer: {}}`,
    "devtools/client/shared/components/SmartTrace": () =>
      "module.exports = () => null;",
    "devtools/client/netmonitor/src/components/TabboxPanel": () => "{}",
    "devtools/client/webconsole/utils/context-menu": () => "{}",
    "devtools/client/shared/telemetry": () => `module.exports = function() {
      this.recordEvent = () => {};
      this.getKeyedHistogramById = () => ({add: () => {}});
    }`,
    "devtools/shared/event-emitter": () =>
      `module.exports = require("devtools-modules/src/utils/event-emitter")`,
    "devtools/client/shared/unicode-url": () =>
      `module.exports = require("devtools-modules/src/unicode-url")`,
    "devtools/shared/DevToolsUtils": () =>
      getModule("devtools/client/webconsole/test/node/fixtures/DevToolsUtils"),
    "devtools/server/actors/reflow": () => "{}",
    "devtools/shared/layout/utils": () => "{getCurrentZoom = () => {}}",
    "resource://gre/modules/AppConstants.jsm": () => "module.exports = {};",
    "devtools/client/framework/devtools": () => `module.exports = {
      gDevTools: {}
    };`,
    "devtools/shared/async-storage": () =>
      getModule("devtools/client/webconsole/test/node/fixtures/async-storage"),
    "devtools/shared/generate-uuid": () =>
      getModule(
        "devtools/client/shared/test-helpers/jest-fixtures/generate-uuid"
      ),
  };

  if (paths.hasOwnProperty(path)) {
    return paths[path]();
  }

  // We need to rewrite all the modules assuming the root is mozilla-central and give them
  // an absolute path.
  if (path.startsWith("devtools/")) {
    return getModule(path);
  }

  return undefined;
});

// Configure enzyme with React 16 adapter. This needs to be done after we set the
// requireHack hook so `require()` calls in Enzyme are handled as well.
const Enzyme = require("enzyme");
const Adapter = require("enzyme-adapter-react-16");
Enzyme.configure({ adapter: new Adapter() });