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

fix: add error message when mock is missing export #1819

Merged

Conversation

jereklas
Copy link
Contributor

@jereklas jereklas commented Aug 8, 2022

No description provided.

@jereklas jereklas changed the title add useful error when mock is missing exports fix: Add error message when mock is missing export (fix #1763) Aug 8, 2022
@jereklas jereklas force-pushed the add-error-on-missing-mock-exports branch from b324c86 to d66dc3e Compare August 8, 2022 03:44
@jereklas jereklas force-pushed the add-error-on-missing-mock-exports branch from d66dc3e to 9d5234e Compare August 8, 2022 06:32
public queueMock(id: string, importer: string, factory?: () => unknown) {
factory = this.wrapFactoryInProxy(id, importer, factory)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

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)

Copy link
Contributor Author

@jereklas jereklas Aug 8, 2022

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
},
}
Copy link
Member

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

Copy link
Contributor Author

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.

Copy link
Member

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

Copy link
Contributor Author

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')
Copy link
Member

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?

@jereklas
Copy link
Contributor Author

jereklas commented Aug 8, 2022

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?

@alfiomy
Copy link

alfiomy commented Aug 8, 2022

Good

@sheremet-va sheremet-va changed the title fix: Add error message when mock is missing export (fix #1763) fix: add error message when mock is missing export Aug 8, 2022
@sheremet-va sheremet-va merged commit 7574759 into vitest-dev:main Aug 8, 2022
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

Successfully merging this pull request may close these issues.

None yet

3 participants