function-hoisting.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. test("basic functionality", () => {
  2. let callHoisted = hoisted();
  3. function hoisted() {
  4. return "foo";
  5. }
  6. expect(hoisted()).toBe("foo");
  7. expect(callHoisted).toBe("foo");
  8. });
  9. // First two calls produce a ReferenceError, but the declarations should be hoisted
  10. test("functions are hoisted across non-lexical scopes", () => {
  11. expect(scopedHoisted).toBeUndefined();
  12. expect(callScopedHoisted).toBeUndefined();
  13. {
  14. var callScopedHoisted = scopedHoisted();
  15. function scopedHoisted() {
  16. return "foo";
  17. }
  18. expect(scopedHoisted()).toBe("foo");
  19. expect(callScopedHoisted).toBe("foo");
  20. }
  21. expect(scopedHoisted()).toBe("foo");
  22. expect(callScopedHoisted).toBe("foo");
  23. });
  24. test("functions are not hoisted across lexical scopes", () => {
  25. const test = () => {
  26. var iife = (function () {
  27. return declaredLater();
  28. })();
  29. function declaredLater() {
  30. return "yay";
  31. }
  32. return iife;
  33. };
  34. expect(() => declaredLater).toThrow(ReferenceError);
  35. expect(test()).toBe("yay");
  36. });