/
watch-flag.test.js
64 lines (60 loc) · 2.31 KB
/
watch-flag.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
'use strict';
const { runAndGetWatchProc, isWebpack5, isWindows } = require('../utils/test-utils');
const { writeFileSync } = require('fs');
const { resolve } = require('path');
const wordsInStatsv4 = ['Hash', 'Version', 'Time', 'Built at:', 'main.js'];
const wordsInStatsv5 = ['asset', 'index.js', 'compiled successfully'];
describe('--watch flag', () => {
it('should recompile upon file change', (done) => {
const proc = runAndGetWatchProc(__dirname, ['--watch'], false, '', true);
let semaphore = 1;
proc.stdout.on('data', (chunk) => {
const data = chunk.toString();
if (data.includes('watching files for updates')) {
writeFileSync(resolve(__dirname, './src/index.js'), `console.log('watch flag test');`);
semaphore = 0;
return;
}
if (semaphore === 0 && data.includes('index.js')) {
if (isWebpack5) {
for (const word of wordsInStatsv5) {
expect(data).toContain(word);
}
} else {
for (const word of wordsInStatsv4) {
expect(data).toContain(word);
}
}
semaphore--;
proc.kill();
done();
return;
}
});
});
it('should print compilation lifecycle', (done) => {
const proc = runAndGetWatchProc(__dirname, ['--watch'], false, '', true);
let semaphore = 0;
proc.stdout.on('data', (chunk) => {
const data = chunk.toString();
if (data.includes('Compilation starting') || data.includes('Compilation finished')) {
semaphore++;
}
// TODO Fix on windows
if ((isWindows || semaphore === 2) && data.includes('index.js')) {
if (isWebpack5) {
for (const word of wordsInStatsv5) {
expect(data).toContain(word);
}
} else {
for (const word of wordsInStatsv4) {
expect(data).toContain(word);
}
}
proc.kill();
done();
return;
}
});
});
});