dynamic-module-import-specifier.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 functionality tests -- verifies functionality of import and export statements
  6. WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
  7. function testScript(source, message, shouldFail = false, explicitAsync = false) {
  8. message += " (script)";
  9. let testfunc = () => testRunner.LoadScript(source, undefined, shouldFail, explicitAsync);
  10. if (shouldFail) {
  11. let caught = false;
  12. assert.throws(testfunc, SyntaxErrr, message);
  13. assert.isTrue(caught, `Expected error not thrown: ${message}`);
  14. } else {
  15. assert.doesNotThrow(testfunc, message);
  16. }
  17. }
  18. function testModuleScript(source, message, shouldFail = false, explicitAsync = false) {
  19. message += " (module)";
  20. let testfunc = () => testRunner.LoadModule(source, 'samethread', shouldFail, explicitAsync);
  21. if (shouldFail) {
  22. let caught = false;
  23. // We can't use assert.throws here because the SyntaxError used to construct the thrown error
  24. // is from a different context so it won't be strictly equal to our SyntaxError.
  25. try {
  26. testfunc();
  27. } catch(e) {
  28. caught = true;
  29. // Compare toString output of SyntaxError and other context SyntaxError constructor.
  30. assert.areEqual(e.constructor.toString(), SyntaxError.toString(), message);
  31. }
  32. assert.isTrue(caught, `Expected error not thrown: ${message}`);
  33. } else {
  34. assert.doesNotThrow(testfunc, message);
  35. }
  36. }
  37. function testDynamicImport(importFunc, thenFunc, catchFunc, _asyncEnter, _asyncExit) {
  38. var promise = importFunc();
  39. assert.isTrue(promise instanceof Promise);
  40. promise.then((v)=>{
  41. _asyncEnter();
  42. thenFunc(v);
  43. _asyncExit();
  44. }).catch((err)=>{
  45. _asyncEnter();
  46. catchFunc(err);
  47. _asyncExit();
  48. });
  49. }
  50. var tests = [
  51. {
  52. name: "Valid cases for import()",
  53. body: function () {
  54. let functionBody =
  55. `
  56. assert.doesNotThrow(function () { eval("import(undefined)"); }, "undefined");
  57. assert.doesNotThrow(function () { eval("import(null)"); }, "null");
  58. assert.doesNotThrow(function () { eval("import(true)"); }, "boolean - true");
  59. assert.doesNotThrow(function () { eval("import(false)"); }, "boolean - false");
  60. assert.doesNotThrow(function () { eval("import(1234567890)"); }, "number");
  61. assert.doesNotThrow(function () { eval("import('abc789cde')"); }, "string literal");
  62. assert.doesNotThrow(function () { eval("import('number' + 100 + 0.4 * 18)"); }, "expression");
  63. assert.doesNotThrow(function () { eval("import(import(true))"); }, "nested import");
  64. assert.doesNotThrow(function () { eval("import(import(Infinity) + import(undefined))"); }, "nested import expression");
  65. `;
  66. testScript(functionBody, "Test importing a simple exported function");
  67. testModuleScript(functionBody, "Test importing a simple exported function");
  68. }
  69. },
  70. {
  71. name: "Syntax errors for import() call",
  72. body: function () {
  73. let functionBody =
  74. `
  75. assert.throws(function () { eval("import()"); }, SyntaxError, "no argument");
  76. assert.throws(function () { eval("import(1, 2)"); }, SyntaxError, "more than one arguments");
  77. assert.throws(function () { eval("import('abc.js', 'def.js')"); }, SyntaxError, "more than one argument - case 2");
  78. assert.throws(function () { eval("import(...['abc.js', 'def.js'])"); }, SyntaxError, "spread argument");
  79. `;
  80. testScript(functionBody, "Test importing a simple exported function");
  81. testModuleScript(functionBody, "Test importing a simple exported function");
  82. }
  83. },
  84. {
  85. name: "Module specifier that are not string",
  86. body: function () {
  87. var testNonStringSpecifier = function (specifier, expectedType, expectedErrMsg, message) {
  88. if (typeof message === "undefined" ) {
  89. message = specifier;
  90. }
  91. let functionBody =
  92. `testDynamicImport(
  93. ()=>{
  94. return import(${specifier});
  95. },
  96. (v)=>{
  97. assert.fail('Expected: promise rejected; actual: promise resolved: ' + '${message}');
  98. },
  99. (err)=>{
  100. assert.isTrue(err instanceof Error, '${message}');
  101. assert.areEqual(${expectedType}, err.constructor, '${message}');
  102. assert.areEqual("${expectedErrMsg}", err.message, '${message}');
  103. }, _asyncEnter, _asyncExit
  104. )`;
  105. testScript(functionBody, "Test importing a simple exported function", false, true);
  106. testModuleScript(functionBody, "Test importing a simple exported function", false, true);
  107. };
  108. testNonStringSpecifier("undefined", "URIError", "undefined");
  109. testNonStringSpecifier("null", "URIError", "null");
  110. testNonStringSpecifier("true", "URIError", "true");
  111. testNonStringSpecifier("false", "URIError", "false");
  112. testNonStringSpecifier("NaN", "URIError", "NaN");
  113. testNonStringSpecifier("+0", "URIError", "0");
  114. testNonStringSpecifier("-0", "URIError", "0");
  115. testNonStringSpecifier("-12345", "URIError", "-12345");
  116. testNonStringSpecifier("1/0", "URIError", "Infinity");
  117. testNonStringSpecifier("1.123456789012345678901", "URIError", "1.1234567890123457");
  118. testNonStringSpecifier("-1.123456789012345678901", "URIError", "-1.1234567890123457");
  119. testNonStringSpecifier('Symbol("abc")', "TypeError", "No implicit conversion of Symbol to String");
  120. testNonStringSpecifier("{}", "URIError", "[object Object]");
  121. }
  122. },
  123. ];
  124. testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });