| 
									
										
										
										
											2017-01-18 23:39:17 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const path = require("path"); | 
					
						
							| 
									
										
										
										
											2019-06-11 19:09:42 +08:00
										 |  |  | const fs = require("graceful-fs"); | 
					
						
							| 
									
										
										
										
											2017-01-18 23:39:17 +08:00
										 |  |  | const MemoryFs = require("memory-fs"); | 
					
						
							| 
									
										
										
										
											2016-05-06 19:38:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-09 21:26:35 +08:00
										 |  |  | const webpack = require(".."); | 
					
						
							| 
									
										
										
										
											2016-05-06 19:38:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 23:39:17 +08:00
										 |  |  | describe("WatchDetection", () => { | 
					
						
							| 
									
										
										
										
											2018-02-25 18:46:17 +08:00
										 |  |  | 	if (process.env.NO_WATCH_TESTS) { | 
					
						
							| 
									
										
										
										
											2018-04-12 05:54:02 +08:00
										 |  |  | 		it.skip("long running tests excluded", () => {}); | 
					
						
							| 
									
										
										
										
											2017-12-20 23:53:56 +08:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-31 04:03:23 +08:00
										 |  |  | 	jest.setTimeout(10000); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 18:46:17 +08:00
										 |  |  | 	for (let changeTimeout = 10; changeTimeout < 100; changeTimeout += 10) { | 
					
						
							| 
									
										
										
										
											2016-05-06 19:38:21 +08:00
										 |  |  | 		createTestCase(changeTimeout); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-02-25 18:46:17 +08:00
										 |  |  | 	for (let changeTimeout = 200; changeTimeout <= 2000; changeTimeout += 200) { | 
					
						
							| 
									
										
										
										
											2016-05-06 19:38:21 +08:00
										 |  |  | 		createTestCase(changeTimeout); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	function createTestCase(changeTimeout) { | 
					
						
							| 
									
										
										
										
											2018-01-31 04:03:23 +08:00
										 |  |  | 		describe(`time between changes ${changeTimeout}ms`, () => { | 
					
						
							| 
									
										
										
										
											2018-02-25 18:46:17 +08:00
										 |  |  | 			const fixturePath = path.join( | 
					
						
							|  |  |  | 				__dirname, | 
					
						
							|  |  |  | 				"fixtures", | 
					
						
							|  |  |  | 				"temp-" + changeTimeout | 
					
						
							|  |  |  | 			); | 
					
						
							| 
									
										
										
										
											2017-01-18 23:39:17 +08:00
										 |  |  | 			const filePath = path.join(fixturePath, "file.js"); | 
					
						
							|  |  |  | 			const file2Path = path.join(fixturePath, "file2.js"); | 
					
						
							|  |  |  | 			const loaderPath = path.join(__dirname, "fixtures", "delay-loader.js"); | 
					
						
							| 
									
										
										
										
											2018-01-24 23:00:43 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			beforeAll(() => { | 
					
						
							| 
									
										
										
										
											2016-05-06 19:38:21 +08:00
										 |  |  | 				try { | 
					
						
							|  |  |  | 					fs.mkdirSync(fixturePath); | 
					
						
							| 
									
										
										
										
											2018-04-12 02:31:28 +08:00
										 |  |  | 				} catch (e) { | 
					
						
							|  |  |  | 					// empty
 | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2016-05-06 19:38:21 +08:00
										 |  |  | 				fs.writeFileSync(filePath, "require('./file2')", "utf-8"); | 
					
						
							|  |  |  | 				fs.writeFileSync(file2Path, "original", "utf-8"); | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2018-01-24 23:00:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 18:46:17 +08:00
										 |  |  | 			afterAll(done => { | 
					
						
							| 
									
										
										
										
											2017-01-18 23:39:17 +08:00
										 |  |  | 				setTimeout(() => { | 
					
						
							| 
									
										
										
										
											2016-05-07 05:15:07 +08:00
										 |  |  | 					try { | 
					
						
							|  |  |  | 						fs.unlinkSync(filePath); | 
					
						
							| 
									
										
										
										
											2018-04-12 02:31:28 +08:00
										 |  |  | 					} catch (e) { | 
					
						
							|  |  |  | 						// empty
 | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2016-05-07 05:15:07 +08:00
										 |  |  | 					try { | 
					
						
							|  |  |  | 						fs.unlinkSync(file2Path); | 
					
						
							| 
									
										
										
										
											2018-04-12 02:31:28 +08:00
										 |  |  | 					} catch (e) { | 
					
						
							|  |  |  | 						// empty
 | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2016-05-07 05:15:07 +08:00
										 |  |  | 					try { | 
					
						
							|  |  |  | 						fs.rmdirSync(fixturePath); | 
					
						
							| 
									
										
										
										
											2018-04-12 02:31:28 +08:00
										 |  |  | 					} catch (e) { | 
					
						
							|  |  |  | 						// empty
 | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2016-05-07 05:15:07 +08:00
										 |  |  | 					done(); | 
					
						
							|  |  |  | 				}, 100); // cool down a bit
 | 
					
						
							| 
									
										
										
										
											2016-05-06 19:38:21 +08:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2018-01-24 23:00:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 18:46:17 +08:00
										 |  |  | 			it("should build the bundle correctly", done => { | 
					
						
							| 
									
										
										
										
											2017-01-18 23:39:17 +08:00
										 |  |  | 				const compiler = webpack({ | 
					
						
							| 
									
										
										
										
											2018-01-24 23:00:43 +08:00
										 |  |  | 					mode: "development", | 
					
						
							| 
									
										
										
										
											2016-05-06 19:38:21 +08:00
										 |  |  | 					entry: loaderPath + "!" + filePath, | 
					
						
							|  |  |  | 					output: { | 
					
						
							|  |  |  | 						path: "/", | 
					
						
							|  |  |  | 						filename: "bundle.js" | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2018-02-25 18:46:17 +08:00
										 |  |  | 				const memfs = (compiler.outputFileSystem = new MemoryFs()); | 
					
						
							| 
									
										
										
										
											2017-01-18 23:39:17 +08:00
										 |  |  | 				let onChange; | 
					
						
							| 
									
										
										
										
											2017-12-20 23:51:24 +08:00
										 |  |  | 				compiler.hooks.done.tap("WatchDetectionTest", () => { | 
					
						
							| 
									
										
										
										
											2018-02-25 18:46:17 +08:00
										 |  |  | 					if (onChange) onChange(); | 
					
						
							| 
									
										
										
										
											2016-05-06 19:38:21 +08:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-18 23:39:17 +08:00
										 |  |  | 				let watcher; | 
					
						
							| 
									
										
										
										
											2016-05-06 19:38:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				step1(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				function step1() { | 
					
						
							| 
									
										
										
										
											2017-01-18 23:39:17 +08:00
										 |  |  | 					onChange = () => { | 
					
						
							| 
									
										
										
										
											2018-02-25 18:46:17 +08:00
										 |  |  | 						if ( | 
					
						
							|  |  |  | 							memfs.readFileSync("/bundle.js") && | 
					
						
							|  |  |  | 							memfs | 
					
						
							|  |  |  | 								.readFileSync("/bundle.js") | 
					
						
							|  |  |  | 								.toString() | 
					
						
							|  |  |  | 								.indexOf("original") >= 0 | 
					
						
							|  |  |  | 						) | 
					
						
							| 
									
										
										
										
											2016-05-06 19:38:21 +08:00
										 |  |  | 							step2(); | 
					
						
							| 
									
										
										
										
											2017-01-18 23:39:17 +08:00
										 |  |  | 					}; | 
					
						
							| 
									
										
										
										
											2016-05-06 19:38:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 18:46:17 +08:00
										 |  |  | 					watcher = compiler.watch( | 
					
						
							|  |  |  | 						{ | 
					
						
							|  |  |  | 							aggregateTimeout: 50 | 
					
						
							|  |  |  | 						}, | 
					
						
							|  |  |  | 						() => {} | 
					
						
							|  |  |  | 					); | 
					
						
							| 
									
										
										
										
											2016-05-06 19:38:21 +08:00
										 |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				function step2() { | 
					
						
							|  |  |  | 					onChange = null; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 18:46:17 +08:00
										 |  |  | 					fs.writeFile( | 
					
						
							|  |  |  | 						filePath, | 
					
						
							|  |  |  | 						"require('./file2'); again", | 
					
						
							|  |  |  | 						"utf-8", | 
					
						
							|  |  |  | 						handleError | 
					
						
							|  |  |  | 					); | 
					
						
							| 
									
										
										
										
											2016-05-06 19:38:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 					setTimeout(step3, changeTimeout); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				function step3() { | 
					
						
							|  |  |  | 					onChange = null; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					fs.writeFile(file2Path, "wrong", "utf-8", handleError); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					setTimeout(step4, changeTimeout); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				function step4() { | 
					
						
							| 
									
										
										
										
											2017-01-18 23:39:17 +08:00
										 |  |  | 					onChange = () => { | 
					
						
							| 
									
										
										
										
											2018-02-25 18:46:17 +08:00
										 |  |  | 						if ( | 
					
						
							|  |  |  | 							memfs | 
					
						
							|  |  |  | 								.readFileSync("/bundle.js") | 
					
						
							|  |  |  | 								.toString() | 
					
						
							|  |  |  | 								.indexOf("correct") >= 0 | 
					
						
							|  |  |  | 						) | 
					
						
							| 
									
										
										
										
											2017-11-15 21:08:11 +08:00
										 |  |  | 							step5(); | 
					
						
							| 
									
										
										
										
											2017-01-18 23:39:17 +08:00
										 |  |  | 					}; | 
					
						
							| 
									
										
										
										
											2016-05-06 19:38:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 					fs.writeFile(file2Path, "correct", "utf-8", handleError); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-15 21:08:11 +08:00
										 |  |  | 				function step5() { | 
					
						
							| 
									
										
										
										
											2016-05-06 19:38:21 +08:00
										 |  |  | 					onChange = null; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-22 17:45:49 +08:00
										 |  |  | 					watcher.close(() => { | 
					
						
							| 
									
										
										
										
											2018-01-24 23:00:43 +08:00
										 |  |  | 						setTimeout(done, 500); | 
					
						
							| 
									
										
										
										
											2017-02-22 17:45:49 +08:00
										 |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2016-05-06 19:38:21 +08:00
										 |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				function handleError(err) { | 
					
						
							| 
									
										
										
										
											2018-02-25 18:46:17 +08:00
										 |  |  | 					if (err) done(err); | 
					
						
							| 
									
										
										
										
											2016-05-06 19:38:21 +08:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2017-01-18 23:39:17 +08:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-05-06 19:38:21 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | }); |