如果只有一个次要版本或补丁版本关闭,则会发生灾难性故障,因为我已经阅读过软件包版本:
"react": "17.0.2",
"react-native": "0.66.0",
"react-test-renderer": "^17.0.2",
"@testing-library/jest-native": "^4.0.4",
"@testing-library/react-native": "^7.2.0",
我的测试如下所示:
import React from 'react';
import {loadFeature, defineFeature} from 'jest-cucumber';
import {fireEvent, render} from '@testing-library/react-native';
import TestIDConstants from '../../../src/constants/TestIDConstants';
import App from '../../../src/App';
const feature = loadFeature(
'features/integration/Onboarding/onboarding-screen.feature',
);
defineFeature(feature, (test) => {
test('App Launches', ({given}) => {
const {getByTestId, rerender, unmount} = render(<App />);
given('I should see the welcome screen as the first screen', () => {
expect(() => getByTestId(TestIDConstants.WELCOME_TITLE_TEXT)).toThrow();
rerender(<App />);
expect(() =>
getByTestId(TestIDConstants.WELCOME_TITLE_TEXT),
).toBeTruthy();
unmount();
});
});
test('Navigation to Privacy Policy', ({given, then}) => {
const {getByTestId, rerender} = render(<App />);
given('I tap the privacy policy button on the welcome screen', () => {
expect(() => getByTestId(TestIDConstants.WELCOME_TITLE_TEXT)).toThrow();
rerender(<App />);
fireEvent.press(getByTestId(TestIDConstants.PRIVACY_POLICY_TOUCHABLE));
rerender(<App />);
});
then('I should be navigated to the privacy policy screen', () => {
expect(getByTestId(TestIDConstants.PRIVACY_POLICY_VIEW)).toBeTruthy();
});
});
});
第一个测试通过,第二个从来没有通过,我总是得到:
● Onboarding › Navigation to Privacy Policy
Unable to find node on an unmounted component.
34 | expect(() => getByTestId(TestIDConstants.WELCOME_TITLE_TEXT)).toThrow();
35 | rerender(<App />);
> 36 | fireEvent.press(getByTestId(TestIDConstants.PRIVACY_POLICY_TOUCHABLE));
| ^
37 | rerender(<App />);
38 | });
我真的不明白这个错误。我实际上是rerender
在上面抛出错误的那一行调用 - 开玩笑(或者可能是反应)如何说组件已卸载?
我的意思是……这些东西是基本的。我想点击一个按钮并希望被导航到一个新屏幕。为什么这么难?
叹息......只是在 React 玩笑测试天堂的另一天。
另一个注意事项:当我们连接到 Gherkin 功能规范时,given
when
then
语法来自jest-cucumber
库 - 但我几乎不认为这是导致问题的原因。我 99% 确定这个错误存在react-testing-library
于react-native
.