亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 Jest 測試快照上返回未定義的字符串連接

在 Jest 測試快照上返回未定義的字符串連接

肥皂起泡泡 2023-05-19 17:18:34
我有以下反應組件import styles from './Alert.module.scss';const Alert = ({  role = 'document',  type = 'info',}) => (<GridItem>  <div className={`${styles.alert} ${styles[`alert-${type}`]}`} role={role}>   {icon && <div className={`${styles['alert-icon']}`} />}   <div className={styles.content}>{children}</div>  </div></GridItem>我正在這樣寫我的測試jest.mock('./Alert.module.scss', () => ({   'alert': 'alert',   'type': 'info',}));jest.mock('./GridItem', () => 'GridItem');describe('Alert', () => {  it('should render correctly', () => {    expect(renderer.create(<Alert>Alert</Alert>)).toMatchSnapshot();  });});問題是在創建快照時,類型變量返回未定義。我假設它與字符串連接有關,因為“角色”變量寫入正確。這是快照。<GridItem>  <div    className="alert undefined"    role="document"  >    <div>      Alert    </div>  </div></GridItem>`;所以,我不確定我在這里遺漏了什么,或者是否對字符串連接有任何限制。我怎樣才能正確得到它?謝謝!
查看完整描述

1 回答

?
慕斯709654

TA貢獻1840條經驗 獲得超5個贊

您在type變量前加上alert-, 它似乎不存在于模擬styles對象上。所以你可以嘗試添加它


jest.mock('./Alert.module.scss', () => ({ 

  'alert': 'alert', 

  'type': 'info',


  // add the following line

  'alert-info': 'info'

}));


查看完整回答
反對 回復 2023-05-19
  • 1 回答
  • 0 關注
  • 126 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號