-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(cleanup): automatically cleanup if afterEach is detected #430
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import React from 'react' | ||
|
||
let render | ||
beforeAll(() => { | ||
process.env.RTL_SKIP_CLEANUP = 'true' | ||
const rtl = require('../') | ||
render = rtl.render | ||
}) | ||
|
||
// This one verifies that if RTL_SKIP_CLEANUP is set | ||
// that we DON'T auto-wire up the afterEach for folks | ||
test('first', () => { | ||
render(<div>hi</div>) | ||
}) | ||
|
||
test('second', () => { | ||
expect(document.body.innerHTML).toEqual('<div><div>hi</div></div>') | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import React from 'react' | ||
import {render} from '../' | ||
|
||
// This just verifies that by importing RTL in an | ||
// environment which supports afterEach (like jest) | ||
// we'll get automatic cleanup between tests. | ||
test('first', () => { | ||
render(<div>hi</div>) | ||
}) | ||
|
||
test('second', () => { | ||
expect(document.body.innerHTML).toEqual('') | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -142,6 +142,16 @@ fireEvent.select = (node, init) => { | |
fireEvent.keyUp(node, init) | ||
} | ||
|
||
// 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 (typeof afterEach === 'function' && !process.env.RTL_SKIP_CLEANUP) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should guard against process and process.env not being defined for people running their tests in actual browsers. |
||
afterEach(async () => { | ||
await asyncAct(async () => {}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider - if ther are any hanging promises after a test is finished, that probably means the user hasn't asserted on the actual stable state of the UI. So this would hide a probable bug. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe this is a similar concern with the async act() in cleanup-after-each, ouch. at least there it's explicit tho. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I'm not sure I understand how this change impacts that at all. This change is doing exactly the same thing that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe this shouldn't be an act(), but it should flush promises anyway. So if there are any stray async updates, the warning will fire, and the dev will account for it in their test. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I'm suggesting it should be removed from that too. |
||
cleanup() | ||
}) | ||
} | ||
|
||
// just re-export everything from dom-testing-library | ||
export * from '@testing-library/dom' | ||
export {render, cleanup, fireEvent, act} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1 @@ | ||
import '@testing-library/jest-dom/extend-expect' | ||
|
||
afterEach(() => { | ||
// have to do a dynamic import so we don't mess up jest mocking for old-act.js | ||
require('../src').cleanup() | ||
}) |
Uh oh!
There was an error while loading. Please reload this page.