-
-
Notifications
You must be signed in to change notification settings - Fork 27
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
The new.target
is undefined even if spy is created with new keyword
#29
Comments
new.target
is undefined if spy is created with new keywordnew.target
is undefined even if spy is created with new keyword
Attempted a fix in #30 |
I was also working on it in #31 😄 |
Well! Works as long as it's fixed 😄 |
Adding a summary from #32 Verified that the following solution works: result = new.target
? new (fn.impl as any)(...args)
: fn.impl.apply(this, args)
if (new.target) Object.setPrototypeOf(r, this); Since MDN recommends against Object.setPrototypeOf because of performance impact, @sheremet-va questioned whether this should actually be considered a bug. It exists in sinon and jest-mock, other mock/spy utilities. |
We can add somewhere how it was called, for example. Store constructor/call string with calls. |
This issue is a bug as per the definition of new.target. Now tinyspy/vitest needs to decide whether they want to fix this bug, or want to retain this bug since it's present in sinon/jest-mock. If I was a maintainer, I would have chosen to fix it and either provide a documentation or transformation in a tool (like vitest-codemod) to fix this in users codebase while they migrate. For example, vitest provides documentation for the following things which are not supported:
I've added transformation in vitest-codemod for the following things which are not supported:
|
This will fix the issue in vitest, and not in tinyspy. That works for me as a user of vitest. |
It cannot be a bug by definition, because custom implementation is always „called“, not „constructed“. Because it is tinyspy who calls it, and not a user. And tinyspy has information if it was called with new. |
Since tinyspy is a spy library, should it imitate what user calls? |
How about adding the Something like this: try {
result = fn.impl.apply(this, args)
if (new.target)
fn.instances.push(this)
type = 'ok'
}
// ...
const reset = () => {
// ...
fn.instances = []
} And vitest could just export values from stub in it's spy implementation const mockContext = {
get calls() {
return stub.calls
},
get instances() {
return stub.instances
}, This will need v2.0.0 release for tinyspy though, to maintain backward compatibility. |
Describe the bug
The
new.target
is undefined if spy is created with new keyword.From MDN for new.target
Steps to reproduce
Observed behavior
Expected behavior
Additional context
Noticed while debugging an issue in vitest at vitest-dev/vitest#2821 (comment)
The text was updated successfully, but these errors were encountered: