return.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. describe("returning from loops", () => {
  2. test("returning from while loops", () => {
  3. function foo() {
  4. while (true) {
  5. return 10;
  6. }
  7. }
  8. expect(foo()).toBe(10);
  9. });
  10. test("returning from do-while loops", () => {
  11. function foo() {
  12. do {
  13. return 10;
  14. } while (true);
  15. }
  16. expect(foo()).toBe(10);
  17. });
  18. test("returning from for loops", () => {
  19. function foo() {
  20. for (let i = 0; i < 5; i++) {
  21. return 10;
  22. }
  23. }
  24. expect(foo()).toBe(10);
  25. });
  26. test("returning from for-in loops", () => {
  27. function foo() {
  28. const o = { a: 1, b: 2 };
  29. for (let a in o) {
  30. return 10;
  31. }
  32. }
  33. expect(foo()).toBe(10);
  34. });
  35. test("returning from for-of loops", () => {
  36. function foo() {
  37. const o = [1, 2, 3];
  38. for (let a of o) {
  39. return 10;
  40. }
  41. }
  42. expect(foo()).toBe(10);
  43. });
  44. });