【问题标题】:Jest is green even if Expected is not equal ReceivedJest 是绿色的,即使 Expected 不等于 Received
【发布时间】:2018-10-25 02:36:20
【问题描述】:
it('User is already present as a supplier', (done) => {
    const store = mockStore({}, [{ type: 'get_user', data: { } }]);
    return store.dispatch(userGetAction({ role: 'supplier' }, () => {})).then(() => {
      try {
        expect(store.getActions()[0].data.disabled).toEqual(true);
        expect(store.getActions()[0].data.errormessage).toEqual('User is already present as a assitantbuyer');
      } catch (err) {
        console.log(err);
      }
      done();
    }).catch(() => {
      done();
    });
  });

为什么它通过并显示绿色状态,即使它是预期不等于实际。

PASS  src/actions/user-get-action-assistant-buyer.test.jsx
  ● Console

    console.error node_modules/fbjs/lib/warning.js:33
    console.log src/actions/user-get-action-assistant-buyer.test.jsx:25
      { Error: expect(received).toEqual(expected)

      Expected value to equal:
        "User is already present"
      Received:
        "User is already present"
          at store.dispatch.then (/Users/prakashchandrabarnwal/Desktop/myProductInduct_CE_Admin/src/actions/user-get-action-assistant-buyer.test.jsx:23:57)
        matcherResult: 
         { actual: 'User is already present as a assitant buyer',
           expected: 'User is already present as a assitantbuyer',
           message: [Function],
           name: 'toEqual',
           pass: false } }

如果我不将期望包装在里面,请尝试将其悄悄地进入 .catch()

thunk 代码返回 UnhandledPromiseRejectionWarning:

const buyerAction = (data = {}, cb) => dispatch => axios({
  method: 'POST',
  url: `http://localhost:3001/api/manageUsers`,
  headers: {
    'x-access-token': authService.getAccessToken()
  },
  data
}).then(res => new Promise((resolve, reject) => {
  if (res.status === 200 && res.data) {
    dispatch({ type: 'buyer_created', data: res.data.message });
    if (data.role === 'buyer') {
      axios({
        method: 'POST',
        url: `http://localhost:3001/api/populateBuyerLookUp`,
        headers: {
          'x-access-token': authService.getAccessToken()
        },
        data
      })

.then((response) => {
            resolve(response);
          }).catch((err) => {
            reject(err);
          });
        }
        cb(res.data.message);
      } else {
        reject(res);
      }
    }))
      .catch(() => {
        console.log('error');
      });

(node:44182) UnhandledPromiseRejectionWarning:未处理的承诺拒绝。此错误源于在没有 catch 块的情况下抛出异步函数内部,或拒绝未使用 .catch() 处理的承诺。 (拒绝编号:1)

【问题讨论】:

    标签: javascript reactjs jestjs redux-thunk


    【解决方案1】:

    因此,您的 expect 因错误而失败,您捕获此错误并记录它,换句话说,您将错误静音。毕竟,您调用“完成”就像没有错误一样。 代码就像你写的那样做:忽略并静音任何错误。您必须从测试中删除所有 catch

    it('User is already present as a supplier', () => {
        const store = mockStore({}, [{ type: 'get_user', data: { } }]);
        return store
           .dispatch(userGetAction({ role: 'supplier' }, () => {}))
           .then(() => {
                expect(store.getActions()[0].data.disabled).toEqual(true);
                expect(store.getActions()[0].data.errormessage).toEqual('User is already present as a assitantbuyer');
            });
      });
    

    您可以从您的测试中返回 Promise(无需使用done),如果 Promise 失败,则整个测试失败。就是这样

    UPD:关于UnhandledPromiseRejectionWarning,我认为它可能与您对“populateBuyerLookUp”的请求相关联,这个请求完全脱离了流程。我试图修复它,但很难理解你到底想要做什么

        const buyerAction = (data = {}, cb) => dispatch => axios({
      method: 'POST',
      url: `http://localhost:3001/api/manageUsers`,
      headers: {
        'x-access-token': authService.getAccessToken()
      },
      data
    })
      .then((res) => {
        dispatch({type: 'buyer_created', data: res.data.message});
        let promise;
        if (data.role === 'buyer') {
          promise = axios({
            method: 'POST',
            url: `http://localhost:3001/api/populateBuyerLookUp`,
            headers: {
              'x-access-token': authService.getAccessToken()
            },
            data
          });
        }
    
        return Promise.resolve(promise).then(() => res.data.message);
      })
      .then((message) => {
        cb(message)
      }).catch(()=>{console.log("error")});
    

    【讨论】:

    • 你能给我一个例子,在这种情况下如何使用done?
    • 能否请您查看 thunk 代码,每当我测试负面场景时,它都会抛出 UnhandledPromiseRejectionWarning。
    • @bikkybarnwal,我已经更新了答案,请看一下
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-02
    • 1970-01-01
    • 2023-02-08
    • 1970-01-01
    相关资源
    最近更新 更多