Skip to content

Commit 33dd46c

Browse files
committed
fix: optimize userEvent.type performance by filtering eventWrapper from instrumentation
Add getKeys filter to exclude 'eventWrapper' property from instrumenter calls to prevent performance regression in userEvent.type operations. This addresses the slowdown reported when upgrading from Storybook 8 to 9 with addon-vitest. The instrumenter was spending significant time sorting large arrays of calls that included the eventWrapper property, which is not needed for testing interactions. By filtering it out at the instrumentation level, we avoid unnecessary processing overhead.
1 parent 1abc1a5 commit 33dd46c

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

code/core/src/test/preview.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ const enhanceContext: LoaderFunction = async (context) => {
9696
if (clipboard) {
9797
context.userEvent = instrument(
9898
{ userEvent: uninstrumentedUserEvent.setup() },
99-
{ intercept: true }
99+
{
100+
intercept: true,
101+
getKeys: (obj) => Object.keys(obj).filter((key) => key !== 'eventWrapper'),
102+
}
100103
).userEvent;
101104

102105
// Restore original clipboard, which was replaced with a stub by userEvent.setup()

code/core/src/test/testing-library.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type TestingLibraryDom = typeof domTestingLibrary;
1515
const testingLibrary = instrument(
1616
{ ...domTestingLibrary },
1717
{
18+
getKeys: (obj) => Object.keys(obj).filter((key) => key !== 'eventWrapper'),
1819
intercept: (method, path) =>
1920
path[0] === 'fireEvent' || method.startsWith('find') || method.startsWith('waitFor'),
2021
}
@@ -118,5 +119,5 @@ export const uninstrumentedUserEvent = _userEvent.userEvent;
118119

119120
export const { userEvent }: { userEvent: UserEvent['userEvent'] } = instrument(
120121
{ userEvent: _userEvent.userEvent },
121-
{ intercept: true }
122+
{ intercept: true, getKeys: (obj) => Object.keys(obj).filter((key) => key !== 'eventWrapper') }
122123
);

0 commit comments

Comments
 (0)