Testing Jest测试,名为redux组件的导出,使useffect触发2次而不是1次

Testing Jest测试,名为redux组件的导出,使useffect触发2次而不是1次,testing,redux,jestjs,react-testing-library,Testing,Redux,Jestjs,React Testing Library,我正在尝试使用jest和react测试库测试组件 我决定对组件进行命名导出,并将其与Redux分开。 问题是,当我使用命名导出时,对空数组调用两次first-useffect。这仅在jest测试和作为命名导出导入它时发生。将其作为默认导出导入或在localhost上运行应用程序时不会发生这种情况 第一次我得到了通过jest测试传递的所有道具,但第二次它被称为“道具消失”,我得到了传递给子组件的道具。为什么会这样 useEffect(() => { console.log('I

我正在尝试使用jest和react测试库测试组件

我决定对组件进行命名导出,并将其与Redux分开。 问题是,当我使用命名导出时,对空数组调用两次first-useffect。这仅在jest测试和作为命名导出导入它时发生。将其作为默认导出导入或在localhost上运行应用程序时不会发生这种情况

第一次我得到了通过jest测试传递的所有道具,但第二次它被称为“道具消失”,我得到了传递给子组件的道具。为什么会这样

  useEffect(() => {
    console.log('I APEARE ONCE', props)
    setParsedQueryString(queryString.parse(location.search))
  }, [])

  useEffect(() => {
 
    if (parsedQueryString.person_id && parsedQueryString.job_id) {
      console.log('SECOND USE EFFECT', props)
      props
        .getLicenseVerificationDataAction(
          window.activeEnvironment,
          parsedQueryString.person_id,
          parsedQueryString.job_id
        )
        .then((response) => {
            setActiveScreen({
              activeStep: "first",
              verificationStatus: "good",
            })
        })
        .catch((error) => {
           console.log("ERROR")
        })
    }
  }, [parsedQueryString])

...

  return activeScreen.activeStep === "first" ? (
    <div data-testid="verification-flow">
      <VerificationFlow
        verificationType={props.match.params.slug}
        verificationData={parsedQueryString}
        changeActiveStep={handleChangeActiveStep}
        activeStep={activeScreen.activeStep}
        verificationStatus={activeScreen.verificationStatus}
      />
    </div>
  ) : (
    <img
      data-testid="spinner"
      src={spinner}
      width="50"
      style={{ position: 'relative', left: '50%', top: '200px' }}
    />
  )

useffect(()=>{
console.log('I APEARE ONCE',props)
setParsedQueryString(queryString.parse(location.search))
}, [])
useffect(()=>{
if(parsedQueryString.person\u id&&parsedQueryString.job\u id){
console.log('第二次使用效果',道具)
道具
.getLicenseVerificationDataAction(
window.activeEnvironment,
parsedQueryString.person\u id,
parsedQueryString.job\u id
)
。然后((响应)=>{
设置活动屏幕({
活动步骤:“第一步”,
验证状态:“好”,
})
})
.catch((错误)=>{
console.log(“错误”)
})
}
},[parsedQueryString])
...
返回activeScreen.activeStep==“第一个”?(
) : (
)
开玩笑

const props={
地点:{
搜索:
“?人员id=1231232,职务id=444555,州=NY,由招聘人员上传=0,职务名称=护理%20家庭%20设施和公司简介=lindsays Smoothis”,
},
getLicenseVerificationDataAction:jest.fn(()=>
承诺,决心({
证书:{
活动:'否',
出生年份:1990年,
作业类型:“RN”,
最后检查:“2020-10-30T15:25:16.102Z”,
许可证状态:“已过期”,
个人识别号:1231232,
},
developerMessage:null,
})
),
}
测试('呈现验证路由',异步()=>{
const{debug,getByTestId,queryByTestId,findByTestId}=render(
)
expect(queryByTestId('spinner')).toBeTruthy()
等待waitForElementToBeRemoved(()=>queryByTestId('spinner'))
expect(queryByTestId('spinner')).toBeFalsy()
wait waitFor(()=>expect(queryByTestId('verification-flow'))。toBeTruthy())
})
我想测试VerificationFlow组件是否已渲染

因此,第一次console.log触发器(“I APEARE ONCE”)时,我看到了我用玩笑传递的道具 然后,console.log(“第二次使用效果”)触发 然后在第一个console.log(“I APEARE ONCE”)触发之后,我看到的道具是传递给VerificationFlow组件的道具,而不是我用Jest传递的道具

这会触发错误“无法读取未定义的属性搜索”,因为我从jest传递的道具不再存在

为什么会触发两次?道具去了哪里? 这是一个顶级组件,没有重新招标。当我在本地主机上运行应用程序时,我看不到console.log再次触发。 只有durin jest测试和命名导出

const props = {
  location: {
    search:
      '?person_id=1231232&job_id=444555&state=NY&uploaded_by_recruiter=0&job_name=Nursing%20home%20Facility&company_profile=lindsays-smoothies',
  },
  getLicenseVerificationDataAction: jest.fn(() =>
    Promise.resolve({
      credentials: {
        active: 'NO',
        birth_year: 1990,
        job_type: 'RN',
        last_checked: '2020-10-30T15:25:16.102Z',
        license_status: 'EXPIRED',
        person_id: 1231232,
      },
      developerMessage: null,
    })
  ),
}

test('verification route is rendered', async () => {
  const { debug, getByTestId, queryByTestId, findByTestId } = render(
    <Provider store={store}>
      <Verification {...props} />
    </Provider>
  )
  expect(queryByTestId('spinner')).toBeTruthy()

  await waitForElementToBeRemoved(() => queryByTestId('spinner'))
  expect(queryByTestId('spinner')).toBeFalsy()
  await waitFor(() => expect(queryByTestId('verification-flow')).toBeTruthy())
})