Skip to content

Commit

Permalink
tests(watch): hash assertion info-verbosity-verbose
Browse files Browse the repository at this point in the history
  • Loading branch information
hemal7735 authored and evenstensberg committed Feb 5, 2019
1 parent 675d5c0 commit 42e5ee8
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`info-verbosity-verbose 1`] = `
"
Compilation starting…
webpack is watching the files…
Compilation finished
Asset Size Chunks Chunk Names
" Asset Size Chunks Chunk Names
main.js 930 bytes 0 [emitted] main
Entrypoint main = main.js
[0] ./index.js 0 bytes {0} [built]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,36 @@ jest.setTimeout(10E6);
/* eslint-disable node/no-unsupported-features */
/* eslint-disable node/no-unsupported-features/es-syntax */

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("info-verbosity-verbose", async done => {
const result = await runWatch(__dirname, [
const webpackProc = runAndGetWatchProc(__dirname, [
"--entry ",
"./index.js",
"--config",
Expand All @@ -22,15 +48,62 @@ test("info-verbosity-verbose", async done => {
"--info-verbosity",
"verbose"
]);
const { stdout, stderr } = result;

const summary = extractSummary(stdout);
// Watch mode with --info-verbosity verbose does not spit the output in one go.
// So we need to keep a track of chunks output order
// 1. Compilation starting...
// 2. webpack is watching the files...
// 3. Compilation finished
// 4. Hash and other info
// 5. Compilation starting...
// 6. Compilation finished
// 7. Hash and other info
var chunkNumber = 0;
var hash1, hash2;

webpackProc.stdout.on("data", data => {
data = data.toString();
chunkNumber++;

switch (chunkNumber) {
case 1:
case 5:
expect(data).toContain("Compilation starting");
break;
case 2:
expect(data).toContain("webpack is watching the files");
break;
case 3:
case 6:
expect(data).toContain("Compilation finished");
break;
case 4:
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");

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

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

expect(summary).toEqual(expect.anything());
expect(summary).toContain("Compilation");
expect(summary).toContain("webpack is watching the files…");
webpackProc.kill();
done();
break;
default:
break;
}
});

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

0 comments on commit 42e5ee8

Please sign in to comment.