Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spyOn getter requires explicit type on getter after Jest 24 #8137

Closed
pburkindine opened this issue Mar 16, 2019 · 9 comments
Closed

spyOn getter requires explicit type on getter after Jest 24 #8137

pburkindine opened this issue Mar 16, 2019 · 9 comments

Comments

@pburkindine
Copy link

pburkindine commented Mar 16, 2019

🐛 Bug Report

I am trying to upgrade our repos to jest 24 and encountered a compilation issue with spying on a mocked getter which worked in jest 23. The invocation is:

jest.spyOn(someMock, 'someProp', 'get').mockReturnValue(someStub);

... and the error is:

Argument of type '"someProp"' is not assignable to parameter of type 'never'.

I can rectify the situation by explicitly setting the return type of the mock function, but it doesn't seem like that should be necessary.

To Reproduce

Steps to reproduce the behavior:

Does not work:

class TestA {
  get foo() {
    return jest.fn();
  }
}

const mock = new TestA();
jest.spyOn(mock, 'foo', 'get').mockReturnValue('baz);

Does not work:

class TestA {
  get foo(): jest.Mock {
    return jest.fn();
  }
}

const mock = new TestA();
jest.spyOn(mock, 'foo', 'get').mockReturnValue('baz);

Works:

class TestA {
  get foo(): {} {
    return jest.fn();
  }
}

const mock = new TestA();
jest.spyOn(mock, 'foo', 'get').mockReturnValue('baz);

Works:

class TestA {
  get foo(): any {
    return jest.fn();
  }
}

const mock = new TestA();
jest.spyOn(mock, 'foo', 'get').mockReturnValue('baz);

Expected behavior

A jest.fn() being returned does not appear to satisfy the condition [K in keyof T]: T[K] extends (...args: any[]) => any in NonFunctionPropertyNames

If it is necessary to explicitly cast jest.fn() return from mocks, this should be reflected in the documentation.

Run npx envinfo --preset jest

Paste the results here:

  System:
    OS: macOS High Sierra 10.13.6
    CPU: (12) x64 Intel(R) Core(TM) i7-8850H CPU @ 2.60GHz
  Binaries:
    Node: 10.15.0 - ~/.nvm/versions/node/v10.15.0/bin/node
    Yarn: 1.13.0 - /usr/local/bin/yarn
    npm: 6.8.0 - ~/.nvm/versions/node/v10.15.0/bin/npm
  npmPackages:
    jest: ^24.5.0 => 24.5.0 
@SimenB
Copy link
Member

SimenB commented Mar 17, 2019

This isn't an error with jest, this is a type error from typescript. See @types/node where your types come from

@SimenB SimenB closed this as completed Mar 17, 2019
@pburkindine
Copy link
Author

@SimenB It seems like this issue was introduced by this commit on Feb 10, which introduced the NonFunctionPropertyNames type to spyOn in jest-mock:

00e52eb

This was working fine in Jest 23 and only occurs when returning a jest.fn(); I don't understand why that would not satisfy the new condition ((...args: any[]) => any) unless there is some foo surrounding the jest.fn() object

If you can please help me understand a bit better why this condition might be failing, I can file an issue with TS.

@SimenB
Copy link
Member

SimenB commented Mar 18, 2019

What you're getting is a type error. While Jest has indeed migrated to TS internally, we still don't distribute our types (it's probably confusing since we copied a lot of the types in jets-mock from @types/jest, so the code is very similar). Maybe it's DefinitelyTyped/DefinitelyTyped@d4fe875#diff-4a4390f0b4b1f7ac085a3d127ce47adb? You can look through https://github.com/DefinitelyTyped/DefinitelyTyped/commits/master/types/jest/index.d.ts and find it

However, if it's undesirable (I don't know) we should definitely change the types in Jest itself as well. I can reopen to track this, but there shouldn't really be a code change here until it's at least been discussed in the context of @types/jest

@SimenB SimenB reopened this Mar 18, 2019
@Godrules500
Copy link

I too am running into this error. Rather I have a type on the method or not I get the same errors. I'm on Jest and "@types/jest": "^24.0.18", and "jest": "^24.9.0" in an Angular environment.

Argument of type '"config"' is not assignable to parameter of type '"prototype"'
Argument of type '"appConfig"' is not assignable to parameter of type 'never'.

@hantsy
Copy link

hantsy commented Mar 12, 2021

After upgrading my nestjs project, I got similar error like this in jest testing codes. Jest version is 26.6.3.

 jest.spyOn(model, 'create').mockResolvedValue(toReturned);
                                                          ~~~~~~~~~~
    src/post/post.service.spec.ts:281:58 - error TS2345: Argument of type 'any' is not assignable to parameter of type 'never'.

    281     jest.spyOn(commentModel, 'create').mockResolvedValue({
                                                                 ~
    282       ...comment,
        ~~~~~~~~~~~~~~~~~
    283       post: { _id: 'test' },
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    284     } as any);

The create method is from Mongoose 5.12.0.

@hantsy
Copy link

hantsy commented Mar 12, 2021

I use mockImplementation(()=>Promise.resovle()) to resolve the problem temporarily. I think it is a problem of the incompatibility between @types/jest and Mongoose 5.12 to make it resolve the wrong type.

PS: Mongoose 5.11 introduce its own types instead of the @types/mongoose

@github-actions
Copy link

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days.

@github-actions github-actions bot added the Stale label Mar 12, 2022
@github-actions
Copy link

This issue was closed because it has been stalled for 7 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 12, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants