-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix: add error message when mock is missing export #1819
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
Conversation
b324c86
to
d66dc3e
Compare
d66dc3e
to
9d5234e
Compare
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 comment
The 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 comment
The 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 { default }
, if user wants to mock default export)
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.
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.
throw new Error(`[vitest] vi.mock("${id}", factory?: () => unknown) is not returning an object. Did you mean to return an object with a "default" key?`) | ||
return value | ||
}, | ||
} |
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.
const factoryHandler = { | ||
apply(target: () => unknown) { | ||
const value = target() | ||
if (typeof value !== 'object') |
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.
Should also check null
, i think?
Looks like a test unrelated to my changes failed in CI? I ran them locally without any issue. What's the process for getting tests re-ran? |
Good |
No description provided.