module-bugfixes.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. // ES6 Module tests for bugfixes
  6. WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
  7. function testModuleScript(source, message, shouldFail = false) {
  8. let testfunc = () => testRunner.LoadModule(source, 'samethread', shouldFail);
  9. if (shouldFail) {
  10. let caught = false;
  11. // We can't use assert.throws here because the SyntaxError used to construct the thrown error
  12. // is from a different context so it won't be strictly equal to our SyntaxError.
  13. try {
  14. testfunc();
  15. } catch(e) {
  16. caught = true;
  17. // Compare toString output of SyntaxError and other context SyntaxError constructor.
  18. assert.areEqual(e.constructor.toString(), SyntaxError.toString(), message);
  19. }
  20. assert.isTrue(caught, `Expected error not thrown: ${message}`);
  21. } else {
  22. assert.doesNotThrow(testfunc, message);
  23. }
  24. }
  25. var tests = [
  26. {
  27. name: "OS12113549: Assertion on module export in ProcessCapturedSym",
  28. body: function() {
  29. let functionBody =
  30. `
  31. import { module1_exportbinding_0 as module2_localbinding_0 } from 'bug_OS12113549_module1.js';
  32. assert.areEqual({"BugID": "OS12113549"}, module2_localbinding_0);
  33. `
  34. testRunner.LoadModule(functionBody, 'samethread');
  35. }
  36. },
  37. {
  38. name: "Memory leak test on syntax error",
  39. body: function() {
  40. try {
  41. WScript.LoadModule('');
  42. WScript.LoadModule('1');
  43. WScript.LoadModule('const a = () -> {};');
  44. } catch(e) {
  45. // no-op
  46. }
  47. }
  48. },
  49. {
  50. name: "Issue 4482: Indirect circular module dependencies",
  51. body: function() {
  52. let functionBody = "import 'module_4482_dep1.js';"
  53. testRunner.LoadModule(functionBody);
  54. }
  55. },
  56. {
  57. name: "Issue 4570: Module that appears multiple times in dependency tree",
  58. body: function() {
  59. let functionBody = "import 'module_4570_dep1.js';"
  60. testRunner.LoadModule(functionBody);
  61. }
  62. },
  63. {
  64. name: "Issue 5171: Incorrect module load order",
  65. body: function() {
  66. WScript.RegisterModuleSource("obj.js", `export const obj = {a:false, b: false, c: false};`);
  67. WScript.RegisterModuleSource("a.js",`
  68. import {obj} from "obj.js";
  69. assert.isTrue(obj.b);
  70. assert.isFalse(obj.c);
  71. assert.isFalse(obj.a);
  72. obj.a = true;`);
  73. WScript.RegisterModuleSource("b.js",`
  74. import {obj} from "obj.js";
  75. assert.isFalse(obj.b);
  76. assert.isFalse(obj.c);
  77. assert.isFalse(obj.a);
  78. obj.b = true;`);
  79. WScript.RegisterModuleSource("c.js",`
  80. import {obj} from "obj.js";
  81. assert.isTrue(obj.b);
  82. assert.isFalse(obj.c);
  83. assert.isTrue(obj.a);
  84. obj.c = true;`);
  85. const start = 'import "b.js"; import "a.js"; import "c.js";';
  86. testRunner.LoadModule(start);
  87. }
  88. },
  89. {
  90. name: "Issue 6261: Specifier dependent module load order",
  91. body: function() {
  92. WScript.RegisterModuleSource("dep", `export const obj = {a:false, b: false, c: false};`);
  93. WScript.RegisterModuleSource("one",`
  94. import {obj} from "dep";
  95. assert.isFalse(obj.b);
  96. assert.isFalse(obj.c);
  97. assert.isFalse(obj.a);
  98. obj.a = true;`);
  99. WScript.RegisterModuleSource("two",`
  100. import {obj} from "dep";
  101. assert.isFalse(obj.b);
  102. assert.isFalse(obj.c);
  103. assert.isTrue(obj.a);
  104. obj.b = true;`);
  105. WScript.RegisterModuleSource("three",`
  106. import {obj} from "dep";
  107. assert.isTrue(obj.b);
  108. assert.isFalse(obj.c);
  109. assert.isTrue(obj.a);
  110. obj.c = true;`);
  111. const start = 'import "one"; import "two"; import "three";';
  112. testRunner.LoadModule(start);
  113. }
  114. },
  115. {
  116. // https://github.com/Microsoft/ChakraCore/issues/5501
  117. name : "Issue 5501: Indirect exports excluded from namespace object - POC 1",
  118. body()
  119. {
  120. WScript.RegisterModuleSource("test5501Part1", `
  121. export {bar as foo} from "test5501Part1";
  122. export const bar = 5;
  123. import * as stuff from "test5501Part1";
  124. assert.areEqual(stuff.bar, stuff.foo);
  125. `);
  126. testRunner.LoadModule("import 'test5501Part1'");
  127. }
  128. },
  129. {
  130. name : "Issue 5501: Indirect exports excluded from namespace object - POC 2",
  131. body()
  132. {
  133. WScript.RegisterModuleSource("test5501Part2a", "export default function () { return true; }");
  134. WScript.RegisterModuleSource("test5501Part2b", "export {default as aliasName} from 'test5501Part2a'");
  135. testRunner.LoadModule(`
  136. import {aliasName} from 'test5501Part2b';
  137. assert.isTrue(aliasName());
  138. `);
  139. }
  140. },
  141. {
  142. name : "Issue 6133: Child module imports non-existant export from another module",
  143. body()
  144. {
  145. WScript.RegisterModuleSource("test6133a", 'import Default from "test6133b";');
  146. WScript.RegisterModuleSource("test6133b", 'export function notDefault () {}');
  147. testRunner.LoadModule('import "test6133a";', undefined, true);
  148. }
  149. },
  150. {
  151. name : "Issue 5236: Module function exports not hoisted test",
  152. body()
  153. {
  154. WScript.RegisterModuleSource("test5236a", `
  155. import {default as Default, bar, foo} from "test5236b";
  156. export default function () {}
  157. export function one() {}
  158. export var two = function () {}
  159. bar();
  160. assert.isNotUndefined(Default);
  161. foo();
  162. `);
  163. WScript.RegisterModuleSource("test5236b", `
  164. import Default from "test5236c";
  165. export function bar() {}
  166. export default class {}
  167. export var foo = function () {}
  168. Default();
  169. `);
  170. WScript.RegisterModuleSource("test5236c", `
  171. import {default as Default, one, two} from "test5236a";
  172. import otherDefault from "test5236b";
  173. export default function bar() {}
  174. Default();
  175. one();
  176. assert.isUndefined(two);
  177. assert.isUndefined(otherDefault);
  178. `);
  179. testRunner.LoadModule('import "test5236a";', undefined, false);
  180. }
  181. }
  182. ];
  183. testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });