Incorrect types for lifecycle functions
See original GitHub issue🐛 Bug Report
The types for the lifecycle functions (beforeEach
etc.) are declared incorrectly in packages/jest-types/src/Global.ts
. These functions are documented as being able to return a promise or generator and accepting a timeout parameter but the defined types disallow anything except a void returning function.
To Reproduce
Take the following code:
import { beforeEach } from "@jest/globals";
beforeEach((): Promise<void> => {
return Promise.resolve();
}, 2000);
This causes TypeScript to complain with Expected 0-1 arguments, but got 2. ts(2554)
and causes the typescript-eslint rule no-misused-promises to complain with Promise returned in function argument where a void return was expected.
.
Expected behavior
The given code should not show any issues.
I think that the type for HookFn should be changed to something like this:
export type LifecycleFn = (done?: DoneFn) => Generator | Promise<void | undefined | unknown> | void | undefined;
export type HookFn = (fn: LifecycleFn, timeout?: number) => void;
envinfo
System:
OS: macOS 10.15.4
CPU: (12) x64 Intel(R) Core(TM) i9-8950HK CPU @ 2.90GHz
Binaries:
Node: 14.1.0 - /usr/local/bin/node
npm: 6.14.4 - /usr/local/bin/npm
npmPackages:
jest: ^26.0.1 => 26.0.1
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Lifecycle hooks - Angular
Use them to perform the following kinds of operations. Hook method, Purpose, Timing. ngOnChanges(), Respond when Angular sets or resets data-bound input ...
Read more >Amazon EC2 Auto Scaling lifecycle hooks
Amazon EC2 Auto Scaling offers the ability to add lifecycle hooks to your Auto Scaling groups. These hooks let you create solutions that...
Read more >ESLint complaining about empty constructor and ngOnInit ...
In my Angular application, the rule no-empty-function, triggered an error for my constructor. Well, ...
Read more >Lifecycle Hooks • Angular - codecraft.tv
A component in Angular has a life-cycle, a number of different phases it goes through from birth to death. We can hook into...
Read more >Angular lifecycle hooks explained - LogRocket Blog
The different lifecycle hooks for Angular components and directives ... export function checkAndUpdateDirectiveDynamic( view: ViewData, ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Closed by #10836, thanks @villasv!
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.