Skip to content

Commit

Permalink
tests(watch): hash assertion for multi-config-watch-opt
Browse files Browse the repository at this point in the history
  • Loading branch information
hemal7735 authored and evenstensberg committed Feb 5, 2019
1 parent 6b4d339 commit 6dd2327
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`multi-config-watch-opt 1`] = `
"
webpack is watching the files…
Child
"Child
Asset Size Chunks Chunk Names
null.js 945 bytes 0 [emitted] null
Entrypoint null = null.js
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,39 @@
"use strict";

jest.setTimeout(10E6);
/* eslint-disable node/no-unsupported-features */
/* eslint-disable node/no-unsupported-features/es-syntax */

jest.setTimeout(10E6);
const fs = require("fs");
const path = require("path");
const { extractSummary, extractHash, appendDataIfFileExists, runAndGetWatchProc } = require("../../../testUtils");

const { runWatch, extractSummary } = 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("multi-config-watch-opt", async done => {
const result = await runWatch(__dirname, [
const webpackProc = runAndGetWatchProc(__dirname, [
"--entry",
"./index.js",
"--config",
Expand All @@ -22,16 +47,53 @@ test("multi-config-watch-opt", 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++;

const summary = extractSummary(stdout);
console.log(data);

expect(summary).toContain("");
expect(summary).toEqual(expect.anything());
expect(summary).toContain("");
expect(summary).toContain("webpack is watching the files…");
switch (chunkNumber) {
case 1:
expect(data).toContain("webpack is watching the files");
break;
case 2:
expect(extractSummary(data)).toMatchSnapshot();

expect(stderr).toHaveLength(0);
expect(summary).toMatchSnapshot();
done();
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");

break;
case 3:
hash2 = extractHash(data);

expect(hash2.hash).not.toBe(hash1.hash);

webpackProc.kill();
done();
break;
default:
break;
}
});

webpackProc.stderr.on("data", error => {
// fail test case if there is any error
done(error.toString());
});
});

0 comments on commit 6dd2327

Please sign in to comment.