Bring toJSON from native-testing-library
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.
getByTestId('testID').toJSON() is available? Docs: https://callstack.github.io/react-native-testing-library/docs/api/#tojson
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
Um, sorry, I got you wrong. We support toJSON only as an output of calling render, right. Happy to get this feature in :)
hehe no worries. Would it be ok license wise to just copy the code across?
Should be fine 👍. We can always attribute the original source code
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
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
This issue is fundamentally related to React Test Renderer, we plan to submit PR there, so that we can re-export it from RNTL.