async-await.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. describe("parsing freestanding async functions", () => {
  2. test("simple", () => {
  3. expect(`async function foo() {}`).toEval();
  4. expect(`async
  5. function foo() {}`).not.toEval();
  6. });
  7. test("await expression", () => {
  8. expect(`async function foo() { await bar(); }`).toEval();
  9. expect(`async function foo() { await; }`).not.toEval();
  10. expect(`function foo() { await bar(); }`).not.toEval();
  11. expect(`function foo() { await; }`).toEval();
  12. });
  13. });
  14. describe("parsing object literal async functions", () => {
  15. test("simple", () => {
  16. expect(`x = { async foo() { } }`).toEval();
  17. expect(`x = { async
  18. foo() { } }`).not.toEval();
  19. });
  20. test("await expression", () => {
  21. expect(`x = { foo() { await bar(); } }`).not.toEval();
  22. expect(`x = { foo() { await; } }`).toEval();
  23. expect(`x = { async foo() { await bar(); } }`).toEval();
  24. expect(`x = { async foo() { await; } }`).not.toEval();
  25. });
  26. });
  27. describe("parsing classes with async methods", () => {
  28. test("simple", () => {
  29. expect(`class Foo { async foo() {} }`).toEval();
  30. expect(`class Foo { static async foo() {} }`).toEval();
  31. expect(`class Foo { async foo() { await bar(); } }`).toEval();
  32. expect(`class Foo { async foo() { await; } }`).not.toEval();
  33. expect(`class Foo { async constructor() {} }`).not.toEval();
  34. });
  35. });
  36. test("function expression names equal to 'await'", () => {
  37. expect(`async function foo() { (function await() {}); }`).toEval();
  38. expect(`async function foo() { function await() {} }`).not.toEval();
  39. });
  40. test("basic functionality", () => {
  41. test("simple", () => {
  42. let executionValue = null;
  43. let resultValue = null;
  44. async function foo() {
  45. executionValue = "someValue";
  46. return "otherValue";
  47. }
  48. const returnValue = foo();
  49. expect(returnValue).toBeInstanceOf(Promise);
  50. returnValue.then(result => {
  51. resultValue = result;
  52. });
  53. runQueuedPromiseJobs();
  54. expect(executionValue).toBe("someValue");
  55. expect(resultValue).toBe("otherValue");
  56. });
  57. test("await", () => {
  58. let resultValue = null;
  59. async function foo() {
  60. return "someValue";
  61. }
  62. async function bar() {
  63. resultValue = await foo();
  64. }
  65. bar();
  66. runQueuedPromiseJobs();
  67. expect(resultValue).toBe("someValue");
  68. });
  69. });