mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-06-28 22:30:01 +00:00
Use contrivances to present something close to act()'s real return type
Make it harder to accidentally use it in contexts where a Promise may be expected.
This commit is contained in:
@@ -191,5 +191,10 @@ describe('React dom test utils', () => {
|
||||
// $ExpectError
|
||||
ReactTestUtils.act(() => null);
|
||||
});
|
||||
it('returns a Promise-like that errors out on use', () => {
|
||||
const result = ReactTestUtils.act(() => {});
|
||||
// $ExpectError
|
||||
Promise.resolve(result);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
11
types/react-dom/test-utils/index.d.ts
vendored
11
types/react-dom/test-utils/index.d.ts
vendored
@@ -291,4 +291,13 @@ export function createRenderer(): ShallowRenderer;
|
||||
* @see https://reactjs.org/blog/2019/02/06/react-v16.8.0.html#testing-hooks
|
||||
*/
|
||||
// the "void | undefined" is here to forbid any sneaky "Promise" returns.
|
||||
export function act(callback: () => void | undefined): void;
|
||||
// the actual return value is always a "DebugPromiseLike",
|
||||
// but having an "| {}" makes it harder to accidentally use.
|
||||
export function act(callback: () => void | undefined): DebugPromiseLike | {};
|
||||
|
||||
// Intentionally doesn't extend PromiseLike<never>.
|
||||
// Ideally this should be as hard to accidentally use as possible.
|
||||
interface DebugPromiseLike {
|
||||
// the actual then() in here is 0-ary, but that doesn't count as a PromiseLike.
|
||||
then(onfulfilled: (value: never) => never, onrejected: (reason: never) => never): never;
|
||||
}
|
||||
|
||||
11
types/react-test-renderer/index.d.ts
vendored
11
types/react-test-renderer/index.d.ts
vendored
@@ -64,4 +64,13 @@ export function create(nextElement: ReactElement<any>, options?: TestRendererOpt
|
||||
* @see https://reactjs.org/blog/2019/02/06/react-v16.8.0.html#testing-hooks
|
||||
*/
|
||||
// the "void | undefined" is here to forbid any sneaky "Promise" returns.
|
||||
export function act(callback: () => void | undefined): void;
|
||||
// the actual return value is always a "DebugPromiseLike",
|
||||
// but having an "| {}" makes it harder to accidentally use.
|
||||
export function act(callback: () => void | undefined): DebugPromiseLike | {};
|
||||
|
||||
// Intentionally doesn't extend PromiseLike<never>.
|
||||
// Ideally this should be as hard to accidentally use as possible.
|
||||
interface DebugPromiseLike {
|
||||
// the actual then() in here is 0-ary, but that doesn't count as a PromiseLike.
|
||||
then(onfulfilled: (value: never) => never, onrejected: (reason: never) => never): never;
|
||||
}
|
||||
|
||||
@@ -73,3 +73,5 @@ act(() => {});
|
||||
act(async () => {});
|
||||
// $ExpectError
|
||||
act(() => null);
|
||||
// $ExpectError
|
||||
Promise.resolve(act(() => {}));
|
||||
|
||||
Reference in New Issue
Block a user