react-native-testing-library icon indicating copy to clipboard operation
react-native-testing-library copied to clipboard

Bring toJSON from native-testing-library

Open acatalina opened this issue 5 years ago • 7 comments

Describe the Feature

toJSON helper would be handy when taking snapshots of components through queries.

We are trying to migrate to v7 from native-testing-library. We don't like relying on snapshots but we do sometimes get aimed snapshots on specific components. At the moment, it causes a circular problem if trying to assert a snapshot on a component, eg:

expect(getByTestId('testID')).toMatchSnapshot();

We use to be able to do this but I guess with NTL we weren't consuming the ReactTestInstance directly so probably the problem was mitigated.

Possible Implementations

https://github.com/testing-library/native-testing-library/blob/a8ba4166942072ee420e2c77b7e377a605481e6b/src/lib/to-json.js

Is it possible to recycle the code from native-testing-library? I don't mind creating a PR but I do not know if we can recycle the code and if the helper is appropriate.

acatalina avatar Sep 07 '20 14:09 acatalina

getByTestId('testID').toJSON() is available? Docs: https://callstack.github.io/react-native-testing-library/docs/api/#tojson

thymikee avatar Sep 07 '20 14:09 thymikee

the docs you mentioned point at the ReactTestRenderer that you get from render. The ReactTestInstance does not contain that method.

https://github.com/callstack/react-native-testing-library/blob/5184f68894ab019ac8503210eb9aaac3a8ef9ac0/flow-typed/npm/react-test-renderer_v16.x.x.js#L21

acatalina avatar Sep 07 '20 14:09 acatalina

Um, sorry, I got you wrong. We support toJSON only as an output of calling render, right. Happy to get this feature in :)

thymikee avatar Sep 07 '20 15:09 thymikee

hehe no worries. Would it be ok license wise to just copy the code across?

acatalina avatar Sep 07 '20 15:09 acatalina

Should be fine 👍. We can always attribute the original source code

thymikee avatar Sep 07 '20 15:09 thymikee

Turns out a little bit more difficult to type this stuff. The implementation relies on internals of react-test-renderer that don't get expose, even though they are there.

Wary that, as any internals, are likely to change and break the implementation without us knowing.

Found this issue in react, I've commented about see what happens https://github.com/facebook/react/issues/14539

acatalina avatar Sep 08 '20 07:09 acatalina

Thanks! Let's see what core maintainers have to say about it. In the meantime I'd suggest using https://github.com/jest-community/snapshot-diff

thymikee avatar Sep 08 '20 08:09 thymikee

This issue is fundamentally related to React Test Renderer, we plan to submit PR there, so that we can re-export it from RNTL.

mdjastrzebski avatar Sep 26 '22 12:09 mdjastrzebski