将变量作为参数从反应测试库传递给 getByText 不起作用

Passing variable as argument to getByText from react testing library does not work

如果您将字符串文字从 React 测试库传递给 getByText,它工作正常

getByText('my text')

但是如果您将相同的字符串作为变量传递,它什么也找不到

const text: string = 'my text'
getByText(text) // this returns no match even though with literal it worked fine

可能是什么问题?

test('page displays users activities on load', async () => {
    const history = createMemoryHistory();
    const {getByText} = render(
        <Router history={history}>
            <Dashboard />
        </Router>
    )

    await waitFor(() => getByText("Programovanie v jazyku C", {exact: false}))

    activityData.forEach((activity) => {
        expect(getByText(activity.title, {exact: false})).toBeTruthy()
    })
})

编辑:添加了我的代码

组件仪表板包含表格,这些表格是从模拟 api 中填充的。模拟 api 数据与 activityData 的数据相同。等待使用字符串文字、activityData 第一项的标题调用。那很好用。但是它不起作用,当用变量调用时。

问题出在我通过模拟服务传递的数据中。它是从 chrome 控制台复制的,复制后它包含格式为“\8ief2”的字符,在控制台打印上看起来像普通字符,可能在 html 中也是如此,但实际上不是。经常检查你的数据。