-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Isn't name given to 'instances' property of 'mockFn.mock' is little misleading? #7070
Comments
instances
property of mockFn.mock
is little misleading?
I agree it's confusing, but I'm not sure what the best solution is... |
I don't think the issue is the binding, I think the issue is pushing to For example, this test fails (notice that test('no instances', () => {
const Car = jest.fn();
const car = Car(); // Note no "new" keyword
expect(Car.mock.instances).toHaveLength(0);
}); When you do this with the bind and foo in OP, you get test('no instances', () => {
const Car = jest.fn();
const foo = {};
const bar = Car.bind(foo);
bar();
expect(Car.mock.instances).toHaveLength(0);
}); |
@rickhanlonii Are you saying that nothing should be pushed onto the The current design of how all the call information for mocks is maintained is with parallel arrays, where index 1 (for example) in all the arrays is expected to contain the relevant information for the second call to the mock. Based on this design, pushing It would be nice to combine all of these parallel arrays into a single array of objects where each entry in the one array contains ALL information about how the mock was called, how it completed, what it returned/threw, etc. |
According to this, it is possible to detect whether a function was called as a constructor as of ECMAScript 6: https://stackoverflow.com/questions/367768/how-to-detect-if-a-function-is-called-as-constructor/31060154#31060154
I don't quite understand all possible environments that Jest can execute within. Can we assume that |
|
Another thing to consider: if a function used as a constructor returns an object, then that return value is returned by the In that situation, do we still want to store |
I'm not sure to be honest 😅 It's been there since the "FIRST" commit back in 2014, but I can't find it in Jasmine's docs. I'd happily remove it since I've never had any use for it whatsoever, but that might just be me. What use-cases are covered by |
Since If that's the case, maybe it could be wrapped in Short version: It probably requires some experimentation, but I wouldn't be surprised if it was impossible to both use new.target and have code that doesn't blow up in IE11. |
I don't see how One simple possibility is to rename/document |
I meant |
Still, there's no overlap between Aside from the (probably rare) edge case I mentioned a few comments ago about implementing a multiton pattern in the constructor, constructor functions themselves don't return anything. The |
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 14 days. |
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. |
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. |
Suppose we have a mock constructor which we can use to create instances of
Car
like this:Snippet of code above makes sense to me, but what you will encounter below can mislead some developers
Since
foo
is not instance ofCar
, it was just bound to bethis
context upon invokingbar
as a regular function, so pushing it toCar.mock.instances
does not make much sense. Shouldn'tinstances
property be better named/aliased ascontextObjects
to get rid of this confusion?The text was updated successfully, but these errors were encountered: