-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.test.js
104 lines (87 loc) · 3.26 KB
/
index.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
import 'jest';
import { withPlugins, extend } from '../index';
const PHASE_DEVELOPMENT_SERVER = 'phase-development-server';
const PHASE_PRODUCTION_SERVER = 'phase-production-server';
describe('next-compose-plugins', () => {
it('extends a base config', () => {
const plugin1 = jest.fn(nextConfig => ({ ...nextConfig, plugin1: true }));
const plugin2 = jest.fn(nextConfig => ({ ...nextConfig, plugin2: true }));
const plugin3 = jest.fn(nextConfig => ({ ...nextConfig, plugin3: true }));
const baseConfig = withPlugins([plugin1, plugin2], {
baseConfig: 'hello',
foo: 'bar',
});
const extendedConfig = extend(baseConfig).withPlugins([plugin3], { foo: 'baz', extendedConfig: 'world' });
expect(extendedConfig(PHASE_DEVELOPMENT_SERVER, { defaultConfig: { nextConfig: 'abc' } })).toEqual({
plugin1: true,
plugin2: true,
plugin3: true,
baseConfig: 'hello',
extendedConfig: 'world',
foo: 'baz',
nextConfig: 'abc',
});
expect(plugin1).toHaveBeenCalledTimes(1);
expect(plugin2).toHaveBeenCalledTimes(1);
expect(plugin3).toHaveBeenCalledTimes(1);
});
it('passes the current phase to the extended config', () => {
const plugin1 = jest.fn(nextConfig => ({ ...nextConfig, plugin1: true }));
const plugin2 = jest.fn(nextConfig => ({ ...nextConfig, plugin2: true }));
const plugin3 = jest.fn(nextConfig => ({ ...nextConfig, plugin3: true }));
const plugin4 = jest.fn(nextConfig => ({ ...nextConfig, plugin4: true }));
const baseConfig = withPlugins([
[plugin1, [PHASE_DEVELOPMENT_SERVER]],
[plugin2, [PHASE_PRODUCTION_SERVER]],
], {
baseConfig: 'hello',
foo: 'bar',
});
const extendedConfig = extend(baseConfig).withPlugins([
[plugin3, [PHASE_DEVELOPMENT_SERVER]],
[plugin4, [PHASE_PRODUCTION_SERVER]],
], { foo: 'baz', extendedConfig: 'world' });
expect(extendedConfig(PHASE_PRODUCTION_SERVER, { defaultConfig: { nextConfig: 'abc' } })).toEqual({
plugin2: true,
plugin4: true,
baseConfig: 'hello',
extendedConfig: 'world',
foo: 'baz',
nextConfig: 'abc',
});
expect(plugin1).toHaveBeenCalledTimes(0);
expect(plugin2).toHaveBeenCalledTimes(1);
expect(plugin3).toHaveBeenCalledTimes(0);
expect(plugin4).toHaveBeenCalledTimes(1);
});
it('extends the webpack config', () => {
const plugin1 = jest.fn(nextConfig => ({
...nextConfig,
webpack: () => {
if (nextConfig.webpack) {
return `changed from base ${nextConfig.webpack()}`;
}
return null;
},
}));
const plugin2 = jest.fn(nextConfig => ({
...nextConfig,
webpack: () => {
if (nextConfig.webpack) {
return `changed from current ${nextConfig.webpack()}`;
}
return null;
},
}));
const baseConfig = withPlugins([plugin1]);
const extendedConfig = extend(baseConfig).withPlugins([plugin2]);
const result = extendedConfig(PHASE_DEVELOPMENT_SERVER, {
defaultConfig: {
webpack: () => 'webpack config',
},
});
expect(result.webpack()).toEqual('changed from current changed from base webpack config');
expect(plugin1).toHaveBeenCalledTimes(1);
expect(plugin2).toHaveBeenCalledTimes(1);
});
});