中继突变未在测试中同步解决

问题描述

我基本上有以下代码。

// MyComponent.js
const myMutation = graphql`
  mutation myMutation($input: MyInput!) {
    doMyMutation(input: $input) {
      ok
    }
  }
`

const MyComponent = () => {
  const [mutate] = useMutation(myMutation)
  const handleClick = async () => {
    const variables = {
      input: {
        stuff: true
      }
    }
    await mutate({ variables })
    myfunction('Hello World')
  }

  return <button onClick={handleClick}>Click Me</button>
}

export default MyComponent

// test.js
import myfunction from './myfunction'

it('should finish mutation',async () => {
  environment.mock.queueOperationResolver(operation =>
    MockPayloadGenerator.generate(operation)
  )

  const { getByText } = render(<MyComponent />)

  fireEvent.click(getByText('Click Me'))

  expect(myfunction).toHaveBeenCalledWith('Hello World')
  // expect(AsyncRequestStore.clickToCall).toHaveBeenCalledTimes(2)
})

// myfunction.js
const myfunction = (message) => {
  console.log(message)
}

export default myfunction

但是,当我运行测试时,突变在测试运行后完成,说有一些异步正在进行。我可以直接在我的代码中看到非计时器。是什么导致这直到几帧/几毫秒后才解决突变?

我希望 myfunction(用 jest.mock('./myfunction') 模拟,用 Hello World 调用,但在测试中此时尚未调用它,它只会得到测试完成后调用。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...