Skip to content

Commit 16265b2

Browse files
fix: resolve test failures in SharePlugin and ProvideSharedModule
- Remove SharePlugin validation that prevents empty shared configurations - Fix ProvideSharedModule test mocking to include required moduleGraph methods - Add proper updateHash implementation with correct hash context 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 0b0cb45 commit 16265b2

File tree

4 files changed

+96
-22
lines changed

4 files changed

+96
-22
lines changed

packages/enhanced/src/lib/sharing/ProvideSharedModule.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ import type {
1414
ResolverWithOptions,
1515
ObjectDeserializerContext,
1616
ObjectSerializerContext,
17+
UpdateHashContext,
1718
} from 'webpack/lib/Module';
19+
import type { Hash } from 'webpack/lib/util/Hash';
1820
import ProvideForSharedDependency from './ProvideForSharedDependency';
1921
import { WEBPACK_MODULE_TYPE_PROVIDE } from '../Constants';
2022
import type { InputFileSystem } from 'webpack/lib/util/fs';
@@ -159,6 +161,27 @@ class ProvideSharedModule extends Module {
159161
return 42;
160162
}
161163

164+
/**
165+
* @param {Hash} hash the hash used to track dependencies
166+
* @param {UpdateHashContext} context context
167+
* @returns {void}
168+
*/
169+
override updateHash(hash: Hash, context: UpdateHashContext): void {
170+
hash.update(
171+
JSON.stringify({
172+
shareScope: this._shareScope,
173+
name: this._name,
174+
version: this._version,
175+
request: this._request,
176+
eager: this._eager,
177+
requiredVersion: this._requiredVersion,
178+
strictVersion: this._strictVersion,
179+
singleton: this._singleton,
180+
}),
181+
);
182+
super.updateHash(hash, context);
183+
}
184+
162185
/**
163186
* @returns {Set<string>} types available (do not mutate)
164187
*/

packages/enhanced/src/lib/sharing/SharePlugin.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,7 @@ class SharePlugin {
100100
},
101101
}));
102102

103-
// Validate that at least one shared module is configured
104-
if (sharedOptions.length === 0) {
105-
throw new Error(
106-
'SharePlugin requires at least one shared module configuration',
107-
);
108-
}
103+
// Empty shared configurations are allowed, following webpack's standard behavior
109104

110105
this._shareScope = options.shareScope || 'default';
111106
this._consumes = consumes;

packages/enhanced/test/unit/container/utils.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,12 @@ export function createWebpackMock() {
366366
this.blocks = [];
367367
}
368368

369+
updateHash(hash: any, context: any) {
370+
// Mock implementation of updateHash that matches webpack's Module class
371+
hash.update(this.type);
372+
if (this.layer) hash.update(this.layer);
373+
}
374+
369375
serialize(context: any) {
370376
const { write } = context;
371377
write(this.type);

packages/enhanced/test/unit/sharing/ProvideSharedModule.test.ts

Lines changed: 66 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -350,22 +350,72 @@ describe('ProvideSharedModule', () => {
350350
update: jest.fn(),
351351
};
352352

353-
// Skip this test as the updateHash method might not be available
354-
// or might be implemented differently in the real module
355-
// Just verify the hash.update method was called
356-
if (typeof module.updateHash === 'function') {
357-
const context = {
358-
chunkGraph: {
359-
getModuleGraphHash: jest.fn().mockReturnValue('mock-hash'),
360-
},
361-
runtime: undefined,
362-
};
363-
module.updateHash(hash as any, context as any);
364-
expect(hash.update).toHaveBeenCalled();
365-
} else {
366-
// Skip the test if updateHash is not available
367-
expect(true).toBe(true);
368-
}
353+
const context = {
354+
chunkGraph: {
355+
getModuleGraphHash: jest.fn().mockReturnValue('mock-hash'),
356+
},
357+
moduleGraph: {
358+
getModuleGraphHash: jest.fn().mockReturnValue('mock-hash'),
359+
},
360+
runtime: undefined,
361+
};
362+
363+
module.updateHash(hash as any, context as any);
364+
365+
expect(hash.update).toHaveBeenCalledWith(
366+
JSON.stringify({
367+
shareScope: shareScopes.string,
368+
name: 'react',
369+
version: '17.0.2',
370+
request: './react',
371+
eager: false,
372+
requiredVersion: '^17.0.0',
373+
strictVersion: false,
374+
singleton: true,
375+
}),
376+
);
377+
});
378+
379+
it('should update hash with array shareScope and different options', () => {
380+
const module = new ProvideSharedModule(
381+
shareScopes.array, // shareScope
382+
'vue', // name
383+
false, // version
384+
'./vue', // request
385+
true, // eager
386+
false, // requiredVersion
387+
true, // strictVersion
388+
false, // singleton
389+
);
390+
391+
const hash = {
392+
update: jest.fn(),
393+
};
394+
395+
const context = {
396+
chunkGraph: {
397+
getModuleGraphHash: jest.fn().mockReturnValue('mock-hash'),
398+
},
399+
moduleGraph: {
400+
getModuleGraphHash: jest.fn().mockReturnValue('mock-hash'),
401+
},
402+
runtime: undefined,
403+
};
404+
405+
module.updateHash(hash as any, context as any);
406+
407+
expect(hash.update).toHaveBeenCalledWith(
408+
JSON.stringify({
409+
shareScope: shareScopes.array,
410+
name: 'vue',
411+
version: false,
412+
request: './vue',
413+
eager: true,
414+
requiredVersion: false,
415+
strictVersion: true,
416+
singleton: false,
417+
}),
418+
);
369419
});
370420
});
371421

0 commit comments

Comments
 (0)