Skip to content

Commit 6dd2327

Browse files
hemal7735evenstensberg
authored andcommitted
tests(watch): hash assertion for multi-config-watch-opt
1 parent 6b4d339 commit 6dd2327

File tree

2 files changed

+75
-16
lines changed

2 files changed

+75
-16
lines changed

test/binCases/watch/multi-config-watch-opt/__snapshots__/multi-config-watch-opt.test.js.snap

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

33
exports[`multi-config-watch-opt 1`] = `
4-
"
5-
webpack is watching the files…
6-
7-
Child
4+
"Child
85
Asset Size Chunks Chunk Names
96
null.js 945 bytes 0 [emitted] null
107
Entrypoint null = null.js
Lines changed: 74 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,39 @@
11
"use strict";
22

3+
jest.setTimeout(10E6);
34
/* eslint-disable node/no-unsupported-features */
45
/* eslint-disable node/no-unsupported-features/es-syntax */
56

6-
jest.setTimeout(10E6);
7+
const fs = require("fs");
8+
const path = require("path");
9+
const { extractSummary, extractHash, appendDataIfFileExists, runAndGetWatchProc } = require("../../../testUtils");
710

8-
const { runWatch, extractSummary } = require("../../../testUtils");
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+
});
934

1035
test("multi-config-watch-opt", async done => {
11-
const result = await runWatch(__dirname, [
36+
const webpackProc = runAndGetWatchProc(__dirname, [
1237
"--entry",
1338
"./index.js",
1439
"--config",
@@ -22,16 +47,53 @@ test("multi-config-watch-opt", async done => {
2247
"--watch"
2348
]);
2449

25-
const { stdout, stderr } = result;
50+
// info-verbosity is set to info by default
51+
// It does not spit the output in one go.
52+
// So we need to keep a track of chunks output order
53+
// 1. webpack is watching the files...
54+
// 2. Hash and other info
55+
// 3. (file changed) Hash and other info
56+
var chunkNumber = 0;
57+
var hash1, hash2;
58+
59+
webpackProc.stdout.on("data", data => {
60+
data = data.toString();
61+
chunkNumber++;
2662

27-
const summary = extractSummary(stdout);
63+
console.log(data);
2864

29-
expect(summary).toContain("");
30-
expect(summary).toEqual(expect.anything());
31-
expect(summary).toContain("");
32-
expect(summary).toContain("webpack is watching the files…");
65+
switch (chunkNumber) {
66+
case 1:
67+
expect(data).toContain("webpack is watching the files");
68+
break;
69+
case 2:
70+
expect(extractSummary(data)).toMatchSnapshot();
3371

34-
expect(stderr).toHaveLength(0);
35-
expect(summary).toMatchSnapshot();
36-
done();
72+
hash1 = extractHash(data);
73+
74+
// We get webpack output after running test
75+
// Since we are running the webpack in watch mode, changing file will generate additional output
76+
// First time output will be validated fully
77+
// Hash of the The subsequent output will be tested against that of first time output
78+
appendDataIfFileExists(__dirname, fileToChange, "//junk-comment");
79+
80+
break;
81+
case 3:
82+
hash2 = extractHash(data);
83+
84+
expect(hash2.hash).not.toBe(hash1.hash);
85+
86+
webpackProc.kill();
87+
done();
88+
break;
89+
default:
90+
break;
91+
}
92+
});
93+
94+
webpackProc.stderr.on("data", error => {
95+
// fail test case if there is any error
96+
done(error.toString());
97+
});
3798
});
99+

0 commit comments

Comments
 (0)