Skip to content

Commit 6b4d339

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

2 files changed

Lines changed: 75 additions & 13 deletions

File tree

test/binCases/watch/multi-config/__snapshots__/multi-config.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 1`] = `
4-
"
5-
webpack is watching the files…
6-
7-
Child
4+
"Child
85
Asset Size Chunks Chunk Names
96
main.js 945 bytes 0 [emitted] main
107
Entrypoint main = main.js

test/binCases/watch/multi-config/multi-config.test.js

Lines changed: 74 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,83 @@ 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("multi-config", async done => {
10-
const result = await runWatch(__dirname);
11-
const { stdout, stderr } = result;
36+
const webpackProc = runAndGetWatchProc(__dirname);
37+
38+
// info-verbosity is set to info by default
39+
// It does not spit the output in one go.
40+
// So we need to keep a track of chunks output order
41+
// 1. webpack is watching the files...
42+
// 2. Hash and other info
43+
// 3. (file changed) Hash and other info
44+
var chunkNumber = 0;
45+
var hash1, hash2;
46+
47+
webpackProc.stdout.on("data", data => {
48+
data = data.toString();
49+
chunkNumber++;
50+
51+
console.log(data);
52+
53+
switch (chunkNumber) {
54+
case 1:
55+
expect(data).toContain("webpack is watching the files");
56+
break;
57+
case 2:
58+
expect(extractSummary(data)).toMatchSnapshot();
59+
60+
hash1 = extractHash(data);
61+
62+
// We get webpack output after running test
63+
// Since we are running the webpack in watch mode, changing file will generate additional output
64+
// First time output will be validated fully
65+
// Hash of the The subsequent output will be tested against that of first time output
66+
appendDataIfFileExists(__dirname, fileToChange, "//junk-comment");
67+
68+
break;
69+
case 3:
70+
hash2 = extractHash(data);
1271

13-
const summary = extractSummary(stdout);
72+
expect(hash2.hash).not.toBe(hash1.hash);
1473

15-
expect(summary).toContain("");
16-
expect(summary).toContain("webpack is watching the files…");
74+
webpackProc.kill();
75+
done();
76+
break;
77+
default:
78+
break;
79+
}
80+
});
1781

18-
expect(stderr).toHaveLength(0);
19-
expect(summary).toMatchSnapshot();
20-
done();
82+
webpackProc.stderr.on("data", error => {
83+
// fail test case if there is any error
84+
done(error.toString());
85+
});
2186
});

0 commit comments

Comments
 (0)