/
watch-flag.test.js
83 lines (68 loc) · 2.72 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
'use strict';
const stripAnsi = require('strip-ansi');
const { runAndGetWatchProc, isWebpack5 } = 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', '--mode', 'production'], false, '', true);
let semaphore = 0;
proc.stdout.on('data', (chunk) => {
const data = stripAnsi(chunk.toString());
if (semaphore === 0 && data.includes('watching files for updates')) {
process.nextTick(() => {
writeFileSync(resolve(__dirname, './src/index.js'), `console.log('watch flag test');`);
semaphore++;
});
}
if (semaphore === 1 && 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++;
}
if (semaphore === 2 && data.includes('watching files for updates')) {
proc.kill();
done();
}
});
});
it('should print compilation lifecycle', (done) => {
const proc = runAndGetWatchProc(__dirname, ['--watch', '--mode', 'production'], false, '', true);
let semaphore = 0;
proc.stdout.on('data', (chunk) => {
const data = stripAnsi(chunk.toString());
if (semaphore === 0 && data.includes('Compilation starting')) {
semaphore++;
}
if (semaphore === 1 && data.includes('Compilation finished')) {
semaphore++;
}
if (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);
}
}
semaphore++;
}
if (semaphore === 3 && data.includes('watching files for updates...')) {
semaphore++;
proc.kill();
done();
}
});
});
});