summaryrefslogtreecommitdiffstats
path: root/js/xpconnect/tests/unit/test_import_es6_modules.js
blob: 30b4edab9fa5ac16ea1821ca961fa912c379ac73 (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

add_task(async function() {
  // Test basic import.
  let ns = ChromeUtils.importESModule("resource://test/es6module.js");
  Assert.equal(ns.loadCount, 1);
  Assert.equal(ns.value, 2);

  // Test re-import of the same module.
  let ns2 = ChromeUtils.importESModule("resource://test/es6module.js");
  Assert.equal(ns.loadCount, 1);
  Assert.equal(ns, ns2);

  // Test imports with absolute and relative URIs return the same thing.
  let ns3 = ChromeUtils.importESModule("resource://test/es6module_absolute.js");
  let ns4 = ChromeUtils.importESModule("resource://test/es6module_absolute2.js");
  Assert.ok(ns3.absoluteX === ns3.relativeX);
  Assert.ok(ns3.absoluteX === ns4.x);

  // Test load failure.
  testFailure("resource://test/es6module_not_found.js", {
    type: "Error",
    message: "Failed to load resource://test/es6module_not_found.js",
    fileName: "test_import_es6_modules.js",
    stack: "testFailure",
    lineNumber: "*",
    columnNumber: "*",
    result: Cr.NS_ERROR_FILE_NOT_FOUND,
  });

  // Test load failure in import.
  testFailure("resource://test/es6module_missing_import.js", {
    type: "Error",
    message: "Failed to load resource://test/es6module_not_found2.js",
    fileName: "test_import_es6_modules.js",
    stack: "testFailure",
    lineNumber: "*",
    columnNumber: "*",
    result: Cr.NS_ERROR_FILE_NOT_FOUND,
  });

  // Test parse error.
  testFailure("resource://test/es6module_parse_error.js", {
    type: "SyntaxError",
    fileName: "resource://test/es6module_parse_error.js",
    stack: "testFailure",
    lineNumber: 1,
    columnNumber: 6,
  });

  // Test parse error in import.
  testFailure("resource://test/es6module_parse_error_in_import.js", {
    type: "SyntaxError",
    fileName: "resource://test/es6module_parse_error.js",
    stack: "testFailure",
    lineNumber: 1,
    columnNumber: 6,
  });

  // Test import error.
  testFailure("resource://test/es6module_import_error.js", {
    type: "SyntaxError",
    fileName: "resource://test/es6module_import_error.js",
    lineNumber: 1,
    columnNumber: 10,
  });

  // Test execution failure.
  let exception1 = testFailure("resource://test/es6module_throws.js", {
    type: "Error",
    message: "foobar",
    stack: "throwFunction",
    fileName: "resource://test/es6module_throws.js",
    lineNumber: 2,
    columnNumber: 9,
  });

  // Test re-import throws the same exception.
  let exception2 = testFailure("resource://test/es6module_throws.js", {
    type: "Error",
    message: "foobar",
    stack: "throwFunction",
    fileName: "resource://test/es6module_throws.js",
    lineNumber: 2,
    columnNumber: 9,
  });
  Assert.ok(exception1 === exception2);

  // Test loading cyclic module graph.
  ns = ChromeUtils.importESModule("resource://test/es6module_cycle_a.js");
  Assert.ok(ns.loaded);
  Assert.equal(ns.getValueFromB(), "b");
  ns = ChromeUtils.importESModule("resource://test/es6module_cycle_b.js");
  Assert.ok(ns.loaded);
  Assert.equal(ns.getValueFromC(), "c");
  ns = ChromeUtils.importESModule("resource://test/es6module_cycle_c.js");
  Assert.ok(ns.loaded);
  Assert.equal(ns.getValueFromA(), "a");

  // Test top-level await is not supported.
  testFailure("resource://test/es6module_top_level_await.js", {
    type: "SyntaxError",
    message: "not supported",
    stack: "testFailure",
    fileName: "resource://test/es6module_top_level_await.js",
    lineNumber: 1,
    columnNumber: 1,
  });
});

add_task(async function testDynamicImport() {
  // Dynamic import while and after evaluating top-level script.
  let ns = ChromeUtils.importESModule("resource://test/es6module_dynamic_import.js");
  let ns2 = await ns.result;
  Assert.equal(ns2.x, 10);

  ns2 = await ns.doImport();
  Assert.equal(ns2.y, 20);

  // Dynamic import for statically imported module.
  Assert.equal(ns.callGetCounter(), 1);
  ns.callSetCounter(5);
  Assert.equal(ns.callGetCounter(), 5);

  const { getCounter, setCounter } = await ns.doImportStatic();
  Assert.equal(getCounter(), 5);
  setCounter(8);
  Assert.equal(getCounter(), 8);
  Assert.equal(ns.callGetCounter(), 8);

  // Dynamic import for missing file.
  ns = ChromeUtils.importESModule("resource://test/es6module_dynamic_import_missing.js");
  let e = await ns.result;
  checkException(e, {
    type: "TypeError",
    message: "error loading dynamically imported",
    fileName: "resource://test/es6module_dynamic_import_missing.js",
    lineNumber: 5,
    columnNumber: 1,
  });

  e = await ns.doImport();
  checkException(e, {
    type: "TypeError",
    message: "error loading dynamically imported",
    fileName: "resource://test/es6module_dynamic_import_missing.js",
    lineNumber: 11,
    columnNumber: 5,
  });

  // Syntax error in dynamic import.
  ns = ChromeUtils.importESModule("resource://test/es6module_dynamic_import_syntax_error.js");
  e = await ns.result;
  checkException(e, {
    type: "SyntaxError",
    message: "unexpected token",
    fileName: "resource://test/es6module_dynamic_import_syntax_error2.js",
    lineNumber: 1,
    columnNumber: 3,
  });

  e = await ns.doImport();
  checkException(e, {
    type: "SyntaxError",
    message: "unexpected token",
    fileName: "resource://test/es6module_dynamic_import_syntax_error3.js",
    lineNumber: 1,
    columnNumber: 4,
  });

  // Runtime error in dynamic import.
  ns = ChromeUtils.importESModule("resource://test/es6module_dynamic_import_runtime_error.js");
  e = await ns.result;
  checkException(e, {
    type: "ReferenceError",
    message: "foo is not defined",
    fileName: "resource://test/es6module_dynamic_import_runtime_error2.js",
    lineNumber: 2,
    columnNumber: 1,
  });

  e = await ns.doImport();
  checkException(e, {
    type: "ReferenceError",
    message: "bar is not defined",
    fileName: "resource://test/es6module_dynamic_import_runtime_error3.js",
    lineNumber: 2,
    columnNumber: 1,
  });
});

function testFailure(url, expected) {
  let threw = false;
  let exception;
  let importLine, importColumn;
  try {
    // Get the line/column for ChromeUtils.importESModule.
    // lineNumber/columnNumber value with "*" in `expected` points the
    // line/column.
    let e = new Error();
    importLine = e.lineNumber + 3;
    importColumn = 17;
    ChromeUtils.importESModule(url);
  } catch (e) {
    threw = true;
    exception = e;
  }

  Assert.ok(threw, "Error should be thrown");

  checkException(exception, expected, importLine, importColumn);

  return exception;
}

function checkException(exception, expected, importLine, importColumn) {
  if ("type" in expected) {
    Assert.equal(exception.constructor.name, expected.type, "error type");
  }
  if ("message" in expected) {
    Assert.ok(exception.message.includes(expected.message),
              `Message "${exception.message}" should contain "${expected.message}"`);
  }
  if ("stack" in expected) {
    Assert.ok(exception.stack.includes(expected.stack),
              `Stack "${exception.stack}" should contain "${expected.stack}"`);
  }
  if ("fileName" in expected) {
    Assert.ok(exception.fileName.includes(expected.fileName),
              `fileName "${exception.fileName}" should contain "${expected.fileName}"`);
  }
  if ("lineNumber" in expected) {
    let expectedLine = expected.lineNumber;
    if (expectedLine === "*") {
      expectedLine = importLine;
    }
    Assert.equal(exception.lineNumber, expectedLine, "lineNumber");
  }
  if ("columnNumber" in expected) {
    let expectedColumn = expected.columnNumber;
    if (expectedColumn === "*") {
      expectedColumn = importColumn;
    }
    Assert.equal(exception.columnNumber, expectedColumn, "columnNumber");
  }
  if ("result" in expected) {
    Assert.equal(exception.result, expected.result, "result");
  }
}