Skip to content

Commit e0a0d97

Browse files
hemal7735evenstensberg
authored andcommittedFeb 5, 2019
tests(watch): hash assertion for info-verbosity-off
1 parent 1d2ccd5 commit e0a0d97

File tree

1 file changed

+63
-11
lines changed

1 file changed

+63
-11
lines changed
 

‎test/binCases/watch/info-verbosity-off/info-verbosity-off.test.js

+63-11
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,38 @@ jest.setTimeout(10e6);
44
/* eslint-disable node/no-unsupported-features */
55
/* eslint-disable node/no-unsupported-features/es-syntax */
66

7-
const { runWatch, extractSummary } = require("../../../testUtils");
7+
const { extractSummary, extractHash, appendDataIfFileExists, runAndGetWatchProc } = require("../../../testUtils");
8+
const fs = require("fs");
9+
const path = require("path");
810

9-
test("info-verbosity-off", async done => {
10-
const result = await runWatch(__dirname, [
11+
const fileToChange = "index.js";
12+
const copyFile = "index_copy.js";
13+
const fileToChangePath = path.resolve(__dirname, fileToChange);
14+
const copyFilePath = path.resolve(__dirname, copyFile);
15+
16+
// create copy of "index.js" => "index_copy.js"
17+
beforeEach(() => {
18+
// fs.copyFileSync was added in Added in: v8.5.0
19+
// We should migrate it once we stop support for v6.x
20+
fs.createReadStream(fileToChangePath).pipe(fs.createWriteStream(copyFilePath));
21+
});
22+
23+
afterEach(() => {
24+
try {
25+
// deleting the file as it is modified by the test
26+
// subsequent test-case runs won't pass as snapshot is not matched
27+
fs.unlinkSync(fileToChangePath);
28+
} catch (e) {
29+
console.warn("could not remove the file:" + fileToChangePath + "\n" + e.message);
30+
} finally {
31+
fs.renameSync(copyFilePath, fileToChangePath);
32+
}
33+
});
34+
35+
// It is modifying the index.js
36+
// Which breaks the test-cases second time
37+
test.only("info-verbosity-off", async done => {
38+
var webpackProc = runAndGetWatchProc(__dirname, [
1139
"--entry ",
1240
"./index.js",
1341
"--config",
@@ -22,15 +50,39 @@ test("info-verbosity-off", async done => {
2250
"--info-verbosity",
2351
"none"
2452
]);
25-
const { stdout, stderr } = result;
2653

27-
const summary = extractSummary(stdout);
54+
var outputCount = 0;
55+
var hash1;
56+
57+
webpackProc.stdout.on("data", data => {
58+
data = data.toString();
59+
60+
if (outputCount === 0) {
61+
hash1 = extractHash(data);
62+
63+
const summary = extractSummary(data);
64+
65+
expect(summary).toEqual(expect.anything());
66+
expect(summary).toContain("");
67+
expect(summary).not.toContain("webpack is watching the files…");
68+
expect(summary).toMatchSnapshot();
69+
70+
// change file
71+
appendDataIfFileExists(__dirname, fileToChange, "//junk-comment");
72+
73+
outputCount++;
74+
} else {
75+
const hash2 = extractHash(data);
2876

29-
expect(summary).toEqual(expect.anything());
30-
expect(summary).toContain("");
31-
expect(summary).not.toContain("webpack is watching the files…");
77+
expect(hash2.hash).not.toBe(hash1.hash);
78+
webpackProc.kill();
79+
done();
80+
}
81+
});
3282

33-
expect(stderr).toHaveLength(0);
34-
expect(summary).toMatchSnapshot();
35-
done();
83+
webpackProc.stderr.on("data", error => {
84+
// fail test case if there is any error
85+
expect(true).toBe(false);
86+
done();
87+
});
3688
});

0 commit comments

Comments
 (0)