Nativebase: Testing With Jest

Created on 20 Nov 2019  ·  5Comments  ·  Source: GeekyAnts/NativeBase

I'm having an issue when trying to test with jest

import { Text } from "native-base"

it('should render the component correctly', () => {
    const component = shallow(<Text>Hello World</Text>)
    expect(component.find(Text).text()).toEqual('Hello World')
})

but getting error

Method “text” is meant to be run on 1 node. 0 found instead.

      4 |     const component = shallow(<Text>Hello World</Text>)
      5 |     console.log(component.debug())
    > 6 |     expect(component.find(Text).text()).toEqual('Hello World')
        |                                 ^
      7 | })
      8 | 

      at ShallowWrapper.single (node_modules/enzyme/src/ShallowWrapper.js:1636:13)
      at ShallowWrapper.text (node_modules/enzyme/src/ShallowWrapper.js:1079:12)
      at Object.text (src/__tests__/App.test.js:6:33)

But if i log the component using

console.log(component.debug())

I can see

 <Text virtual={[undefined]} style={{...}} uppercase={false}>
      Hello World
    </Text>

Except i'm not doing it properly

My jest config in package.json

"jest": {
    "preset": "react-native",
    "setupFilesAfterEnv": [
      "./setupTests.js"
    ]
  }

Most helpful comment

Have you solved the issue? For me, I am having a different issue:

Details:

    /Users/xxx/node_modules/@codler/react-native-keyboard-aware-scroll-view/index.js:3
    import listenToKeyboardEvents from './lib/KeyboardAwareHOC'
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module
        at compileFunction (<anonymous>)

      at Runtime._execModule (node_modules/jest-runtime/build/index.js:1179:56)
      at Object.<anonymous> (node_modules/native-base/src/basic/Content.js:5:1)

I am on native-base 2.13.13

All 5 comments

Have you solved the issue? For me, I am having a different issue:

Details:

    /Users/xxx/node_modules/@codler/react-native-keyboard-aware-scroll-view/index.js:3
    import listenToKeyboardEvents from './lib/KeyboardAwareHOC'
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module
        at compileFunction (<anonymous>)

      at Runtime._execModule (node_modules/jest-runtime/build/index.js:1179:56)
      at Object.<anonymous> (node_modules/native-base/src/basic/Content.js:5:1)

I am on native-base 2.13.13

Hello @Aung-Myint-Thein , i don't know about your issue, but yeah i solved mine
expect(component.find(Title).children().text()).toEqual("Contact Us") expect(component.find(H2).children().text()).toEqual("Let's Get In Touch !")
using the component children instead

Have you solved the issue? For me, I am having a different issue:

Details:

    /Users/xxx/node_modules/@codler/react-native-keyboard-aware-scroll-view/index.js:3
    import listenToKeyboardEvents from './lib/KeyboardAwareHOC'
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module
        at compileFunction (<anonymous>)

      at Runtime._execModule (node_modules/jest-runtime/build/index.js:1179:56)
      at Object.<anonymous> (node_modules/native-base/src/basic/Content.js:5:1)

I am on native-base 2.13.13

@Aung-Myint-Thein did u find the solution

Anyone found a solution?

Have you solved the issue? For me, I am having a different issue:

Details:

    /Users/xxx/node_modules/@codler/react-native-keyboard-aware-scroll-view/index.js:3
    import listenToKeyboardEvents from './lib/KeyboardAwareHOC'
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module
        at compileFunction (<anonymous>)

      at Runtime._execModule (node_modules/jest-runtime/build/index.js:1179:56)
      at Object.<anonymous> (node_modules/native-base/src/basic/Content.js:5:1)

I am on native-base 2.13.13

I'm not quite sure if it's the correct solution to this, but adding @codler to transformIgnorePatterns removes the error for me.

Example:

"jest" : { "transformIgnorePatterns": [ "/node_modules/(?!(jest-)?@codler)" ] }

Was this page helpful?
0 / 5 - 0 ratings