/
single-config-watch-opt.test.js
94 lines (80 loc) · 2.34 KB
/
single-config-watch-opt.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
"use strict";
jest.setTimeout(10e6);
/* eslint-disable node/no-unsupported-features */
/* eslint-disable node/no-unsupported-features/es-syntax */
const fs = require("fs");
const path = require("path");
const {
extractSummary,
extractHash,
appendDataIfFileExists,
runAndGetWatchProc,
copyFile
} = require("../../../testUtils");
const fileToChange = "index.js";
const fileToChangePath = path.resolve(__dirname, fileToChange);
var copyFilePath;
// create copy of "index.js" => "index_copy.js"
beforeEach(() => {
copyFilePath = copyFile(__dirname, fileToChange);
});
afterEach(() => {
try {
// subsequent test-case runs won't pass as snapshot is not matched
// hence, deleting the file as it is modified by the test
fs.unlinkSync(fileToChangePath);
} catch (e) {
console.warn("could not remove the file:" + fileToChangePath + "\n" + e.message);
} finally {
fs.renameSync(copyFilePath, fileToChangePath);
}
});
test.skip("single-config-watch-opt", done => {
const webpackProc = runAndGetWatchProc(__dirname, [
"--entry",
"./index.js",
"--config",
"./webpack.config.js",
"--output-filename",
"[name].js",
"--output-chunk-filename",
"[id].chunk.js",
"--target",
"async-node",
"--watch"
]);
// info-verbosity is set to info by default
// It does not spit the output in one go.
// So we need to keep a track of chunks output order
// 1. webpack is watching the files...
// 2. Hash and other info
// 3. (file changed) Hash and other info
var chunkNumber = 0;
var hash1, hash2;
webpackProc.stdout.on("data", data => {
data = data.toString();
chunkNumber++;
switch (chunkNumber) {
case 1:
expect(extractSummary(data)).toMatchSnapshot();
hash1 = extractHash(data);
// We get webpack output after running test
// Since we are running the webpack in watch mode, changing file will generate additional output
// First time output will be validated fully
// Hash of the The subsequent output will be tested against that of first time output
appendDataIfFileExists(__dirname, fileToChange, "//junk-comment");
break;
case 2:
hash2 = extractHash(data);
expect(hash2.hash).not.toBe(hash1.hash);
webpackProc.kill();
done();
break;
default:
break;
}
});
webpackProc.stderr.on("data", error => {
expect(error.toString()).toContain("webpack is watching the files");
});
});