Skip to content

Commit 15dd25f

Browse files
committed
[React] improve error handling in resolveReactComponent
1 parent bffdae8 commit 15dd25f

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/React/assets/src/register_controller.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ export function registerReactControllerComponents(context: __WebpackModuleApi.Re
3737
const possibleValues = Object.keys(reactControllers).map((key) =>
3838
key.replace('./', '').replace('.jsx', '').replace('.tsx', '')
3939
);
40+
41+
if (possibleValues.includes(name)) {
42+
throw new Error(`React controller "${name}" could not be resolved. Ensure the module exports the controller as default.`);
43+
}
44+
4045
throw new Error(`React controller "${name}" does not exist. Possible values: ${possibleValues.join(', ')}`);
4146
}
4247

src/React/assets/test/register_controller.test.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const createFakeFixturesContext = (): RequireContext => {
1919
const files: any = {
2020
'./MyJsxComponent.jsx': { default: MyJsxComponent },
2121
'./MyTsxComponent.tsx': { default: MyTsxComponent },
22+
'./NoDefaultExportComponent.jsx': { default: undefined },
2223
};
2324

2425
const context = (id: string): any => files[id];
@@ -45,4 +46,11 @@ describe('registerReactControllerComponents', () => {
4546

4647
expect(() => resolveComponent('MyABCComponent')).toThrow('React controller "MyABCComponent" does not exist. Possible values: MyJsxComponent, MyTsxComponent');
4748
});
49+
50+
it('throws when no default export found in imported module', () => {
51+
registerReactControllerComponents(createFakeFixturesContext());
52+
const resolveComponent = (window as any).resolveReactComponent;
53+
54+
expect(() => resolveComponent('NoDefaultExportComponent')).toThrow('React controller "NoDefaultExportComponent" could not be resolved. Ensure the module exports the controller as default.');
55+
});
4856
});

0 commit comments

Comments
 (0)