使用笑话spyOn无法检测到在try-catch块内调用的方法

问题描述 投票:0回答:1

我遇到的问题是Jest报告setResultsSpy被调用0次,实际上我知道它被调用了。我通过在代码中的console.log(results)下放置const results = await getFileList(data.path);知道了这一点,并且能够看到返回的结果。

我现在的猜测是try-catch块创建了一个本地作用域,这导致这些调用未注册。如果是这样,我的问题是“如何测试这些方法是否已被调用”?

// test_myFunction.js

test((`myFunction with valid path should return list of files`), () => {
  const actions = {
    setMsg: () => { },
    setButton: () => {},
    setResults: () => {},
    setAppState: () => {}
  };
  const setMsgSpy = jest.spyOn(actions, 'setMsg');
  const setSubmitButtonStateSpy = jest.spyOn(actions, 'setButton');
  const setResultsSpy = jest.spyOn(actions, 'setResults');
  const setAppStateSpy = jest.spyOn(actions, 'setAppState');
  const returnedFileList = [
    'file1.pdf',
    'file2.pdf',
    'file3.pdf',
  ];
  const requestConfig = {
    component: COMPONENTS.myComponent,
    request: RequestTypes.REQUEST,
    data: {path: 'folder1'},
    actions
  };

  processRequest(requestConfig)

  expect(setMsgSpy).toHaveBeenCalledTimes(1);
  expect(setMsgSpy)
  .toHaveBeenCalledWith('loading');

  expect(setButtonSpy).toHaveBeenCalledTimes(1);

  expect(setResultsSpy).toHaveBeenCalledTimes(1);
  expect(setResultsSpy).toHaveBeenCalledWith(returnedFileList);

  expect(setAppStateSpy).toHaveBeenCalledTimes(1);
  expect(setAppStateSpy).toHaveBeenCalledWith('confirm');
});

_

// myFunction.js
async function processRequest({
  component,
  request,
  data,
  actions,
}){
  if (component === COMPONENTS.myComponent) {
    const path = data.path.trim();
    switch (request) {
      case RequestTypes.REQUEST:
        actions.setMsg('message');
        actions.setButton('disabled');

          try {
            const results = await getFileList(data.path);
            actions.setResults(results);
            actions.setAppState('confirm');
          } catch (e) {
            actions.setError(e);
            actions.setAppState('error');
          }
        }
        break;
      default:
        break;
    }
  }
jestjs spy
1个回答
0
投票

问题是,由于getFileList()是一个异步函数,在getFileList()执行的结果完成之前,杰斯特未能通过测试。

解决方案是测试异步处理执行as per the documentation。有四种方法可以解决此问题:

  1. 使用回调
  2. 在返回的诺言上使用.then().catch()(请参阅.then() here.catch() here上的文档)
  3. 使用.resolves()上的.rejects()expect()玩笑方法让玩笑解决承诺。
  4. 通过将测试匿名函数声明为async并在await上使用processRequest()来使用Async-Await语法。

我喜欢使用选项4,因为我喜欢使用异步等待语法。解决方法如下:

// test_myFunction.js

test((`myFunction with valid path should return list of files`), async () => {
  //(all of the variables established from above)    

  await processRequest(requestConfig)

  expect(setMsgSpy).toHaveBeenCalledTimes(1);
  expect(setMsgSpy)
  .toHaveBeenCalledWith('loading');

  expect(setButtonSpy).toHaveBeenCalledTimes(1);

  expect(setResultsSpy).toHaveBeenCalledTimes(1);
  expect(setResultsSpy).toHaveBeenCalledWith(returnedFileList);

  expect(setAppStateSpy).toHaveBeenCalledTimes(1);
  expect(setAppStateSpy).toHaveBeenCalledWith('confirm');
});

注意第一行使用async,调用await时使用processRequest(requestConfig)

© www.soinside.com 2019 - 2024. All rights reserved.