在jest中获取条件内文本的错误

eblbsuwk  于 12个月前  发布在  Jest
关注(0)|答案(1)|浏览(133)

我正在学习如何使用jest编写测试用例。我有一个React文件,我有一些文本或按钮内的条件。现在当我得到的文本错误.但是,没有条件,它的工作。下面是ts文件:

{fileErr === '1' && <Text mt={2}>Please fix the issue before continuing.</Text>}
          {fileErr === '2' && <Text mt={2}>For more information see&nbsp;<Link href={faqUrl} target="_blank">FAQ.</Link><br/>Do you want to continue?</Text>}
{fileErr === '2' &&
             (<Button rightIcon={<MdArrowForward />} onClick={onContinue}>
              Continue
            </Button>)}

下面是测试用例:

expect(getByText(/Selected define version 2.0 does not match the version in: ./i)).toBeInTheDocument()
expect(getByText(/The following files are missing - make sure they are within the required folder structure as mentioned below./i)).toBeInTheDocument()
const continueButton = getByText(/^Continue$/i)
userEvent.click(continueButton)

现在,当我运行测试用例时,我得到以下错误:

  • TestingLibraryElementError:找不到具有以下文本的元素:/继续/i。这可能是因为文本被多个元素分解。在这种情况下,您可以为文本匹配器提供一个函数,使匹配器更加灵活。

请给我提供更好的解决方案

oewdyzsn

oewdyzsn1#

两个都试试

const continueButton = getByText(/Continue/i)

const continueButton = getByText((content, element) => {
  // Perform custom logic to match the text
  return content.startsWith('Continue')
})

相关问题