Skip to content

Commit 55632d6

Browse files
hemal7735evenstensberg
authored andcommittedFeb 5, 2019
tests(watch): hash assertion for single-config-opt
1 parent 48f34d1 commit 55632d6

File tree

2 files changed

+91
-12
lines changed

2 files changed

+91
-12
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`single-config-watch-opt 1`] = `
4+
" Asset Size Chunks Chunk Names
5+
null.js 930 bytes 0 [emitted] null
6+
Entrypoint null = null.js
7+
[0] ./index.js 0 bytes {0} [built]
8+
9+
WARNING in configuration
10+
The 'mode' option has not been set, webpack will fallback to 'production' for this value. Set 'mode' option to 'development' or 'production' to enable defaults for each environment.
11+
You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/concepts/mode/
12+
"
13+
`;
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,39 @@
11
"use strict";
22

3-
const { runWatch, extractSummary } = require("../../../testUtils");
4-
test("single-config-watch-opt", () => {
5-
runWatch(__dirname, [
3+
jest.setTimeout(10E6);
4+
/* eslint-disable node/no-unsupported-features */
5+
/* eslint-disable node/no-unsupported-features/es-syntax */
6+
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+
});
34+
35+
test("single-config-watch-opt", async done => {
36+
const webpackProc = runAndGetWatchProc(__dirname, [
637
"--entry",
738
"./index.js",
839
"--config",
@@ -14,18 +45,53 @@ test("single-config-watch-opt", () => {
1445
"--target",
1546
"async-node",
1647
"--watch"
17-
]).then(result => {
18-
const { stdout, stderr } = result;
48+
]);
1949

20-
const summary = extractSummary(stdout);
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;
2158

22-
expect(summary).toEqual(expect.anything());
23-
expect(summary).toContain("");
24-
expect(summary).toContain("webpack is watching the files…");
59+
webpackProc.stdout.on("data", data => {
60+
data = data.toString();
61+
chunkNumber++;
2562

26-
expect(stderr).toHaveLength(0);
63+
switch (chunkNumber) {
64+
case 1:
65+
expect(data).toContain("webpack is watching the files");
66+
break;
67+
case 2:
68+
expect(extractSummary(data)).toMatchSnapshot();
2769

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

0 commit comments

Comments
 (0)
Please sign in to comment.