| 
									
										
										
										
											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"); | 
					
						
							| 
									
										
										
										
											2020-02-13 00:13:53 +08:00
										 |  |  | const { createFsFromVolume, Volume } = require("memfs"); | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							| 
									
										
										
										
											2024-06-11 20:32:02 +08:00
										 |  |  | 		// eslint-disable-next-line jest/no-disabled-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); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-19 21:48:00 +08:00
										 |  |  | 	createTestCase(100, true); | 
					
						
							|  |  |  | 	createTestCase(10, true); | 
					
						
							|  |  |  | 	createTestCase(600, true); | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-19 21:48:00 +08:00
										 |  |  | 	function createTestCase(changeTimeout, invalidate) { | 
					
						
							|  |  |  | 		describe(`time between changes ${changeTimeout}ms${ | 
					
						
							|  |  |  | 			invalidate ? " with invalidate call" : "" | 
					
						
							|  |  |  | 		}`, () => {
 | 
					
						
							| 
									
										
										
										
											2018-02-25 18:46:17 +08:00
										 |  |  | 			const fixturePath = path.join( | 
					
						
							|  |  |  | 				__dirname, | 
					
						
							|  |  |  | 				"fixtures", | 
					
						
							| 
									
										
										
										
											2024-07-31 10:39:30 +08:00
										 |  |  | 				`temp-${changeTimeout}` | 
					
						
							| 
									
										
										
										
											2018-02-25 18:46:17 +08:00
										 |  |  | 			); | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							| 
									
										
										
										
											2024-07-31 15:37:05 +08:00
										 |  |  | 				} catch (_err) { | 
					
						
							| 
									
										
										
										
											2018-04-12 02:31:28 +08:00
										 |  |  | 					// 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); | 
					
						
							| 
									
										
										
										
											2024-07-31 15:37:05 +08:00
										 |  |  | 					} catch (_err) { | 
					
						
							| 
									
										
										
										
											2018-04-12 02:31:28 +08:00
										 |  |  | 						// empty
 | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2016-05-07 05:15:07 +08:00
										 |  |  | 					try { | 
					
						
							|  |  |  | 						fs.unlinkSync(file2Path); | 
					
						
							| 
									
										
										
										
											2024-07-31 15:37:05 +08:00
										 |  |  | 					} catch (_err) { | 
					
						
							| 
									
										
										
										
											2018-04-12 02:31:28 +08:00
										 |  |  | 						// empty
 | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2016-05-07 05:15:07 +08:00
										 |  |  | 					try { | 
					
						
							|  |  |  | 						fs.rmdirSync(fixturePath); | 
					
						
							| 
									
										
										
										
											2024-07-31 15:37:05 +08:00
										 |  |  | 					} catch (_err) { | 
					
						
							| 
									
										
										
										
											2018-04-12 02:31:28 +08:00
										 |  |  | 						// 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", | 
					
						
							| 
									
										
										
										
											2024-07-31 10:39:30 +08:00
										 |  |  | 					entry: `${loaderPath}!${filePath}`, | 
					
						
							| 
									
										
										
										
											2016-05-06 19:38:21 +08:00
										 |  |  | 					output: { | 
					
						
							| 
									
										
										
										
											2020-02-17 16:31:02 +08:00
										 |  |  | 						path: "/directory", | 
					
						
							| 
									
										
										
										
											2016-05-06 19:38:21 +08:00
										 |  |  | 						filename: "bundle.js" | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2020-02-15 13:54:52 +08:00
										 |  |  | 				const memfs = (compiler.outputFileSystem = createFsFromVolume( | 
					
						
							| 
									
										
										
										
											2020-02-13 00:13:53 +08:00
										 |  |  | 					new Volume() | 
					
						
							|  |  |  | 				)); | 
					
						
							| 
									
										
										
										
											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 ( | 
					
						
							| 
									
										
										
										
											2020-02-17 16:32:05 +08:00
										 |  |  | 							memfs.readFileSync("/directory/bundle.js") && | 
					
						
							| 
									
										
										
										
											2018-02-25 18:46:17 +08:00
										 |  |  | 							memfs | 
					
						
							| 
									
										
										
										
											2020-02-17 16:32:05 +08:00
										 |  |  | 								.readFileSync("/directory/bundle.js") | 
					
						
							| 
									
										
										
										
											2018-02-25 18:46:17 +08:00
										 |  |  | 								.toString() | 
					
						
							| 
									
										
										
										
											2024-07-31 16:02:41 +08:00
										 |  |  | 								.includes("original") | 
					
						
							| 
									
										
										
										
											2018-02-25 18:46:17 +08:00
										 |  |  | 						) | 
					
						
							| 
									
										
										
										
											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() { | 
					
						
							| 
									
										
										
										
											2021-05-19 21:48:00 +08:00
										 |  |  | 					onChange = () => { | 
					
						
							|  |  |  | 						expect(compiler.modifiedFiles).not.toBe(undefined); | 
					
						
							|  |  |  | 						expect(compiler.removedFiles).not.toBe(undefined); | 
					
						
							|  |  |  | 					}; | 
					
						
							| 
									
										
										
										
											2016-05-06 19:38:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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() { | 
					
						
							| 
									
										
										
										
											2021-05-19 21:48:00 +08:00
										 |  |  | 					if (invalidate) watcher.invalidate(); | 
					
						
							| 
									
										
										
										
											2016-05-06 19:38:21 +08:00
										 |  |  | 					fs.writeFile(file2Path, "wrong", "utf-8", handleError); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					setTimeout(step4, changeTimeout); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				function step4() { | 
					
						
							| 
									
										
										
										
											2017-01-18 23:39:17 +08:00
										 |  |  | 					onChange = () => { | 
					
						
							| 
									
										
										
										
											2021-05-19 21:48:00 +08:00
										 |  |  | 						expect(compiler.modifiedFiles).not.toBe(undefined); | 
					
						
							|  |  |  | 						expect(compiler.removedFiles).not.toBe(undefined); | 
					
						
							| 
									
										
										
										
											2018-02-25 18:46:17 +08:00
										 |  |  | 						if ( | 
					
						
							|  |  |  | 							memfs | 
					
						
							| 
									
										
										
										
											2020-02-17 16:32:05 +08:00
										 |  |  | 								.readFileSync("/directory/bundle.js") | 
					
						
							| 
									
										
										
										
											2018-02-25 18:46:17 +08:00
										 |  |  | 								.toString() | 
					
						
							| 
									
										
										
										
											2024-07-31 16:02:41 +08:00
										 |  |  | 								.includes("correct") | 
					
						
							| 
									
										
										
										
											2018-02-25 18:46:17 +08:00
										 |  |  | 						) | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 	} | 
					
						
							|  |  |  | }); |