Skip to content

Commit 48f34d1

Browse files
hemal7735evenstensberg
authored andcommitted
tests(watch): hash assertion for single-config
1 parent 6dd2327 commit 48f34d1

File tree

2 files changed

+73
-15
lines changed

2 files changed

+73
-15
lines changed

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

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

Lines changed: 72 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,36 @@
55

66
jest.setTimeout(10E6);
77

8-
const { runWatch, extractSummary } = require("../../../testUtils");
8+
const fs = require("fs");
9+
const path = require("path");
10+
const { extractSummary, extractHash, appendDataIfFileExists, runAndGetWatchProc } = require("../../../testUtils");
11+
12+
const fileToChange = "index.js";
13+
const copyFile = "index_copy.js";
14+
const fileToChangePath = path.resolve(__dirname, fileToChange);
15+
const copyFilePath = path.resolve(__dirname, copyFile);
16+
17+
// create copy of "index.js" => "index_copy.js"
18+
beforeEach(() => {
19+
// fs.copyFileSync was added in Added in: v8.5.0
20+
// We should refactor the below code once our minimal supported version is v8.5.0
21+
fs.createReadStream(fileToChangePath).pipe(fs.createWriteStream(copyFilePath));
22+
});
23+
24+
afterEach(() => {
25+
try {
26+
// subsequent test-case runs won't pass as snapshot is not matched
27+
// hence, deleting the file as it is modified by the test
28+
fs.unlinkSync(fileToChangePath);
29+
} catch (e) {
30+
console.warn("could not remove the file:" + fileToChangePath + "\n" + e.message);
31+
} finally {
32+
fs.renameSync(copyFilePath, fileToChangePath);
33+
}
34+
});
935

1036
test("single-config", async(done) => {
11-
const result = await runWatch(__dirname, [
37+
const webpackProc = runAndGetWatchProc(__dirname, [
1238
"--entry",
1339
"./index.js",
1440
"--config",
@@ -22,17 +48,52 @@ test("single-config", async(done) => {
2248
"--watch"
2349
]);
2450

25-
const { stdout, stderr } = result;
51+
// info-verbosity is set to info by default
52+
// It does not spit the output in one go.
53+
// So we need to keep a track of chunks output order
54+
// 1. webpack is watching the files...
55+
// 2. Hash and other info
56+
// 3. (file changed) Hash and other info
57+
var chunkNumber = 0;
58+
var hash1, hash2;
59+
60+
webpackProc.stdout.on("data", data => {
61+
data = data.toString();
62+
chunkNumber++;
63+
64+
console.log(data);
65+
66+
switch (chunkNumber) {
67+
case 1:
68+
expect(data).toContain("webpack is watching the files");
69+
break;
70+
case 2:
71+
expect(extractSummary(data)).toMatchSnapshot();
72+
73+
hash1 = extractHash(data);
74+
75+
// We get webpack output after running test
76+
// Since we are running the webpack in watch mode, changing file will generate additional output
77+
// First time output will be validated fully
78+
// Hash of the The subsequent output will be tested against that of first time output
79+
appendDataIfFileExists(__dirname, fileToChange, "//junk-comment");
2680

27-
const summary = extractSummary(stdout);
81+
break;
82+
case 3:
83+
hash2 = extractHash(data);
2884

29-
expect(summary).toContain("");
30-
expect(summary).toEqual(expect.anything());
31-
expect(summary).toContain("");
32-
expect(summary).toContain("webpack is watching the files…");
85+
expect(hash2.hash).not.toBe(hash1.hash);
3386

34-
expect(stderr).toHaveLength(0);
35-
expect(summary).toMatchSnapshot();
36-
done();
87+
webpackProc.kill();
88+
done();
89+
break;
90+
default:
91+
break;
92+
}
93+
});
3794

95+
webpackProc.stderr.on("data", error => {
96+
// fail test case if there is any error
97+
done(error.toString());
98+
});
3899
});

0 commit comments

Comments
 (0)