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

Forwarding generic leads to ReferenceError #971

Open
Philipp3211 opened this issue Nov 2, 2021 · 6 comments
Open

Forwarding generic leads to ReferenceError #971

Philipp3211 opened this issue Nov 2, 2021 · 6 comments

Comments

@Philipp3211
Copy link

While trying to extend the functionality of the createMock function, I tried to pass a generic to it. This makes it crash.

function createCustomMock<T extends object>(): T {
    return createMock<T>(); // ReferenceError: t is not defined
}
@uittorio
Copy link
Member

uittorio commented Nov 2, 2021

Hi @Philipp3211 !

Ts auto mock reads your code and replaces the createMock calls with a mock.

It is supposed to work the way your are describing.

Ts auto mock is not just a runtime library, it is also a compile time library.

Is there any specific reason you want to wrap it?

@Philipp3211
Copy link
Author

Philipp3211 commented Nov 2, 2021

It is supposed to work the way your are describing.

Do you mean the wrapping is not supposed to work, or do you mean that this generally should be possible and I have to fix my wrapper?

Is there any specific reason you want to wrap it?

I want to apply the workaround for #969 automatically when creating a class mock.

@uittorio
Copy link
Member

uittorio commented Nov 2, 2021

I meant that ts auto mock does not support your scenario because of the way that it generates the mocks.

What you can do is that you can create a function that receives the mock in input and changes the prototype, not sure if that works though.
i haven't tried

@uittorio
Copy link
Member

uittorio commented Nov 2, 2021

Something like this. it's seems to work

const hello = () => {
    return {
        a: 's'
    };
};

class AClass {}
const a = hello();
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
a.__proto__ = AClass.prototype;
if (a instanceof AClass) {
    console.log('hello');
} else {
    console.log('nope');
}

@Pmyl
Copy link
Collaborator

Pmyl commented Nov 2, 2021

createMock function cannot be used with a generics generics (lol) provided as parameter, the function to work around it should specify the specific class.

So instead of:

function wrap<T extends object>() {
  const mock = createMock<T>();
  // do more things
  return mock;
}

const workAroundMock = wrap<MyClass>();

It should be:

function wrap() {
  const mock = createMock<MyClass>();
  // do more things
  return mock;
}

const workAroundMock = wrap();

There are reasons behind this and some were explained by uittorio, I'm not at my pc right now so sorry for the formatting and not explaining everything

@Pmyl
Copy link
Collaborator

Pmyl commented Nov 2, 2021

I know this workaround is not perfect for your case, unfortunately we don't support making the mock an instance of the provided class. We tried and there are some weird scenarios where few requirements clash with each other.
We can investigate a bit more if it's something that affects more people but I can't promise we would be able to do it :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants