React Native – Testing (1)

How to run a test and update snapshot?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
jest path_to_a_test_file -u
jest path_to_a_test_file -u
jest path_to_a_test_file -u

How to setup React Native testing?

reactnativetesting.io

  • Install jest globally
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
npm install -g jest-cli
npm install -g jest-cli
npm install -g jest-cli
  • Install @testing-library/react-native
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$ yarn add --dev @testing-library/react-native
$ yarn add --dev @testing-library/react-native
$ yarn add --dev @testing-library/react-native
  • Change .babelrc to babel.config.js
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
module.exports = {
presets: ['module:metro-react-native-babel-preset'],
plugins: [
[
'transform-inline-environment-variables',
{
include: ['SHOW_STORYBOOK'],
},
],
],
}
module.exports = { presets: ['module:metro-react-native-babel-preset'], plugins: [ [ 'transform-inline-environment-variables', { include: ['SHOW_STORYBOOK'], }, ], ], }
module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
  plugins: [
    [
      'transform-inline-environment-variables',
      {
        include: ['SHOW_STORYBOOK'],
      },
    ],
  ],
}
  • Add Jest plugin in VSCode

How to configure jest?

Cannot use import statement outside a module

  • Add the error module into jest.config.js
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
transformIgnorePatterns: [
'node_modules/(?!(@react-native|react-native|common|@me/library)/)'
]
transformIgnorePatterns: [ 'node_modules/(?!(@react-native|react-native|common|@me/library)/)' ]
transformIgnorePatterns: [
  'node_modules/(?!(@react-native|react-native|common|@me/library)/)'
]

Test file template

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import React from 'react';
import { render } from '@testing-library/react-native';
import ErrorBoundary from '..';
import { Text } from 'react-native';
describe('ErrorBoundary', () => {
describe('Rendering', () => {
it('renders the children when there is no error', () => {
const { getByText } = render(
<ErrorBoundary>
<Text>Hello World</Text>
</ErrorBoundary>
);
expect(getByText('Hello World')).toBeDefined();
});
it('renders ErrorView when an error occurs', () => {
const ThrowError = () => {
throw new Error('Test error');
}
const { getByTestId } = render(
<ErrorBoundary>
<ThrowError />
</ErrorBoundary>
);
expect(getByTestId('id-error-view')).toBeCalled;
});
})
describe('Interaction', () => { })
});
import React from 'react'; import { render } from '@testing-library/react-native'; import ErrorBoundary from '..'; import { Text } from 'react-native'; describe('ErrorBoundary', () => { describe('Rendering', () => { it('renders the children when there is no error', () => { const { getByText } = render( <ErrorBoundary> <Text>Hello World</Text> </ErrorBoundary> ); expect(getByText('Hello World')).toBeDefined(); }); it('renders ErrorView when an error occurs', () => { const ThrowError = () => { throw new Error('Test error'); } const { getByTestId } = render( <ErrorBoundary> <ThrowError /> </ErrorBoundary> ); expect(getByTestId('id-error-view')).toBeCalled; }); }) describe('Interaction', () => { }) });
import React from 'react';
import { render } from '@testing-library/react-native';
import ErrorBoundary from '..';
import { Text } from 'react-native';

describe('ErrorBoundary', () => {
  describe('Rendering', () => {
    it('renders the children when there is no error', () => {
      const { getByText } = render(
        <ErrorBoundary>
          <Text>Hello World</Text>
        </ErrorBoundary>
      );

      expect(getByText('Hello World')).toBeDefined();
    });

    it('renders ErrorView when an error occurs', () => {
      const ThrowError = () => {
        throw new Error('Test error');
      }

      const { getByTestId } = render(
        <ErrorBoundary>
          <ThrowError />
        </ErrorBoundary>
      );

      expect(getByTestId('id-error-view')).toBeCalled;
    });
  })

  describe('Interaction', () => { })
});

Be the first to comment

Leave a Reply

Your email address will not be published.


*