/
watch-flag.test.js
82 lines (67 loc) · 2.65 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
'use strict';
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 \u001b[1m\u001b[32msuccessfully\u001b[39m\u001b[22m`];
describe('--watch flag', () => {
it('should recompile upon file change', (done) => {
const proc = runAndGetWatchProc(__dirname, ['--watch'], false, '', true);
let semaphore = 0;
proc.stdout.on('data', (chunk) => {
const data = 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'], false, '', true);
let semaphore = 0;
proc.stdout.on('data', (chunk) => {
const data = 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();
}
});
});
});