/
cache-flags.test.js
182 lines (152 loc) · 7.66 KB
/
cache-flags.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
'use strict';
const path = require('path');
const rimraf = require('rimraf');
const { run, isWindows } = require('../utils/test-utils');
const { existsSync, writeFileSync, unlinkSync } = require('fs');
const { resolve } = require('path');
describe('cache related flags from core', () => {
beforeEach((done) => {
rimraf(path.join(__dirname, '../../node_modules/.cache/webpack/*'), () => {
done();
});
});
it('should be successful with --cache ', () => {
const { stderr, stdout, exitCode } = run(__dirname, ['--cache']);
expect(stderr).toBeFalsy();
expect(exitCode).toBe(0);
expect(stdout).toContain(`type: 'memory'`);
});
it('should be successful with --no-cache ', () => {
const { stderr, stdout, exitCode } = run(__dirname, ['--no-cache']);
expect(stderr).toBeFalsy();
expect(exitCode).toBe(0);
expect(stdout).toContain('cache: false');
});
it('should set cache.type', () => {
const { stderr, stdout, exitCode } = run(__dirname, ['--cache-type', 'filesystem']);
expect(stderr).toBeFalsy();
expect(exitCode).toBe(0);
expect(stdout).toContain(`type: 'filesystem'`);
});
it('should set cache.cacheDirectory with --cache-cache-directory', () => {
const { stderr, stdout, exitCode } = run(__dirname, ['--cache-type', 'filesystem', '--cache-cache-directory', './test-cache-path']);
expect(stderr).toBeFalsy();
expect(exitCode).toBe(0);
expect(stdout).toContain('test-cache-path');
expect(existsSync(resolve(__dirname, './test-cache-path'))).toBeTruthy();
});
it('should set cache.cacheLocation with --cache-cache-locations', () => {
const { stderr, stdout, exitCode } = run(__dirname, [
'--cache-type',
'filesystem',
'--cache-cache-location',
'./test-locate-cache',
]);
expect(stderr).toBeFalsy();
expect(exitCode).toBe(0);
expect(stdout).toContain('test-locate-cache');
expect(existsSync(resolve(__dirname, './test-locate-cache'))).toBeTruthy();
});
it('should set cache.hashAlgorithm with --cache-hash-algorithm', () => {
const { stderr, stdout, exitCode } = run(__dirname, ['--cache-type', 'filesystem', '--cache-hash-algorithm', 'sha256']);
expect(stderr).toBeFalsy();
expect(exitCode).toBe(0);
expect(stdout).toContain(`hashAlgorithm: 'sha256'`);
});
it('should set cache.name with --cache-name', () => {
const { stderr, stdout, exitCode } = run(__dirname, ['--cache-type', 'filesystem', '--cache-name', 'cli-test']);
expect(stderr).toBeFalsy();
expect(exitCode).toBe(0);
expect(stdout).toContain(`name: 'cli-test'`);
});
it('should set cache.store with --cache-store', () => {
const { stderr, stdout, exitCode } = run(__dirname, ['--cache-type', 'filesystem', '--cache-store', 'pack']);
expect(stderr).toBeFalsy();
expect(exitCode).toBe(0);
expect(stdout).toContain(`store: 'pack'`);
});
it('should set cache.version with --cache-version', () => {
const { stderr, stdout, exitCode } = run(__dirname, ['--cache-type', 'filesystem', '--cache-version', '1.1.3']);
expect(stderr).toBeFalsy();
expect(exitCode).toBe(0);
expect(stdout).toContain(`version: '1.1.3'`);
});
it('should assign cache build dependencies correctly when cache type is filesystem', () => {
// TODO: Fix on windows
if (isWindows) return;
const { stderr, stdout, exitCode } = run(__dirname, ['--cache-type', 'filesystem', '-c', './webpack.config.js']);
expect(stderr).toBeFalsy();
expect(exitCode).toBe(0);
expect(stdout).toContain('buildDependencies');
expect(stdout).toContain("config: [ './webpack.config.js' ]");
expect(stdout).not.toContain('[cached]');
// Run again to check for cache
const newRun = run(__dirname, ['--cache-type', 'filesystem', '-c', './webpack.config.js']);
expect(newRun.stdout).toContain('[cached]');
expect(newRun.stderr).toBeFalsy();
expect(newRun.exitCode).toEqual(0);
});
it('should assign cache build dependencies correctly when cache type is filesystem in config', () => {
// TODO: Fix on windows
if (isWindows) return;
const { stderr, stdout, exitCode } = run(__dirname, ['-c', './webpack.cache.config.js']);
expect(stderr).toBeFalsy();
expect(exitCode).toBe(0);
expect(stdout).toContain('buildDependencies');
expect(stdout).toContain("config: [ './webpack.cache.config.js' ]");
expect(stdout).toContain("type: 'filesystem'");
// Run again to check for cache
const newRun = run(__dirname, ['-c', './webpack.cache.config.js']);
expect(newRun.stdout).toContain('[cached]');
expect(newRun.stderr).toBeFalsy();
expect(newRun.exitCode).toEqual(0);
});
it('should assign cache build dependencies with multiple configs', () => {
// TODO: Fix on windows
if (isWindows) return;
const { stderr, stdout, exitCode } = run(__dirname, ['-c', './webpack.cache.config.js', '-c', './webpack.config.js']);
expect(stderr).toBeFalsy();
expect(stdout).toContain('buildDependencies');
expect(stdout).toContain("config: [ './webpack.cache.config.js', './webpack.config.js' ]");
expect(stdout).toContain("type: 'filesystem'");
expect(exitCode).toEqual(0);
});
it('should assign cache build dependencies with default config', () => {
// TODO: Fix on windows
if (isWindows) return;
const { stderr, stdout, exitCode } = run(__dirname, ['--cache-type', 'filesystem']);
expect(stderr).toBeFalsy();
expect(stdout).toContain('buildDependencies');
expect(stdout).toContain(`'${path.join(__dirname, './webpack.config.js')}'`);
expect(stdout).toContain("type: 'filesystem'");
expect(exitCode).toEqual(0);
});
it('should assign cache build dependencies with merged configs', () => {
// TODO: Fix on windows
if (isWindows) return;
const { stderr, stdout, exitCode } = run(__dirname, ['-c', './webpack.cache.config.js', '-c', './webpack.config.js', '--merge']);
expect(stderr).toBeFalsy();
expect(stdout).toContain('buildDependencies');
expect(stdout).toContain("config: [ './webpack.cache.config.js', './webpack.config.js' ]");
expect(stdout).toContain("type: 'filesystem'");
expect(exitCode).toEqual(0);
});
it('should invalidate cache when config changes', () => {
// TODO: Fix on windows
if (isWindows) return;
// Creating a temporary webpack config
writeFileSync(resolve(__dirname, './webpack.test.config.js'), 'module.exports = {mode: "development"}');
const { stderr, stdout } = run(__dirname, ['--cache-type', 'filesystem', '-c', './webpack.test.config.js']);
expect(stderr).toBeFalsy();
expect(stdout).not.toContain('[cached]');
// Running again should use the cache
const newRun = run(__dirname, ['--cache-type', 'filesystem', '-c', './webpack.test.config.js']);
expect(newRun.stdout).toContain('[cached]');
// Change config to invalidate cache
writeFileSync(resolve(__dirname, './webpack.test.config.js'), 'module.exports = {mode: "production"}');
const newRun2 = run(__dirname, ['--cache-type', 'filesystem', '-c', './webpack.test.config.js']);
unlinkSync(resolve(__dirname, './webpack.test.config.js'));
expect(newRun2).not.toContain('[cached]');
expect(newRun2.exitCode).toEqual(0);
});
});