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

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

如何在使用 Jest 進行單元測試時對函數內的 promise 進行代碼覆蓋

如何在使用 Jest 進行單元測試時對函數內的 promise 進行代碼覆蓋

慕田峪7331174 2021-10-29 16:13:01
在使用 jest 進行單元測試時,如何編寫代碼覆蓋然后捕獲函數內的 Promise 函數?請看下面的代碼。服務.jsexport const userLogin = data => {  return AjaxService.post(    "http://localhost/3000/signin", data  ).then(    res => {      return res.data;    },    error => {      return error.response.data;    }  );};AjaxService.jsexport const AjaxService = {  post: (url, data, headers) => {    return axios({      method: "POST",      url: url,      headers: headers || { "content-type": "application/json" },      data: data    });  }}示例.jsclass Login extends Component {  handleSubmit = (event) => {    if (this.props.handleSubmit) this.props.handleSubmit();     this.setState({isLoggedIn: true})    userLogin().then((res) => {     // when promise resolve     var response = res;    }, (err) => {      // when promise reject        var error = err;    })  }  render() {   return (    <form id="login-form" onSubmit={(e) => this.handleSubmit(e)} >     <input type="username" />     <input type="password" />     <button type="submit">Login</button>    </form>   )  }}示例.test.jsit("test login form submit ", () => {    wrapper = shallow(<Login />);    let instance = wrapper.instance(); // get class instance    instance.handleSubmit(); // will trigger component method    let actualVal = wrapper.state().isLoggedIn; // get state key value    expect(true).to.eql(actualVal);  });--coverage在 Jest 中使用生成覆蓋率報告后我們可以看到 promise 成功和錯誤函數中的代碼沒有作為單元測試的一部分被覆蓋。所以請幫忙介紹一下。謝謝。
查看完整描述

2 回答

  • 2 回答
  • 0 關注
  • 261 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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