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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Argument of type X is not assignable to parameter of type 'never' #2610

Closed
ypicard opened this issue May 25, 2021 · 3 comments
Closed

Argument of type X is not assignable to parameter of type 'never' #2610

ypicard opened this issue May 25, 2021 · 3 comments
Labels

Comments

@ypicard
Copy link

ypicard commented May 25, 2021

馃悰 Bug Report

I am trying to use ts-jest to mock one of my dependencies. This dependency (auth0.ManageClient) has multiple definitions for a same method and overrides it based on the params you pass it:

    // Roles
    getRoles(): Promise<Role[]>;
    getRoles(cb: (err: Error, roles: Role[]) => void): void;
    getRoles(params: GetRolesData): Promise<Role[]>;
    getRoles(params: GetRolesData, cb: (err: Error, roles: Role[]) => void): void;
    getRoles(params: GetRolesDataPaged): Promise<RolePage>;
    getRoles(params: GetRolesDataPaged, cb: (err: Error, rolePage: RolePage) => void): void;

When trying to mock the getRoles method, I get the following message: Argument of type X is not assignable to parameter of type 'never'.

I think this is because ts-jest is expecting me to override the getRoles(cb: (err: Error, roles: Role[]) => void): void; which is not what I am trying to do. I want to mock its result with .mockResolvedValue.

To Reproduce

import { ManagementClient } from 'auth0';
import { MockedObjectDeep } from 'ts-jest/dist/utils/testing';
import { mocked } from 'ts-jest/utils';

jest.mock('auth0');

describe('UsersService', () => {
    let manageClient: MockedObjectDeep<ManagementClient>;

    beforeEach(() => {
        jest.clearAllMocks();

        manageClient = mocked(
            new ManagementClient({
                domain: 'DOMAIN',
                clientId: 'CLIENT_ID',
                clientSecret: 'CLIENT_SECRET',
            }),
            true
        );
    });

    describe('updateUserRoles', () => {
        beforeEach(() => {
            manageClient.getRoles.mockResolvedValue([]); // error will show here
        });

        it('should test something', async () => {
            //    ...
        });
    });
});

Expected behavior

Typings should support method overriding.

@ypicard ypicard added Bug Report Needs Repo Need a minimium repository to reproduce the problem Needs Triage labels May 25, 2021
@ahnpnl ahnpnl added 馃悰 Bug Confirmed Bug is confirmed and removed Bug Report Needs Repo Need a minimium repository to reproduce the problem Needs Triage labels May 25, 2021
@spridev
Copy link

spridev commented May 26, 2021

The same problem is occuring when using @aws-sdk modules:

import { Readable } from 'stream'
import { mocked } from 'ts-jest/utils'
import { S3 } from '@aws-sdk/client-s3'

jest.mock('@aws-sdk/client-s3')

const S3Mock = mocked(S3, true)

test('get-object', async function () {
  const data = Readable.from('data')

  // TS2322: Type 'Readable' is not assignable to type 'never'.
  S3Mock.prototype.getObject.mockResolvedValue({ Body: data })
})

I'm using the following code as a temporary fix:

S3Mock.prototype.getObject.mockImplementation(() => ({ Body: data }))

Hope it helps.

@ahnpnl
Copy link
Collaborator

ahnpnl commented Jul 25, 2021

PR is welcome if you guys know the fix :)

@ahnpnl
Copy link
Collaborator

ahnpnl commented Dec 5, 2021

close as won't fix since mocked now is included in jest-mock, see jestjs/jest#12089

@ahnpnl ahnpnl closed this as completed Dec 5, 2021
@ahnpnl ahnpnl added WontFix and removed 馃悰 Bug Confirmed Bug is confirmed labels Dec 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants