跳至主要內容

jasmine-dom

jasmine-dom 是 Testing Library 的配套函式庫,為 Jasmine 提供自訂的 DOM 元素匹配器

npm install --save-dev @testing-library/jasmine-dom

然後依照 jasmine-dom 文件中的使用方法章節,將匹配器加入 Jasmine。

<span data-testid="not-empty"><span data-testid="empty"></span></span>
<div data-testid="visible">Visible Example</div>

expect(screen.queryByTestId('not-empty')).not.toBeEmptyDOMElement()
expect(screen.getByText('Visible Example')).toBeVisible()

注意:當使用其中一些匹配器時,您可能需要確保使用查詢函式(例如 queryByTestId),而不是 get 函式(例如 getByTestId)。否則,get* 函式可能會在您的斷言之前拋出錯誤。

請查看 jasmine-dom 的文件,以取得可用的完整匹配器清單。