为什么需要调用`mount`才能使测试正常工作?

时间:2019-07-16 11:47:12

标签: jestjs enzyme

我在使测试在我的React应用程序中正常工作时遇到问题。这是我的设置:

TestComponents.js

const TestComponent = () => {
  const example = useExample()

  if (example.getValue()) {
    return <div>VALUE IS TRUE</div>
  }

  return <div>VALUE IS FALSE</div>
}

export default TestComponent

TestComponent.test.js

describe("TestComponent", () => {

  beforeEach(() => {
    wrapper = mount(<TestComponent />)
  })

  it("cant handle named export without any updates", () => {
    exampleHook.useExample = jest.fn(() => ({
      getValue: () => true
    }))

    expect(wrapper.text()).toEqual("VALUE IS TRUE")

    exampleHook.useExample = jest.fn(() => ({
      getValue: () => false
    }))

    expect(wrapper.text()).toEqual("VALUE IS FALSE")
  })

  it("cant handle named export with wrapper update", () => {
    ...

    wrapper.update()

    ...
  })

  it("can handle named export with wrapper mount", () => {
    ...

    wrapper.mount()

    ...
  })
})

我得到的结果如下:

✕ cant handle named export without any updates (13ms)
✕ cant handle named export with wrapper update (2ms)
✓ can handle named export with wrapper mount (2ms)

我还没有看到任何示例表明您在模拟之间运行了mount。你有什么建议吗?

0 个答案:

没有答案