|
2 | 2 |
|
3 | 3 | require("../helpers/warmup-webpack"); |
4 | 4 |
|
| 5 | +const fs = require("fs"); |
5 | 6 | const path = require("path"); |
6 | 7 | const testDirectory = path.resolve(__dirname, ".."); |
7 | 8 | const expectNoDeprecations = require("../helpers/expectNoDeprecations"); |
@@ -75,4 +76,54 @@ describe("WatchClose", () => { |
75 | 76 | expect(num).toBe(1111); |
76 | 77 | }); |
77 | 78 | }); |
| 79 | + |
| 80 | + describe("watching again after close", () => { |
| 81 | + const fixturePath = path.join( |
| 82 | + testDirectory, |
| 83 | + "fixtures", |
| 84 | + `temp-watch-close-${Date.now()}` |
| 85 | + ); |
| 86 | + const filePath = path.join(fixturePath, "file.js"); |
| 87 | + |
| 88 | + beforeAll(() => { |
| 89 | + fs.mkdirSync(fixturePath, { recursive: true }); |
| 90 | + fs.writeFileSync(filePath, "module.exports = 'before';", "utf8"); |
| 91 | + }); |
| 92 | + |
| 93 | + afterAll(() => { |
| 94 | + fs.unlinkSync(filePath); |
| 95 | + fs.rmdirSync(fixturePath); |
| 96 | + }); |
| 97 | + |
| 98 | + it("should read a file edited while closed", async () => { |
| 99 | + const webpack = require("../../"); |
| 100 | + |
| 101 | + const outputPath = path.join(testDirectory, "js/WatchCloseAgain"); |
| 102 | + const compiler = webpack({ |
| 103 | + mode: "development", |
| 104 | + entry: filePath, |
| 105 | + output: { path: outputPath, filename: "bundle.js" } |
| 106 | + }); |
| 107 | + /** |
| 108 | + * @returns {Promise<string>} the emitted bundle of the first build |
| 109 | + */ |
| 110 | + const build = () => |
| 111 | + new Promise((resolve, reject) => { |
| 112 | + const watching = /** @type {import("../../").Watching} */ ( |
| 113 | + compiler.watch({}, (err) => { |
| 114 | + if (err) return reject(err); |
| 115 | + watching.close(() => |
| 116 | + resolve( |
| 117 | + fs.readFileSync(path.join(outputPath, "bundle.js"), "utf8") |
| 118 | + ) |
| 119 | + ); |
| 120 | + }) |
| 121 | + ); |
| 122 | + }); |
| 123 | + |
| 124 | + expect(await build()).toContain("before"); |
| 125 | + fs.writeFileSync(filePath, "module.exports = 'after';", "utf8"); |
| 126 | + expect(await build()).toContain("after"); |
| 127 | + }); |
| 128 | + }); |
78 | 129 | }); |
0 commit comments