| 
									
										
										
										
											2020-07-05 17:26:26 -07:00
										 |  |  | test("basic functionality", () => { | 
					
						
							| 
									
										
										
										
											2020-07-06 07:37:45 -07:00
										 |  |  |     let callHoisted = hoisted(); | 
					
						
							|  |  |  |     function hoisted() { | 
					
						
							|  |  |  |         return "foo"; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     expect(hoisted()).toBe("foo"); | 
					
						
							|  |  |  |     expect(callHoisted).toBe("foo"); | 
					
						
							| 
									
										
										
										
											2020-07-05 17:26:26 -07:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-06-04 14:48:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-05 17:26:26 -07:00
										 |  |  | // First two calls produce a ReferenceError, but the declarations should be hoisted
 | 
					
						
							| 
									
										
										
										
											2021-07-04 03:15:52 +02:00
										 |  |  | test("functions are hoisted across non-lexical scopes", () => { | 
					
						
							| 
									
										
										
										
											2020-07-06 07:37:45 -07:00
										 |  |  |     expect(scopedHoisted).toBeUndefined(); | 
					
						
							|  |  |  |     expect(callScopedHoisted).toBeUndefined(); | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         var callScopedHoisted = scopedHoisted(); | 
					
						
							|  |  |  |         function scopedHoisted() { | 
					
						
							|  |  |  |             return "foo"; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         expect(scopedHoisted()).toBe("foo"); | 
					
						
							|  |  |  |         expect(callScopedHoisted).toBe("foo"); | 
					
						
							| 
									
										
										
										
											2020-06-04 14:48:36 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-07-05 17:26:26 -07:00
										 |  |  |     expect(scopedHoisted()).toBe("foo"); | 
					
						
							|  |  |  |     expect(callScopedHoisted).toBe("foo"); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-06-04 14:48:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-05 17:26:26 -07:00
										 |  |  | test("functions are not hoisted across lexical scopes", () => { | 
					
						
							| 
									
										
										
										
											2020-07-06 07:37:45 -07:00
										 |  |  |     const test = () => { | 
					
						
							|  |  |  |         var iife = (function () { | 
					
						
							|  |  |  |             return declaredLater(); | 
					
						
							|  |  |  |         })(); | 
					
						
							|  |  |  |         function declaredLater() { | 
					
						
							|  |  |  |             return "yay"; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return iife; | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2020-06-04 14:48:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-06 07:37:45 -07:00
										 |  |  |     expect(() => declaredLater).toThrow(ReferenceError); | 
					
						
							|  |  |  |     expect(test()).toBe("yay"); | 
					
						
							| 
									
										
										
										
											2020-07-05 17:26:26 -07:00
										 |  |  | }); |