2

I want to test my React Native code, but I am struggling about which testing library I should use.

According to what I've read on the internet, it is a better practice to focus on testing the user behavior than the internal logic of a component. That's why I use jest library with test-renderer.

There are two recommended libraries on the ReactNative official documentation that are equivalent to me: react-native-testing-library and @testing-library/react-native (also known as native-testing-library).

What's the difference between them ?

skyboyer
  • 22,209
  • 7
  • 57
  • 64
dng
  • 411
  • 1
  • 5
  • 18
  • *better practice to focus on testing the user behavior than the internal logic of a component*- fwiw, it's not a better practice, otherwise it would annihilate isolated unit testing long time ago. Both approaches have their pros and cons. Blackbox testing is a gray zone because it doesn't allow to efficiently isolate problems for failed tests on one side and still doesn't behave like real thing like it would in e2e. Doesn't answer your question, just my 2 cents why Enzyme shouldn't be totally ignored for RN. – Estus Flask May 24 '20 at 10:47

2 Answers2

1

testing-library/native-testing-library is suggesting to use callstack/testing-library on github, however the npm package for @testing-library/react-native@7.x.x points to the repository https://github.com/callstack/react-native-testing-library.

In the docs:

You may have noticed a strange v2 to v7 upgrade, skipping versions 3, 4, 5 and 6. This is because we renamed the react-native-testing-library npm package to @testing-library/react-native, officially joining the "Testing Library" family . We're merging existing two libraries into a single one. The native-testing-library repository, which had v6, will soon be archived and using @testing-library/react-native below v7, sourced from mentioned repository, is deprecated.

For branding purposes we keep the "React Native Testing Library" name, similar to "React Testing Library". Only the npm published package is changing. The code repository also stays the same under Callstack governance.

In summary: The codebase is essentially the same, (as it's from the same developers), however, since joining @testing-library, there was some administrative work to be done on the npm registry by the developers to consolidate the packages.

Denis Tsoi
  • 9,428
  • 8
  • 37
  • 56
0

In the past there were to React Native Testing Libraries: @testing-library/react-native built by Brandon Carroll and react-native-testing-library by Callstack. Both of these libraries where modelled after React Testing Library by Kent C. Dodds, and relied on React Test Renderer for rendering components.

After some time, the later one has been more actively maintained and saw bigger adoption in the community. In Jun 2020 the former one (by Brandon Carroll) has been deprecated and the later one (by Callstack) has become the official React Native library in the Testing Library org, taking over the @testing-library/react-native NPM package name. During merge process the API of Callstack's library has been modified to allow for easier transition of users from the deprecated library.

Maciej Jastrzebski
  • 3,674
  • 3
  • 22
  • 28