Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #199 from testing-library/pr/cleanup
Global cleanup
- Loading branch information
Showing
7 changed files
with
180 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
afterEach(async () => { | ||
await cleanup() | ||
}) | ||
} | ||
|
||
export { cleanup, addCleanup, removeCleanup } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |