-
Notifications
You must be signed in to change notification settings - Fork 373
Expand file tree
/
Copy pathbundlerUtils.spec.js
More file actions
118 lines (92 loc) · 3.96 KB
/
bundlerUtils.spec.js
File metadata and controls
118 lines (92 loc) · 3.96 KB
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
/* eslint-disable max-classes-per-file */
/* eslint-disable global-require */
/**
* Unit tests for bundlerUtils.js. The utility returns the active bundler
* module based on shakapacker.yml's `assets_bundler` setting; it supports
* both webpack and rspack.
*/
jest.mock('@rspack/core', () => ({
ProvidePlugin: class MockRspackProvidePlugin {},
CssExtractRspackPlugin: class MockCssExtractRspackPlugin {},
optimize: { LimitChunkCountPlugin: class MockRspackLimitChunkCount {} },
}));
jest.mock('webpack', () => ({
ProvidePlugin: class MockWebpackProvidePlugin {},
DefinePlugin: class MockWebpackDefinePlugin {},
optimize: { LimitChunkCountPlugin: class MockWebpackLimitChunkCount {} },
}));
describe('bundlerUtils', () => {
let mockConfig;
beforeEach(() => {
jest.resetModules();
mockConfig = { assets_bundler: 'webpack' };
});
afterEach(() => {
jest.clearAllMocks();
});
describe('getBundler()', () => {
it('returns rspack when assets_bundler is rspack', () => {
mockConfig.assets_bundler = 'rspack';
jest.doMock('shakapacker', () => ({ config: mockConfig }));
const utils = require('../../../config/webpack/bundlerUtils');
const bundler = utils.getBundler();
expect(bundler).toBeDefined();
expect(bundler.CssExtractRspackPlugin).toBeDefined();
expect(bundler.CssExtractRspackPlugin.name).toBe('MockCssExtractRspackPlugin');
});
it('returns webpack when assets_bundler is webpack', () => {
mockConfig.assets_bundler = 'webpack';
jest.doMock('shakapacker', () => ({ config: mockConfig }));
const utils = require('../../../config/webpack/bundlerUtils');
const bundler = utils.getBundler();
expect(bundler).toBeDefined();
expect(bundler.DefinePlugin).toBeDefined();
expect(bundler.DefinePlugin.name).toBe('MockWebpackDefinePlugin');
});
it('returns webpack when assets_bundler is undefined', () => {
mockConfig.assets_bundler = undefined;
jest.doMock('shakapacker', () => ({ config: mockConfig }));
const utils = require('../../../config/webpack/bundlerUtils');
const bundler = utils.getBundler();
expect(bundler.DefinePlugin).toBeDefined();
});
it('returns cached bundler on subsequent calls', () => {
mockConfig.assets_bundler = 'webpack';
jest.doMock('shakapacker', () => ({ config: mockConfig }));
const utils = require('../../../config/webpack/bundlerUtils');
const bundler1 = utils.getBundler();
const bundler2 = utils.getBundler();
expect(bundler1).toBe(bundler2);
});
});
describe('isRspack()', () => {
it('returns true when assets_bundler is rspack', () => {
mockConfig.assets_bundler = 'rspack';
jest.doMock('shakapacker', () => ({ config: mockConfig }));
const utils = require('../../../config/webpack/bundlerUtils');
expect(utils.isRspack()).toBe(true);
});
it('returns false when assets_bundler is webpack', () => {
mockConfig.assets_bundler = 'webpack';
jest.doMock('shakapacker', () => ({ config: mockConfig }));
const utils = require('../../../config/webpack/bundlerUtils');
expect(utils.isRspack()).toBe(false);
});
});
describe('getCssExtractPlugin()', () => {
it('returns CssExtractRspackPlugin when using rspack', () => {
mockConfig.assets_bundler = 'rspack';
jest.doMock('shakapacker', () => ({ config: mockConfig }));
const utils = require('../../../config/webpack/bundlerUtils');
const plugin = utils.getCssExtractPlugin();
expect(plugin).toBeDefined();
expect(plugin.name).toBe('MockCssExtractRspackPlugin');
});
it('throws when assets_bundler is not rspack', () => {
mockConfig.assets_bundler = 'webpack';
jest.doMock('shakapacker', () => ({ config: mockConfig }));
const utils = require('../../../config/webpack/bundlerUtils');
expect(() => utils.getCssExtractPlugin()).toThrow('only available when assets_bundler is rspack');
});
});
});