-
Notifications
You must be signed in to change notification settings - Fork 232
Global cleanup #199
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
Global cleanup #199
Changes from all commits
183c3ce
ba02be0
73c1f61
217e299
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,26 @@ | ||
import { act } from 'react-test-renderer' | ||
|
||
let cleanupCallbacks = [] | ||
|
||
async function cleanup() { | ||
await act(async () => {}) | ||
cleanupCallbacks.forEach((cb) => cb()) | ||
cleanupCallbacks = [] | ||
} | ||
|
||
function addCleanup(callback) { | ||
cleanupCallbacks.push(callback) | ||
} | ||
|
||
function removeCleanup(callback) { | ||
cleanupCallbacks = cleanupCallbacks.filter((cb) => cb !== callback) | ||
} | ||
|
||
// Automatically registers cleanup in supported testing frameworks | ||
if (typeof afterEach === 'function' && !process.env.RHTL_SKIP_AUTO_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. 📓 Do we want this to explicitly be the string 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. Again, this is implemented as per 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. Cool. Nice to see sharing the pattern from RTL for dev familiarity. |
||
afterEach(async () => { | ||
await cleanup() | ||
}) | ||
} | ||
|
||
export { cleanup, addCleanup, removeCleanup } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { useEffect } from 'react' | ||
|
||
// This verifies that if RHTL_SKIP_AUTO_CLEANUP is set | ||
// then we DON'T auto-wire up the afterEach for folks | ||
describe('skip auto cleanup (disabled) tests', () => { | ||
let cleanupCalled = false | ||
let renderHook | ||
|
||
beforeAll(() => { | ||
process.env.RHTL_SKIP_AUTO_CLEANUP = 'true' | ||
renderHook = require('src').renderHook | ||
}) | ||
|
||
test('first', () => { | ||
const hookWithCleanup = () => { | ||
useEffect(() => { | ||
return () => { | ||
cleanupCalled = true | ||
} | ||
}) | ||
} | ||
renderHook(() => hookWithCleanup()) | ||
}) | ||
|
||
test('second', () => { | ||
expect(cleanupCalled).toBe(false) | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { useEffect } from 'react' | ||
|
||
// This verifies that if RHTL_SKIP_AUTO_CLEANUP is set | ||
// then we DON'T auto-wire up the afterEach for folks | ||
describe('skip auto cleanup (no afterEach) tests', () => { | ||
let cleanupCalled = false | ||
let renderHook | ||
|
||
beforeAll(() => { | ||
afterEach = false | ||
renderHook = require('src').renderHook | ||
}) | ||
|
||
test('first', () => { | ||
const hookWithCleanup = () => { | ||
useEffect(() => { | ||
return () => { | ||
cleanupCalled = true | ||
} | ||
}) | ||
} | ||
renderHook(() => hookWithCleanup()) | ||
}) | ||
|
||
test('second', () => { | ||
expect(cleanupCalled).toBe(false) | ||
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. ❓ Is this test supposed to check It appears to be a copy of the above test, You may need to clear the 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. No. In the Happy to hear ideas for better ways to test this case. 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. welp |
||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { useEffect } from 'react' | ||
import { renderHook } from 'src' | ||
|
||
// This verifies that by importing RHTL in an | ||
// environment which supports afterEach (like jest) | ||
// we'll get automatic cleanup between tests. | ||
describe('auto cleanup tests', () => { | ||
let cleanupCalled = false | ||
|
||
test('first', () => { | ||
const hookWithCleanup = () => { | ||
useEffect(() => { | ||
return () => { | ||
cleanupCalled = true | ||
} | ||
}) | ||
} | ||
renderHook(() => hookWithCleanup()) | ||
}) | ||
|
||
test('second', () => { | ||
expect(cleanupCalled).toBe(true) | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { useEffect } from 'react' | ||
import { renderHook, cleanup } from 'src' | ||
|
||
describe('cleanup tests', () => { | ||
test('should flush effects on cleanup', async () => { | ||
let cleanupCalled = false | ||
|
||
const hookWithCleanup = () => { | ||
useEffect(() => { | ||
return () => { | ||
cleanupCalled = true | ||
} | ||
}) | ||
} | ||
|
||
renderHook(() => hookWithCleanup()) | ||
|
||
await cleanup() | ||
|
||
expect(cleanupCalled).toBe(true) | ||
}) | ||
|
||
test('should cleanup all rendered hooks', async () => { | ||
let cleanupCalled = [] | ||
const hookWithCleanup = (id) => { | ||
useEffect(() => { | ||
return () => { | ||
cleanupCalled[id] = true | ||
} | ||
}) | ||
} | ||
|
||
renderHook(() => hookWithCleanup(1)) | ||
renderHook(() => hookWithCleanup(2)) | ||
|
||
await cleanup() | ||
|
||
expect(cleanupCalled[1]).toBe(true) | ||
expect(cleanupCalled[2]).toBe(true) | ||
}) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❓ Do you happen to have any reference or example of a testing library auto adding a global
afterEach
?I think I like this, i'm just curious about community best practices and such.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The registration of this is lifted almost verbatim from
@testing-library/react-testing-library#430
I don't think this is a commonly used pattern (yet).