-
Notifications
You must be signed in to change notification settings - Fork 373
Expand file tree
/
Copy pathbundlerUtils.spec.js
More file actions
144 lines (113 loc) · 4.91 KB
/
bundlerUtils.spec.js
File metadata and controls
144 lines (113 loc) · 4.91 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
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
/* eslint-disable max-classes-per-file */
/* eslint-disable global-require */
/**
* Unit tests for bundlerUtils.js
* Tests bundler auto-detection and helper functions
*
* Note: These tests verify the bundler selection logic without actually
* loading Rspack (which requires Node.js globals not available in jsdom).
* We use require() inside tests to ensure proper mocking order.
*/
// Mock the bundler packages to avoid loading them
jest.mock('webpack', () => ({
ProvidePlugin: class MockProvidePlugin {},
optimize: { LimitChunkCountPlugin: class MockLimitChunkCount {} },
}));
jest.mock('@rspack/core', () => ({
ProvidePlugin: class MockRspackProvidePlugin {},
CssExtractRspackPlugin: class MockCssExtractRspackPlugin {},
optimize: { LimitChunkCountPlugin: class MockRspackLimitChunkCount {} },
}));
jest.mock('mini-css-extract-plugin', () => class MiniCssExtractPlugin {});
describe('bundlerUtils', () => {
let mockConfig;
beforeEach(() => {
// Reset module cache
jest.resetModules();
// Create fresh mock config
mockConfig = { assets_bundler: 'webpack' };
});
afterEach(() => {
jest.clearAllMocks();
});
describe('getBundler()', () => {
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.ProvidePlugin).toBeDefined();
expect(bundler.ProvidePlugin.name).toBe('MockProvidePlugin');
});
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();
// Rspack has CssExtractRspackPlugin
expect(bundler.CssExtractRspackPlugin).toBeDefined();
expect(bundler.CssExtractRspackPlugin.name).toBe('MockCssExtractRspackPlugin');
});
});
describe('isRspack()', () => {
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);
});
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);
});
});
describe('getCssExtractPlugin()', () => {
it('returns mini-css-extract-plugin when using webpack', () => {
mockConfig.assets_bundler = 'webpack';
jest.doMock('shakapacker', () => ({ config: mockConfig }));
const utils = require('../../../config/webpack/bundlerUtils');
const plugin = utils.getCssExtractPlugin();
expect(plugin).toBeDefined();
expect(plugin.name).toBe('MiniCssExtractPlugin');
});
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();
// Rspack plugin class name
expect(plugin.name).toBe('MockCssExtractRspackPlugin');
});
});
describe('Edge cases and error handling', () => {
it('defaults to 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).toBeDefined();
expect(bundler.ProvidePlugin.name).toBe('MockProvidePlugin');
});
it('throws error for invalid bundler type', () => {
mockConfig.assets_bundler = 'invalid-bundler';
jest.doMock('shakapacker', () => ({ config: mockConfig }));
const utils = require('../../../config/webpack/bundlerUtils');
expect(() => utils.getBundler()).toThrow('Invalid assets_bundler: "invalid-bundler"');
expect(() => utils.getBundler()).toThrow('Must be one of: webpack, rspack');
});
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();
// Should return same instance (memoized)
expect(bundler1).toBe(bundler2);
});
});
});