| 
									
										
										
										
											2020-06-25 17:44:11 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-15 20:51:52 +08:00
										 |  |  | require("./helpers/warmup-webpack"); | 
					
						
							| 
									
										
										
										
											2020-06-25 17:44:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-15 20:51:52 +08:00
										 |  |  | const path = require("path"); | 
					
						
							| 
									
										
										
										
											2020-06-25 17:44:11 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | describe("WatchClose", () => { | 
					
						
							|  |  |  | 	jest.setTimeout(5000); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	describe("multiple calls watcher", () => { | 
					
						
							|  |  |  | 		const fixturePath = path.join(__dirname, "fixtures"); | 
					
						
							| 
									
										
										
										
											2021-06-01 00:22:09 +08:00
										 |  |  | 		const outputPath = path.join(__dirname, "js/WatchClose"); | 
					
						
							| 
									
										
										
										
											2020-06-25 17:44:11 +08:00
										 |  |  | 		const filePath = path.join(fixturePath, "a.js"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		let compiler; | 
					
						
							|  |  |  | 		let watcher; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		beforeEach(() => { | 
					
						
							| 
									
										
										
										
											2021-07-15 20:51:52 +08:00
										 |  |  | 			const webpack = require("../"); | 
					
						
							| 
									
										
										
										
											2020-06-25 17:44:11 +08:00
										 |  |  | 			compiler = webpack({ | 
					
						
							|  |  |  | 				mode: "development", | 
					
						
							|  |  |  | 				entry: filePath, | 
					
						
							|  |  |  | 				output: { | 
					
						
							| 
									
										
										
										
											2021-06-01 00:22:09 +08:00
										 |  |  | 					path: outputPath, | 
					
						
							| 
									
										
										
										
											2020-06-25 17:44:11 +08:00
										 |  |  | 					filename: "bundle.js" | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2021-02-14 04:18:47 +08:00
										 |  |  | 			watcher = compiler.watch({ poll: 300 }, () => {}); | 
					
						
							| 
									
										
										
										
											2020-06-25 17:44:11 +08:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		afterEach(() => { | 
					
						
							|  |  |  | 			watcher.close(); | 
					
						
							|  |  |  | 			compiler = null; | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		function close(watcher, callback) { | 
					
						
							|  |  |  | 			return new Promise(res => { | 
					
						
							|  |  |  | 				const onClose = () => { | 
					
						
							|  |  |  | 					callback(); | 
					
						
							|  |  |  | 					res(); | 
					
						
							|  |  |  | 				}; | 
					
						
							|  |  |  | 				watcher.close(onClose); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-22 15:29:10 +08:00
										 |  |  | 		it("each callback should be called", async () => { | 
					
						
							| 
									
										
										
										
											2020-06-25 17:44:11 +08:00
										 |  |  | 			let num = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			await Promise.all([ | 
					
						
							|  |  |  | 				close(watcher, () => (num += 1)), | 
					
						
							|  |  |  | 				close(watcher, () => (num += 10)) | 
					
						
							|  |  |  | 			]); | 
					
						
							| 
									
										
										
										
											2020-07-15 20:48:40 +08:00
										 |  |  | 			await Promise.all([ | 
					
						
							|  |  |  | 				close(watcher, () => (num += 100)), | 
					
						
							|  |  |  | 				close(watcher, () => (num += 1000)) | 
					
						
							|  |  |  | 			]); | 
					
						
							| 
									
										
										
										
											2020-06-25 17:44:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-15 20:48:40 +08:00
										 |  |  | 			expect(num).toBe(1111); | 
					
						
							| 
									
										
										
										
											2020-06-25 17:44:11 +08:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }); |