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
fix: add error message when mock is missing export #1819
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -335,7 +335,38 @@ export class VitestMocker { | |
return this.request(dep) | ||
} | ||
|
||
private wrapFactoryInProxy(id: string, importer: string, factory?: () => unknown) { | ||
if (!factory) | ||
return factory | ||
|
||
const exportHandler = { | ||
get(target: Record<string, any>, prop: any) { | ||
const val = target[prop] | ||
|
||
// 'then' can exist on non-Promise objects, need nested instanceof check for logic to work | ||
if (prop === 'then') { | ||
if (target instanceof Promise) | ||
return target.then.bind(target) | ||
} | ||
else if (val === undefined) { | ||
throw new Error(`[vitest] No "${prop}" export is defined on the "${id}" mock defined here: ${importer}`) | ||
} | ||
|
||
return val | ||
}, | ||
} | ||
|
||
const factoryHandler = { | ||
apply(target: () => Object) { | ||
return new Proxy(target(), exportHandler) | ||
}, | ||
} | ||
|
||
return new Proxy(factory, factoryHandler) as () => unknown | ||
} | ||
|
||
public queueMock(id: string, importer: string, factory?: () => unknown) { | ||
factory = this.wrapFactoryInProxy(id, importer, factory) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't we just wrap exports here: ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And while we are here, let's throw an error, if factory returns non-object. (With a help message that it should return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I split the proxy objects. The export object proxy is now happening where you suggested, and the factory proxy object is now returning an error if the return value is not an object. |
||
VitestMocker.pendingIds.push({ type: 'mock', id, importer, factory }) | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need a proxy, if we can just check it here:
https://github.com/jereklas/vitest/blob/bdd74e50836a70d9aae410781f5c6fdbe5093f3a/packages/vitest/src/runtime/mocker.ts#L109
?
Factory can also return a promise that resolves to primitive
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need the proxy on the exports object because you don't know which export is being accessed until the calling code imports something?
I need to head to bed, but will try taking a look at your factory related concern tomorrow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do need a roxy for exports object, but I don't see why we need proxy for calling a factory - we are calling it ourselves on https://github.com/jereklas/vitest/blob/bdd74e50836a70d9aae410781f5c6fdbe5093f3a/packages/vitest/src/runtime/mocker.ts#L109
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah gotcha. I misunderstood your statement.