Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added ways to prevent auto cleanup
  • Loading branch information
mdjastrzebski committed May 20, 2020
commit b8f96a942364b614543a6c2703435b5cacd6f8d7
1 change: 1 addition & 0 deletions dont-cleanup-after-each.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
process.env.RNTL_SKIP_AUTO_CLEANUP = true;
2 changes: 2 additions & 0 deletions pure.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// makes it so people can import from 'react-native-testing-library/pure'
module.exports = require('./build/pure');
40 changes: 40 additions & 0 deletions src/__tests__/auto-cleanup-skip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import React from 'react';
import { View } from 'react-native';

let render;
beforeAll(() => {
process.env.RTL_SKIP_AUTO_CLEANUP = 'true';
const rtl = require('../');
render = rtl.render;
});

let isMounted = false;

class Test extends React.Component<*> {
componentDidMount() {
isMounted = true;
}

componentWillUnmount() {
isMounted = false;
if (this.props.onUnmount) {
this.props.onUnmount();
}
}
render() {
return <View />;
}
}

// This just verifies that by importing RNTL in an
// environment which supports afterEach (like jest)
// we'll get automatic cleanup between tests.
test('first', () => {
const fn = jest.fn();
render(<Test onUnmount={fn} />);
expect(fn).not.toHaveBeenCalled();
});

test('second', () => {
expect(isMounted).toEqual(true);
});
6 changes: 5 additions & 1 deletion website/docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ Re-render the in-memory tree with a new root element. This simulates a React upd
unmount(): void
```

Unmount the in-memory tree, triggering the appropriate lifecycle events
Unmount the in-memory tree, triggering the appropriate lifecycle events.

:::note
Usually you should not need to call `unmount` as it is done automatically if your test runner supports `afterEach` hook (like Jest, mocha, Jasmine).
:::

### `debug`

Expand Down
24 changes: 24 additions & 0 deletions website/docs/Migration20.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,30 @@ This guides describes major steps involved in migrating your testing code from u

Node 8 reached its EOL more than 5 months ago, so it's about time to target the library to Node 10. If you used lower version, you'll have to upgrade to v10, but we suggest using the latest LTS version.

## Auto Cleanup

`cleanup()` function is now called automatically after every test, if your testing framework supports `afterEach` hook (like Jest, mocha, and Jasmine).

You should be able to safely remove all `afterEach(cleanup)` calls in your code.

This change might break your code, if you tests are not isolated, i.e. you call `render` outside `test` block. Generally, you should [keep your tests isolated](https://kentcdodds.com/blog/test-isolation-with-react), but if you can't or don't want to do this right away you can prevent this behavior using any of the foloowing ways:

1. by importing `'react-native-testing-library/pure'` instead of `'react-native-testing-library'`

2. by importing `'react-native-testing-library/dont-cleanup-after-each'` before importing `'react-native-testing-library'`. You can do it in a global way by using Jest's `setupFiles` like this:

```js
{
setupFiles: ['react-native-testing-library/dont-cleanup-after-each'];
}
```

3. by setting `RTNL_SKIP_AUTO_CLEANUP` env variable to `true`. You can do this with `cross-evn` like this:

```sh
cross-env RNTL_SKIP_AUTO_CLEANUP=true jest
```

## WaitFor API changes

`waitForElement` function has been renamed to `waitFor` for consistency with React Testing Library. Additionally the signature has slightly changed from:
Expand Down