Skip to content

Commit

Permalink
tests(watch): hash assertion for single-config-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 48f34d1 commit 55632d6
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`single-config-watch-opt 1`] = `
" Asset Size Chunks Chunk Names
null.js 930 bytes 0 [emitted] null
Entrypoint null = null.js
[0] ./index.js 0 bytes {0} [built]
WARNING in configuration
The 'mode' option has not been set, webpack will fallback to 'production' for this value. Set 'mode' option to 'development' or 'production' to enable defaults for each environment.
You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/concepts/mode/
"
`;
Original file line number Diff line number Diff line change
@@ -1,8 +1,39 @@
"use strict";

const { runWatch, extractSummary } = require("../../../testUtils");
test("single-config-watch-opt", () => {
runWatch(__dirname, [
jest.setTimeout(10E6);
/* eslint-disable node/no-unsupported-features */
/* eslint-disable node/no-unsupported-features/es-syntax */

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-watch-opt", async done => {
const webpackProc = runAndGetWatchProc(__dirname, [
"--entry",
"./index.js",
"--config",
Expand All @@ -14,18 +45,53 @@ test("single-config-watch-opt", () => {
"--target",
"async-node",
"--watch"
]).then(result => {
const { stdout, stderr } = result;
]);

const summary = extractSummary(stdout);
// 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;

expect(summary).toEqual(expect.anything());
expect(summary).toContain("");
expect(summary).toContain("webpack is watching the files…");
webpackProc.stdout.on("data", data => {
data = data.toString();
chunkNumber++;

expect(stderr).toHaveLength(0);
switch (chunkNumber) {
case 1:
expect(data).toContain("webpack is watching the files");
break;
case 2:
expect(extractSummary(data)).toMatchSnapshot();

expect(summary).toMatchSnapshot();
return;
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 55632d6

Please sign in to comment.