basic-modules.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Because you can't easily load modules directly we load them via here and check
  2. // if they passed by checking the result
  3. function expectModulePassed(filename) {
  4. if (!filename.endsWith(".mjs") || !filename.startsWith("./")) {
  5. throw new ExpectationError(
  6. "Expected module name to start with './' " +
  7. "and end with '.mjs' but got '" +
  8. filename +
  9. "'"
  10. );
  11. }
  12. async function getModule() {
  13. return import(filename);
  14. }
  15. let moduleLoaded = false;
  16. let moduleResult = null;
  17. let thrownError = null;
  18. getModule()
  19. .then(result => {
  20. moduleLoaded = true;
  21. moduleResult = result;
  22. expect(moduleResult).toHaveProperty("passed", true);
  23. })
  24. .catch(error => {
  25. thrownError = error;
  26. });
  27. runQueuedPromiseJobs();
  28. if (thrownError) {
  29. throw thrownError;
  30. }
  31. expect(moduleLoaded).toBeTrue();
  32. return moduleResult;
  33. }
  34. describe("testing behavior", () => {
  35. // To ensure the other tests are interpreter correctly we first test the underlying
  36. // mechanisms so these tests don't use expectModulePassed.
  37. test("can load a module", () => {
  38. let passed = false;
  39. let error = null;
  40. import("./empty.mjs")
  41. .then(() => {
  42. passed = true;
  43. })
  44. .catch(err => {
  45. error = err;
  46. });
  47. runQueuedPromiseJobs();
  48. if (error) throw error;
  49. expect(passed).toBeTrue();
  50. });
  51. test("can load a module twice", () => {
  52. let passed = false;
  53. let error = null;
  54. import("./empty.mjs")
  55. .then(() => {
  56. passed = true;
  57. })
  58. .catch(err => {
  59. error = err;
  60. });
  61. runQueuedPromiseJobs();
  62. if (error) throw error;
  63. expect(passed).toBeTrue();
  64. });
  65. test("can retrieve exported value", () => {
  66. async function getValue(filename) {
  67. const imported = await import(filename);
  68. expect(imported).toHaveProperty("passed", true);
  69. }
  70. let passed = false;
  71. let error = null;
  72. getValue("./single-const-export.mjs")
  73. .then(obj => {
  74. passed = true;
  75. })
  76. .catch(err => {
  77. error = err;
  78. });
  79. runQueuedPromiseJobs();
  80. if (error) throw error;
  81. expect(passed).toBeTrue();
  82. });
  83. test("expectModulePassed works", () => {
  84. expectModulePassed("./single-const-export.mjs");
  85. });
  86. });
  87. describe("in- and exports", () => {
  88. test("variable and lexical declarations", () => {
  89. const result = expectModulePassed("./basic-export-types.mjs");
  90. expect(result).not.toHaveProperty("default", null);
  91. expect(result).toHaveProperty("constValue", 1);
  92. expect(result).toHaveProperty("letValue", 2);
  93. expect(result).toHaveProperty("varValue", 3);
  94. expect(result).toHaveProperty("namedConstValue", 1 + 3);
  95. expect(result).toHaveProperty("namedLetValue", 2 + 3);
  96. expect(result).toHaveProperty("namedVarValue", 3 + 3);
  97. });
  98. test("default exports", () => {
  99. const result = expectModulePassed("./module-with-default.mjs");
  100. expect(result).toHaveProperty("defaultValue");
  101. expect(result.default).toBe(result.defaultValue);
  102. });
  103. test("declaration exports which can be used in the module it self", () => {
  104. expectModulePassed("./declarations-tests.mjs");
  105. });
  106. });
  107. describe("loops", () => {
  108. test("import and export from own file", () => {
  109. expectModulePassed("./loop-self.mjs");
  110. });
  111. test("import something which imports a cycle", () => {
  112. expectModulePassed("./loop-entry.mjs");
  113. });
  114. });