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

[Bug]: spying on getters that return complex functions throws type never error #4166

Open
jbsil opened this issue Jun 16, 2023 · 0 comments
Open

Comments

@jbsil
Copy link

jbsil commented Jun 16, 2023

Version

29.1.0

Steps to reproduce

jest.spyOn for getters/setters seems to work correctly with most Types, but breaks with any complex function type.

type CustomType = {
  hello: string;
  world: World;
}
class World {
  innitialize() {
    console.log('hello');
  }
};
class Example {
  get hello() {
    return 'world';
  }
  get world(): CustomType {
    return {
      hello: 'world',
      world: new World(),
    };
  }
  get nativeFunction(): Function {
    return () => {};
  }
  set nativeFunction(_: Function) {
    // do nothing
  }
  get worldFactory(): () => () => World{
    return () => () => new World();
  }
  set worldFactory(_: () => () => World) {
    // do nothing
  }
}

const example = new Example();
jest.spyOn(example, 'hello', 'get').mockReturnValue('mocked');
jest.spyOn(example, 'world', 'get').mockReturnValue({hello: 'mocked',world: new World()});
jest.spyOn(example, 'nativeFunction', 'get').mockReturnValue(jest.fn());
jest.spyOn(example, 'nativeFunction', 'set').mockImplementation(jest.fn());
jest.spyOn(example, 'worldFactory', 'get').mockReturnValue(() => () => new World());
//----------------------------------^ Argument of type 'string' is not assignable to parameter of type 'never'.
jest.spyOn(example, 'worldFactory', 'set').mockImplementation(jest.fn());
//----------------------------------^ Argument of type 'string' is not assignable to parameter of type 'never'.

Expected behavior

I expect that all Types should be usable while spying on getters and setters

Actual behavior

complex getters and setters always throw Argument of type 'string' is not assignable to parameter of type 'never'.
or Argument of type '"<getter name>"' is not assignable to parameter of type '"<other>" | "<valid>" | "<getters>"'. (The latter is probably keyOf Type, and the complex getter is somehow excluded from those keys?)

Debug log

ts-jest.log

Additional context

the error is different if you import jest from @jest/globals

import { jest } from '@jest/globals';
type CustomType = {
  hello: string;
  world: World;
}
class World {
  innitialize() {
    console.log('hello');
  }
};
class Example {
  get hello() {
    return 'world';
  }
  get world(): CustomType {
    return {
      hello: 'world',
      world: new World(),
    };
  }
  get nativeFunction(): Function {
    return () => {};
  }
  set nativeFunction(_: Function) {
    // do nothing
  }
  get worldFactory(): () => () => World{
    return () => () => new World();
  }
  set worldFactory(_: () => () => World) {
    // do nothing
  }
}

const example = new Example();
jest.spyOn(example, 'hello', 'get').mockReturnValue('mocked');
jest.spyOn(example, 'world', 'get').mockReturnValue({hello: 'mocked',world: new World()});
jest.spyOn(example, 'nativeFunction', 'get').mockReturnValue(jest.fn());
jest.spyOn(example, 'nativeFunction', 'set').mockImplementation(jest.fn());
jest.spyOn(example, 'worldFactory', 'get').mockReturnValue(() => () => new World());
//------------------^ Argument of type '"worldFactory"' is not assignable to parameter of type '"world" | "hello" | "nativeFunction"'.
jest.spyOn(example, 'worldFactory', 'set').mockImplementation(jest.fn());
//------------------^ Argument of type '"worldFactory"' is not assignable to parameter of type '"world" | "hello" | "nativeFunction"'.

Environment

System:
    OS: macOS 13.4
    CPU: (10) arm64 Apple M1 Pro
  Binaries:
    Node: 18.9.0 - ~/.nvm/versions/node/v18.9.0/bin/node
    npm: 8.19.1 - ~/.nvm/versions/node/v18.9.0/bin/npm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant