-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The previous version of async act detection left an open hanging act scope, which broke tests and expectations. This PR delays the detection until it's been called at least once.
- Loading branch information
Sunil Pai
authored and
Kent C. Dodds
committed
Jul 23, 2019
1 parent
4aa0c56
commit ffe2b79
Showing
4 changed files
with
325 additions
and
59 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
let asyncAct | ||
|
||
jest.mock('react-dom/test-utils', () => ({ | ||
act: cb => { | ||
return cb() | ||
}, | ||
})) | ||
|
||
beforeEach(() => { | ||
jest.resetModules() | ||
asyncAct = require('../act-compat').asyncAct | ||
jest.spyOn(console, 'error').mockImplementation(() => {}) | ||
}) | ||
|
||
afterEach(() => { | ||
console.error.mockRestore() | ||
}) | ||
|
||
test('async act works when it does not exist (older versions of react)', async () => { | ||
const callback = jest.fn() | ||
await asyncAct(async () => { | ||
await Promise.resolve() | ||
await callback() | ||
}) | ||
expect(console.error).toHaveBeenCalledTimes(0) | ||
expect(callback).toHaveBeenCalledTimes(1) | ||
|
||
callback.mockClear() | ||
console.error.mockClear() | ||
|
||
await asyncAct(async () => { | ||
await Promise.resolve() | ||
await callback() | ||
}) | ||
expect(console.error).toHaveBeenCalledTimes(0) | ||
expect(callback).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
test('async act recovers from errors', async () => { | ||
try { | ||
await asyncAct(async () => { | ||
await null | ||
throw new Error('test error') | ||
}) | ||
} catch (err) { | ||
console.error('call console.error') | ||
} | ||
expect(console.error).toHaveBeenCalledTimes(1) | ||
expect(console.error.mock.calls).toMatchInlineSnapshot(` | ||
Array [ | ||
Array [ | ||
"call console.error", | ||
], | ||
] | ||
`) | ||
}) | ||
|
||
test('async act recovers from sync errors', async () => { | ||
try { | ||
await asyncAct(() => { | ||
throw new Error('test error') | ||
}) | ||
} catch (err) { | ||
console.error('call console.error') | ||
} | ||
expect(console.error).toHaveBeenCalledTimes(1) | ||
expect(console.error.mock.calls).toMatchInlineSnapshot(` | ||
Array [ | ||
Array [ | ||
"call console.error", | ||
], | ||
] | ||
`) | ||
}) | ||
|
||
/* eslint no-console:0 */ |
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 |
---|---|---|
@@ -1,9 +1,80 @@ | ||
import {act} from '..' | ||
let act, asyncAct | ||
|
||
beforeEach(() => { | ||
jest.resetModules() | ||
act = require('..').act | ||
asyncAct = require('../act-compat').asyncAct | ||
jest.spyOn(console, 'error').mockImplementation(() => {}) | ||
}) | ||
|
||
afterEach(() => { | ||
console.error.mockRestore() | ||
}) | ||
|
||
jest.mock('react-dom/test-utils', () => ({})) | ||
|
||
test('act works even when there is no act from test utils', () => { | ||
const callback = jest.fn() | ||
act(callback) | ||
expect(callback).toHaveBeenCalledTimes(1) | ||
expect(console.error).toHaveBeenCalledTimes(0) | ||
}) | ||
|
||
test('async act works when it does not exist (older versions of react)', async () => { | ||
const callback = jest.fn() | ||
await asyncAct(async () => { | ||
await Promise.resolve() | ||
await callback() | ||
}) | ||
expect(console.error).toHaveBeenCalledTimes(0) | ||
expect(callback).toHaveBeenCalledTimes(1) | ||
|
||
callback.mockClear() | ||
console.error.mockClear() | ||
|
||
await asyncAct(async () => { | ||
await Promise.resolve() | ||
await callback() | ||
}) | ||
expect(console.error).toHaveBeenCalledTimes(0) | ||
expect(callback).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
test('async act recovers from errors', async () => { | ||
try { | ||
await asyncAct(async () => { | ||
await null | ||
throw new Error('test error') | ||
}) | ||
} catch (err) { | ||
console.error('call console.error') | ||
} | ||
expect(console.error).toHaveBeenCalledTimes(1) | ||
expect(console.error.mock.calls).toMatchInlineSnapshot(` | ||
Array [ | ||
Array [ | ||
"call console.error", | ||
], | ||
] | ||
`) | ||
}) | ||
|
||
test('async act recovers from sync errors', async () => { | ||
try { | ||
await asyncAct(() => { | ||
throw new Error('test error') | ||
}) | ||
} catch (err) { | ||
console.error('call console.error') | ||
} | ||
expect(console.error).toHaveBeenCalledTimes(1) | ||
expect(console.error.mock.calls).toMatchInlineSnapshot(` | ||
Array [ | ||
Array [ | ||
"call console.error", | ||
], | ||
] | ||
`) | ||
}) | ||
|
||
/* eslint no-console:0 */ |
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
Oops, something went wrong.