Fix: Handle non-configurable properties in instrumenter for expect.toThrow

This commit is contained in:
Kasper Peulen 2025-03-20 23:51:24 +01:00
parent 54c6d58cad
commit 039ca1a733
2 changed files with 34 additions and 8 deletions

View File

@ -101,7 +101,7 @@ export const WithLoaders = {
},
};
const UserEventSetup = {
export const UserEventSetup = {
play: async (context) => {
const { args, canvasElement, step, userEvent } = context;
const canvas = within(canvasElement);
@ -132,4 +132,28 @@ const UserEventSetup = {
},
};
export { UserEventSetup };
/**
* Demonstrates the expect.toThrow functionality from issue #28406
* https://github.com/storybookjs/storybook/issues/28406
*
* This tests various forms of throw assertions to ensure they all work correctly.
*/
export const ToThrow = {
play: async () => {
await expect(() => {
throw new Error('test error');
}).toThrow();
await expect(() => {
throw new Error('specific error');
}).toThrowError();
await expect(() => {
throw new Error('specific message');
}).toThrow('specific message');
await expect(() => {
// This doesn't throw
}).not.toThrow();
},
};

View File

@ -355,12 +355,14 @@ export class Instrumenter {
(acc, key) => {
const descriptor = getPropertyDescriptor(obj, key);
if (typeof descriptor?.get === 'function') {
const getter = () => descriptor?.get?.bind(obj)?.();
Object.defineProperty(acc, key, {
get: () => {
return this.instrument(getter(), { ...options, path: path.concat(key) }, depth);
},
});
if (descriptor.configurable) {
const getter = () => descriptor?.get?.bind(obj)?.();
Object.defineProperty(acc, key, {
get: () => {
return this.instrument(getter(), { ...options, path: path.concat(key) }, depth);
},
});
}
return acc;
}