Реагируйте с помощью Jest - PullRequest
1 голос
/ 07 февраля 2020

Я новичок в React native и изучал, как написать тест для экрана приветствия. На экране есть только фоновое изображение, текст и кнопка.

Вот ошибка, которую я получаю

onsole.error node_modules/prop-types/checkPropTypes.js:20

  Warning: Failed prop type: Invalid prop `source` supplied to `Image`.

      in Image (at WelcomeScreen.js:91)

      in WelcomeScreen (at WelcomeTest.js:7)

console.error node_modules/react/cjs/react.development.js:188

  Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.



  Check your code at WelcomeScreen.js:104.

      in WelcomeScreen (at WelcomeTest.js:7)

console.error node_modules/react-test-renderer/cjs/react-test-renderer.development.js:9036

  The above error occurred in one of your React components:

      in View (created by View)

      in View (at ImageBackground.js:63)

      in ImageBackground (at WelcomeScreen.js:74)

      in WelcomeScreen (at WelcomeTest.js:7)



  Consider adding an error boundary to your tree to customize error handling behavior.

Вот что я написал для WelcomeTest. js

    import 'react-native'
import React from 'react'
import WelcomeScreen from '../App/Containers/WelcomeScreen'
import renderer from 'react-test-renderer'

test('WelcomeScreen component', () => {
  const tree = renderer.create(<WelcomeScreen />).toJSON()
  expect(tree).toMatchSnapshot()
})

Не уверен, что я делаю не так. Я проверил свой импорт и убедился, что страница отображается правильно. Может кто-нибудь, пожалуйста, укажите мне в правильном направлении. Заранее спасибо

Ответы [ 2 ]

1 голос
/ 07 февраля 2020

В вашем jest.config. js добавьте:

moduleNameMapper: {
    '^image![a-zA-Z0-9$_-]+$': 'GlobalImageStub',
}

Это должно привести к корректному разрешению пути к изображениям для целей тестирования.

0 голосов
/ 07 февраля 2020

Я думаю, что Jest не может использовать изображение при создании снимка для сравнения со старым снимком.

...