Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: call "cleanup" automatically #238

Merged
merged 15 commits into from May 20, 2020
34 changes: 34 additions & 0 deletions src/__tests__/auto-cleanup.js
@@ -0,0 +1,34 @@
import React from 'react';
import { View } from 'react-native';
import { render } from '..';

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', () => {
mdjastrzebski marked this conversation as resolved.
Show resolved Hide resolved
expect(isMounted).toEqual(false);
});
2 changes: 1 addition & 1 deletion src/__tests__/cleanup.test.js
Expand Up @@ -2,7 +2,7 @@
/* eslint-disable react/no-multi-comp */
import React from 'react';
import { View } from 'react-native';
import { cleanup, render } from '..';
import { cleanup, render } from '../pure';

class Test extends React.Component<*> {
componentWillUnmount() {
Expand Down
2 changes: 2 additions & 0 deletions src/__tests__/waitFor.test.js
Expand Up @@ -55,6 +55,8 @@ test('waits for element until timeout is met', async () => {
await expect(
waitFor(() => getByText('Fresh'), { timeout: 100 })
).rejects.toThrow();

await waitFor(() => getByText('Fresh'));
thymikee marked this conversation as resolved.
Show resolved Hide resolved
});

test('waits for element with custom interval', async () => {
Expand Down
31 changes: 15 additions & 16 deletions src/index.js
@@ -1,18 +1,17 @@
// @flow
import act from './act';
import cleanup from './cleanup';
import fireEvent from './fireEvent';
import flushMicrotasksQueue from './flushMicrotasksQueue';
thymikee marked this conversation as resolved.
Show resolved Hide resolved
import render from './render';
import shallow from './shallow';
import waitFor, { waitForElement } from './waitFor';
import within from './within';
import { cleanup } from './pure';

export { act };
export { cleanup };
export { fireEvent };
export { flushMicrotasksQueue };
export { render };
export { shallow };
export { waitFor, waitForElement };
export { within };
// If we're running in a test runner that supports afterEach
// then we'll automatically run cleanup afterEach test
// this ensures that tests run in isolation from each other
// if you don't like this then either import the `pure` module
// or set the RNTL_SKIP_AUTO_CLEANUP env variable to 'true'.
if (typeof afterEach === 'function' && !process.env.RNTL_SKIP_AUTO_CLEANUP) {
// eslint-disable-next-line no-undef
afterEach(async () => {
await flushMicrotasksQueue();
cleanup();
});
}

export * from './pure';
18 changes: 18 additions & 0 deletions src/pure.js
@@ -0,0 +1,18 @@
// @flow
import act from './act';
import cleanup from './cleanup';
import fireEvent from './fireEvent';
import flushMicrotasksQueue from './flushMicrotasksQueue';
import render from './render';
import shallow from './shallow';
import waitFor, { waitForElement } from './waitFor';
import within from './within';

export { act };
export { cleanup };
export { fireEvent };
export { flushMicrotasksQueue };
export { render };
export { shallow };
export { waitFor, waitForElement };
export { within };
2 changes: 2 additions & 0 deletions website/docs/API.md
Expand Up @@ -123,6 +123,8 @@ const cleanup: () => void;

Unmounts React trees that were mounted with `render`.

> Please note that this is done automatically if the testing framework you're using supports the `afterEach` global (like mocha, Jest, and Jasmine). If not, you will need to do manual cleanups after each test.
mdjastrzebski marked this conversation as resolved.
Show resolved Hide resolved

For example, if you're using the `jest` testing framework, then you would need to use the `afterEach` hook like so:

```jsx
thymikee marked this conversation as resolved.
Show resolved Hide resolved
Expand Down