summaryrefslogtreecommitdiffstats
path: root/toolkit/components/workerloader/tests/worker_test_loading.js
blob: 551c56d7d38015b1a1426eec7dd61590740c1928 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

/* eslint-env worker */

"use strict";

importScripts("utils_worker.js"); // Test suite code
info("Test suite configured");

/* import-globals-from /toolkit/components/workerloader/require.js */
importScripts("resource://gre/modules/workers/require.js");
info("Loader imported");

var PATH =
  "chrome://mochitests/content/chrome/toolkit/components/workerloader/tests/";
var tests = [];
var add_test = function (test) {
  tests.push(test);
};

add_test(function test_setup() {
  ok(typeof require != "undefined", "Function |require| is defined");
});

// Test simple loading (moduleA-depends.js requires moduleB-dependency.js)
add_test(function test_load() {
  let A = require(PATH + "moduleA-depends.js");
  ok(true, "Opened module A");

  is(A.A, true, "Module A exported value A");
  ok(!("B" in A), "Module A did not export value B");
  is(A.importedFoo, "foo", "Module A re-exported B.foo");

  // re-evaluating moduleB-dependency.js would cause an error, but re-requiring it shouldn't
  let B = require(PATH + "moduleB-dependency.js");
  ok(true, "Managed to re-require module B");
  is(B.B, true, "Module B exported value B");
  is(B.foo, "foo", "Module B exported value foo");
});

// Test simple circular loading (moduleC-circular.js and moduleD-circular.js require each other)
add_test(function test_circular() {
  let C = require(PATH + "moduleC-circular.js");
  ok(true, "Loaded circular modules C and D");
  is(
    C.copiedFromD.copiedFromC.enteredC,
    true,
    "Properties exported by C before requiring D can be seen by D immediately"
  );

  let D = require(PATH + "moduleD-circular.js");
  is(
    D.exportedFromC.finishedC,
    true,
    "Properties exported by C after requiring D can be seen by D eventually"
  );
});

// Testing error cases
add_test(function test_exceptions() {
  let should_throw = function (f) {
    try {
      f();
      return null;
    } catch (ex) {
      return ex;
    }
  };

  let exn = should_throw(() => require(PATH + "this module doesn't exist"));
  ok(!!exn, "Attempting to load a module that doesn't exist raises an error");

  exn = should_throw(() => require(PATH + "moduleE-throws-during-require.js"));
  ok(
    !!exn,
    "Attempting to load a module that throws at toplevel raises an error"
  );
  is(
    exn.moduleName,
    PATH + "moduleE-throws-during-require.js",
    "moduleName is correct"
  );
  isnot(
    exn.moduleStack.indexOf("moduleE-throws-during-require.js"),
    -1,
    "moduleStack contains the name of the module"
  );
  is(exn.lineNumber, 12, "The error comes with the right line number");

  exn = should_throw(() => require(PATH + "moduleF-syntaxerror.xml"));
  ok(!!exn, "Attempting to load a non-well formatted module raises an error");

  exn = should_throw(() => require(PATH + "moduleG-throws-later.js").doThrow());
  ok(!!exn, "G.doThrow() has raised an error");
  info(exn);
  ok(exn.toString().startsWith("TypeError"), "The exception is a TypeError.");
  is(
    exn.moduleName,
    PATH + "moduleG-throws-later.js",
    "The name of the module is correct"
  );
  isnot(
    exn.moduleStack.indexOf("moduleG-throws-later.js"),
    -1,
    "The name of the right file appears somewhere in the stack"
  );
  is(exn.lineNumber, 13, "The error comes with the right line number");
});

function get_exn(f) {
  try {
    f();
    return undefined;
  } catch (ex) {
    return ex;
  }
}

// Test module.exports
add_test(function test_module_dot_exports() {
  let H = require(PATH + "moduleH-module-dot-exports.js");
  is(H.key, "value", "module.exports worked");
  let H2 = require(PATH + "moduleH-module-dot-exports.js");
  is(H2.key, "value", "module.exports returned the same key");
  ok(H2 === H, "module.exports returned the same module the second time");
  let exn = get_exn(() => (H.key = "this should not be accepted"));
  ok(
    exn instanceof TypeError,
    "Cannot alter value in module.exports after export"
  );
  exn = get_exn(() => (H.key2 = "this should not be accepted, either"));
  ok(
    exn instanceof TypeError,
    "Cannot add value to module.exports after export"
  );
});

// Test relative imports (moduleI-depends.js requires moduleJ-dependency.js)
add_test(function test_load() {
  let I = require(PATH + "moduleI-depends.js");
  ok(true, "Opened module I");

  is(I.I, true, "Module I exported value I");
  ok(!("J" in I), "Module I did not export value J");
  is(I.importedFoo, "foo", "Module I re-exported J.foo");

  // re-evaluating moduleJ-dependency.js would cause an error, but re-requiring it shouldn't
  let J = require(PATH + "moduleJ-dependency.js");
  ok(true, "Managed to re-require module J");
  is(J.J, true, "Module J exported value J");
  is(J.foo, "foo", "Module J exported value foo");
});

self.onmessage = function (message) {
  for (let test of tests) {
    info("Entering " + test.name);
    try {
      test();
    } catch (ex) {
      ok(false, "Test " + test.name + " failed");
      info(ex);
      info(ex.stack);
    }
    info("Leaving " + test.name);
  }
  finish();
};