|
| 1 | +// Copy from https://github.com/webpack/webpack/blob/main/test/WatchSuspend.test.js |
| 2 | +"use strict"; |
| 3 | + |
| 4 | +const path = require("path"); |
| 5 | +const fs = require("fs"); |
| 6 | + |
| 7 | +describe("WatchSuspend", () => { |
| 8 | + if (process.env.NO_WATCH_TESTS) { |
| 9 | + it.skip("long running tests excluded", () => {}); |
| 10 | + return; |
| 11 | + } |
| 12 | + |
| 13 | + jest.setTimeout(5000); |
| 14 | + |
| 15 | + describe("suspend and resume watcher", () => { |
| 16 | + const fixturePath = path.join( |
| 17 | + __dirname, |
| 18 | + "fixtures", |
| 19 | + "temp-watch-" + Date.now() |
| 20 | + ); |
| 21 | + const filePath = path.join(fixturePath, "file.js"); |
| 22 | + const file2Path = path.join(fixturePath, "file2.js"); |
| 23 | + const file3Path = path.join(fixturePath, "file3.js"); |
| 24 | + const outputPath = path.join(__dirname, "js/WatchSuspend"); |
| 25 | + const outputFile = path.join(outputPath, "bundle.js"); |
| 26 | + let compiler = null; |
| 27 | + let watching = null; |
| 28 | + let onChange = null; |
| 29 | + |
| 30 | + beforeAll(() => { |
| 31 | + try { |
| 32 | + fs.mkdirSync(fixturePath); |
| 33 | + } catch (e) { |
| 34 | + // skip |
| 35 | + } |
| 36 | + try { |
| 37 | + fs.writeFileSync(filePath, "'foo'", "utf-8"); |
| 38 | + fs.writeFileSync(file2Path, "'file2'", "utf-8"); |
| 39 | + fs.writeFileSync(file3Path, "'file3'", "utf-8"); |
| 40 | + } catch (e) { |
| 41 | + // skip |
| 42 | + } |
| 43 | + const { rspack: webpack } = require("../"); |
| 44 | + compiler = webpack({ |
| 45 | + mode: "development", |
| 46 | + entry: filePath, |
| 47 | + output: { |
| 48 | + path: outputPath, |
| 49 | + filename: "bundle.js" |
| 50 | + } |
| 51 | + }); |
| 52 | + watching = compiler.watch({ aggregateTimeout: 50, poll: 1000 }, () => {}); |
| 53 | + |
| 54 | + compiler.hooks.done.tap("WatchSuspendTest", () => { |
| 55 | + if (onChange) onChange(); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + afterAll(() => { |
| 60 | + watching.close(); |
| 61 | + compiler = null; |
| 62 | + try { |
| 63 | + fs.unlinkSync(filePath); |
| 64 | + } catch (e) { |
| 65 | + // skip |
| 66 | + } |
| 67 | + try { |
| 68 | + fs.rmdirSync(fixturePath); |
| 69 | + } catch (e) { |
| 70 | + // skip |
| 71 | + } |
| 72 | + }); |
| 73 | + |
| 74 | + it("should compile successfully", done => { |
| 75 | + onChange = () => { |
| 76 | + expect(fs.readFileSync(outputFile, "utf-8")).toContain("'foo'"); |
| 77 | + onChange = null; |
| 78 | + done(); |
| 79 | + }; |
| 80 | + }); |
| 81 | + |
| 82 | + it("should suspend compilation", done => { |
| 83 | + onChange = jest.fn(); |
| 84 | + watching.suspend(); |
| 85 | + fs.writeFileSync(filePath, "'bar'", "utf-8"); |
| 86 | + setTimeout(() => { |
| 87 | + expect(onChange.mock.calls.length).toBe(0); |
| 88 | + onChange = null; |
| 89 | + done(); |
| 90 | + }, 1000); |
| 91 | + }); |
| 92 | + |
| 93 | + it("should resume compilation", done => { |
| 94 | + onChange = () => { |
| 95 | + expect(fs.readFileSync(outputFile, "utf-8")).toContain("'bar'"); |
| 96 | + onChange = null; |
| 97 | + done(); |
| 98 | + }; |
| 99 | + watching.resume(); |
| 100 | + }); |
| 101 | + |
| 102 | + for (const changeBefore of [true]) |
| 103 | + for (const delay of [200]) { |
| 104 | + // eslint-disable-next-line no-loop-func |
| 105 | + it(`should not ignore changes during resumed compilation (changeBefore: ${changeBefore}, delay: ${delay}ms)`, async () => { |
| 106 | + // aggregateTimeout must be long enough for this test |
| 107 | + // So set-up new watcher and wait when initial compilation is done |
| 108 | + await new Promise(resolve => { |
| 109 | + watching.close(() => { |
| 110 | + watching = compiler.watch( |
| 111 | + { aggregateTimeout: 1000, poll: 1000 }, |
| 112 | + () => { |
| 113 | + resolve(); |
| 114 | + } |
| 115 | + ); |
| 116 | + }); |
| 117 | + }); |
| 118 | + return new Promise(resolve => { |
| 119 | + if (changeBefore) fs.writeFileSync(filePath, "'bar'", "utf-8"); |
| 120 | + setTimeout(() => { |
| 121 | + watching.suspend(); |
| 122 | + fs.writeFileSync(filePath, "'baz'", "utf-8"); |
| 123 | + |
| 124 | + onChange = "throw"; |
| 125 | + setTimeout(() => { |
| 126 | + onChange = () => { |
| 127 | + expect(fs.readFileSync(outputFile, "utf-8")).toContain( |
| 128 | + "'baz'" |
| 129 | + ); |
| 130 | + expect( |
| 131 | + compiler.modifiedFiles && |
| 132 | + Array.from(compiler.modifiedFiles).sort() |
| 133 | + ).toEqual([filePath]); |
| 134 | + expect( |
| 135 | + compiler.removedFiles && Array.from(compiler.removedFiles) |
| 136 | + ).toEqual([]); |
| 137 | + onChange = null; |
| 138 | + resolve(); |
| 139 | + }; |
| 140 | + watching.resume(); |
| 141 | + }, delay); |
| 142 | + }, 200); |
| 143 | + }); |
| 144 | + }); |
| 145 | + } |
| 146 | + |
| 147 | + it("should not drop changes when suspended", done => { |
| 148 | + const aggregateTimeout = 50; |
| 149 | + // Trigger initial compilation with file2.js (assuming correct) |
| 150 | + fs.writeFileSync( |
| 151 | + filePath, |
| 152 | + 'require("./file2.js"); require("./file3.js")', |
| 153 | + "utf-8" |
| 154 | + ); |
| 155 | + |
| 156 | + onChange = () => { |
| 157 | + // Initial compilation is done, start the test |
| 158 | + watching.suspend(); |
| 159 | + |
| 160 | + // Trigger the first change (works as expected): |
| 161 | + fs.writeFileSync(file2Path, "'foo'", "utf-8"); |
| 162 | + |
| 163 | + // Trigger the second change _after_ aggregation timeout of the first |
| 164 | + setTimeout(() => { |
| 165 | + fs.writeFileSync(file3Path, "'bar'", "utf-8"); |
| 166 | + |
| 167 | + // Wait when the file3 edit is settled and re-compile |
| 168 | + setTimeout(() => { |
| 169 | + watching.resume(); |
| 170 | + |
| 171 | + onChange = () => { |
| 172 | + onChange = null; |
| 173 | + expect(fs.readFileSync(outputFile, "utf-8")).toContain("'bar'"); |
| 174 | + done(); |
| 175 | + }; |
| 176 | + }, 200); |
| 177 | + }, aggregateTimeout + 50); |
| 178 | + }; |
| 179 | + }); |
| 180 | + }); |
| 181 | +}); |
0 commit comments