question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
SimenBcommented, Dec 4, 2020

Closed by #10836, thanks @villasv!

0reactions
github-actions[bot]commented, May 11, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found