[英]Jest: How to unmock components listed in an index.js file?
使用 Jest 對我的 React 組件進行單元測試。
我在src/components/index.js
下列出了我的組件,以便我可以執行以下操作:
import { MyComponent } from 'components';
使用 jest 時,如何在這樣的 require 路徑上調用unmock
?
jest.unmock('components/MyComponent'); // ???
import { MyComponent } from 'components';
// ... tests below ...
以上不起作用,盡管這基本上是預期的結果。
我以前也遇到過類似的問題,並且在 jest 12.0.1 中刪除了導入解構工作。
所以而不是;
import { MyComponent } from 'components';
嘗試;
import index from 'components'
// test code
index.MyComponent
就我個人而言,我通常只是直接包含我想要測試的組件,而不是從索引中引用它。 僅當使用未定義將測試放在代碼旁邊的標准的測試庫時,才使用索引似乎是值得的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.