diff --git a/test/binCases/watch/single-config/__snapshots__/single-config.test.js.snap b/test/binCases/watch/single-config/__snapshots__/single-config.test.js.snap index 4653fa3c252..eb23870405e 100644 --- a/test/binCases/watch/single-config/__snapshots__/single-config.test.js.snap +++ b/test/binCases/watch/single-config/__snapshots__/single-config.test.js.snap @@ -1,10 +1,7 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`single-config 1`] = ` -" -webpack is watching the files… - - Asset Size Chunks Chunk Names +" Asset Size Chunks Chunk Names null.js 930 bytes 0 [emitted] null Entrypoint null = null.js [0] ./index.js 0 bytes {0} [built] diff --git a/test/binCases/watch/single-config/single-config.test.js b/test/binCases/watch/single-config/single-config.test.js index 5dc4ff566c4..8add0223ea0 100644 --- a/test/binCases/watch/single-config/single-config.test.js +++ b/test/binCases/watch/single-config/single-config.test.js @@ -5,10 +5,36 @@ jest.setTimeout(10E6); -const { runWatch, extractSummary } = require("../../../testUtils"); +const fs = require("fs"); +const path = require("path"); +const { extractSummary, extractHash, appendDataIfFileExists, runAndGetWatchProc } = require("../../../testUtils"); + +const fileToChange = "index.js"; +const copyFile = "index_copy.js"; +const fileToChangePath = path.resolve(__dirname, fileToChange); +const copyFilePath = path.resolve(__dirname, copyFile); + +// create copy of "index.js" => "index_copy.js" +beforeEach(() => { + // fs.copyFileSync was added in Added in: v8.5.0 + // We should refactor the below code once our minimal supported version is v8.5.0 + fs.createReadStream(fileToChangePath).pipe(fs.createWriteStream(copyFilePath)); +}); + +afterEach(() => { + try { + // subsequent test-case runs won't pass as snapshot is not matched + // hence, deleting the file as it is modified by the test + fs.unlinkSync(fileToChangePath); + } catch (e) { + console.warn("could not remove the file:" + fileToChangePath + "\n" + e.message); + } finally { + fs.renameSync(copyFilePath, fileToChangePath); + } +}); test("single-config", async(done) => { - const result = await runWatch(__dirname, [ + const webpackProc = runAndGetWatchProc(__dirname, [ "--entry", "./index.js", "--config", @@ -22,17 +48,52 @@ test("single-config", async(done) => { "--watch" ]); - const { stdout, stderr } = result; + // info-verbosity is set to info by default + // It does not spit the output in one go. + // So we need to keep a track of chunks output order + // 1. webpack is watching the files... + // 2. Hash and other info + // 3. (file changed) Hash and other info + var chunkNumber = 0; + var hash1, hash2; + + webpackProc.stdout.on("data", data => { + data = data.toString(); + chunkNumber++; + + console.log(data); + + switch (chunkNumber) { + case 1: + expect(data).toContain("webpack is watching the files"); + break; + case 2: + expect(extractSummary(data)).toMatchSnapshot(); + + hash1 = extractHash(data); + + // We get webpack output after running test + // Since we are running the webpack in watch mode, changing file will generate additional output + // First time output will be validated fully + // Hash of the The subsequent output will be tested against that of first time output + appendDataIfFileExists(__dirname, fileToChange, "//junk-comment"); - const summary = extractSummary(stdout); + break; + case 3: + hash2 = extractHash(data); - expect(summary).toContain(""); - expect(summary).toEqual(expect.anything()); - expect(summary).toContain(""); - expect(summary).toContain("webpack is watching the files…"); + expect(hash2.hash).not.toBe(hash1.hash); - expect(stderr).toHaveLength(0); - expect(summary).toMatchSnapshot(); - done(); + webpackProc.kill(); + done(); + break; + default: + break; + } + }); + webpackProc.stderr.on("data", error => { + // fail test case if there is any error + done(error.toString()); + }); });