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:
Jessica
2019-02-06 19:07:43 +09:00
parent 1755edd5fc
commit 8af624afbe
4 changed files with 27 additions and 2 deletions

View File

@@ -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);
});
});
});

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -73,3 +73,5 @@ act(() => {});
act(async () => {});
// $ExpectError
act(() => null);
// $ExpectError
Promise.resolve(act(() => {}));