summaryrefslogtreecommitdiffstats
path: root/js/xpconnect/tests/unit/test_import_es6_modules.js
blob: 9b5659871f257b7fb3a6ba7ab32aff699c48390c (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
/* 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: 5,
  });

  // 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: 5,
  });

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

  // 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: 0,
  });

  // Test dynamic import is not supported.
  ns = ChromeUtils.importESModule("resource://test/es6module_dynamic_import.js");
  const e = await ns.result;
  checkException(e, {
    type: "TypeError",
    message: "not supported",
    fileName: "resource://test/es6module_dynamic_import.js",
    lineNumber: 5,
    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");
  }
}