Skip to content

Commit 42e5ee8

Browse files
hemal7735evenstensberg
authored andcommitted
tests(watch): hash assertion info-verbosity-verbose
1 parent 675d5c0 commit 42e5ee8

File tree

2 files changed

+84
-20
lines changed

2 files changed

+84
-20
lines changed

test/binCases/watch/info-verbosity-verbose/__snapshots__/info-verbosity-verbose.test.js.snap

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`info-verbosity-verbose 1`] = `
4-
"
5-
Compilation starting…
6-
7-
8-
webpack is watching the files…
9-
10-
11-
Compilation finished
12-
13-
Asset Size Chunks Chunk Names
4+
" Asset Size Chunks Chunk Names
145
main.js 930 bytes 0 [emitted] main
156
Entrypoint main = main.js
167
[0] ./index.js 0 bytes {0} [built]

test/binCases/watch/info-verbosity-verbose/info-verbosity-verbose.test.js

Lines changed: 83 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,36 @@ 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 fs = require("fs");
8+
const path = require("path");
9+
const { extractSummary, extractHash, appendDataIfFileExists, runAndGetWatchProc } = require("../../../testUtils");
10+
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 refactor the below code once our minimal supported version is v8.5.0
20+
fs.createReadStream(fileToChangePath).pipe(fs.createWriteStream(copyFilePath));
21+
});
22+
23+
afterEach(() => {
24+
try {
25+
// subsequent test-case runs won't pass as snapshot is not matched
26+
// hence, deleting the file as it is modified by the test
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+
});
834

935
test("info-verbosity-verbose", async done => {
10-
const result = await runWatch(__dirname, [
36+
const webpackProc = runAndGetWatchProc(__dirname, [
1137
"--entry ",
1238
"./index.js",
1339
"--config",
@@ -22,15 +48,62 @@ test("info-verbosity-verbose", async done => {
2248
"--info-verbosity",
2349
"verbose"
2450
]);
25-
const { stdout, stderr } = result;
2651

27-
const summary = extractSummary(stdout);
52+
// Watch mode with --info-verbosity verbose does not spit the output in one go.
53+
// So we need to keep a track of chunks output order
54+
// 1. Compilation starting...
55+
// 2. webpack is watching the files...
56+
// 3. Compilation finished
57+
// 4. Hash and other info
58+
// 5. Compilation starting...
59+
// 6. Compilation finished
60+
// 7. Hash and other info
61+
var chunkNumber = 0;
62+
var hash1, hash2;
63+
64+
webpackProc.stdout.on("data", data => {
65+
data = data.toString();
66+
chunkNumber++;
67+
68+
switch (chunkNumber) {
69+
case 1:
70+
case 5:
71+
expect(data).toContain("Compilation starting");
72+
break;
73+
case 2:
74+
expect(data).toContain("webpack is watching the files");
75+
break;
76+
case 3:
77+
case 6:
78+
expect(data).toContain("Compilation finished");
79+
break;
80+
case 4:
81+
expect(extractSummary(data)).toMatchSnapshot();
82+
83+
hash1 = extractHash(data);
84+
85+
// We get webpack output after running test
86+
// Since we are running the webpack in watch mode, changing file will generate additional output
87+
// First time output will be validated fully
88+
// Hash of the The subsequent output will be tested against that of first time output
89+
appendDataIfFileExists(__dirname, fileToChange, "//junk-comment");
90+
91+
break;
92+
case 7:
93+
hash2 = extractHash(data);
94+
95+
expect(hash2.hash).not.toBe(hash1.hash);
2896

29-
expect(summary).toEqual(expect.anything());
30-
expect(summary).toContain("Compilation");
31-
expect(summary).toContain("webpack is watching the files…");
97+
webpackProc.kill();
98+
done();
99+
break;
100+
default:
101+
break;
102+
}
103+
});
32104

33-
expect(stderr).toHaveLength(0);
34-
expect(summary).toMatchSnapshot();
35-
done();
105+
webpackProc.stderr.on("data", error => {
106+
// fail test case if there is any error
107+
done(error.toString());
108+
});
36109
});

0 commit comments

Comments
 (0)