为什么 @testing-library/user-event 上不存在清除方法

coo*_*kun 6 javascript reactjs react-testing-library user-event

我正在开发一个 CMS 项目,刚刚遇到一个问题。_userEvent.default.clear is not a function

\n\n
import user from \'@testing-library/user-event\'\n\ntest(\'can edit label\', async () => {\n    createArticleMock()\n    await renderRootAndInitStore(rightNowTeasers, globalInitialState)\n\n    const index = 1\n    const input = screen.getByDisplayValue(labels[index])\n    const newLabel = \'V\xc3\x84RLDEN\'\n    user.clear(input)\n    user.type(input, newLabel)\n\n    expect(await screen.findByDisplayValue(newLabel))\n    user.click(screen.getByTestId(\'settings-form-save\'))\n    await screen.findByText(newLabel)\n})\n
Run Code Online (Sandbox Code Playgroud)\n\n

当我访问时@testing-library/user-event,我看到那些线条

\n\n
// Definitions by: Wu Haotian <https://github.com/whtsky>\nexport interface IUserOptions {\n    allAtOnce?: boolean;\n    delay?: number;\n}\n\nexport interface ITabUserOptions {\n    shift?: boolean;\n    focusTrap?: Document | Element;\n}\n\nexport type TargetElement = Element | Window;\n\ndeclare const userEvent: {\n    click: (element: TargetElement) => void;\n    dblClick: (element: TargetElement) => void;\n    selectOptions: (element: TargetElement, values: string | string[]) => void;\n    type: (\n        element: TargetElement,\n        text: string,\n        userOpts?: IUserOptions\n    ) => Promise<void>;\n    tab: (userOpts?: ITabUserOptions) => void;\n};\n\nexport default userEvent;\n
Run Code Online (Sandbox Code Playgroud)\n\n

正如文件所示clear()userEvent. 但文档指出了clear()方法。

\n\n

文档 =>文档

\n\n

这是我第一次使用这个库。有人知道问题是什么吗?

\n

小智 4

您必须将@testing-library/user-event更新到最新版本。然后该错误将得到解决。您可以在这里找到最新版本: https: //www.npmjs.com/package/@testing-library/user-event