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 2 commits
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export default 'a default' |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,7 +108,25 @@ export class VitestMocker { | |
return cached | ||
const exports = await mock() | ||
this.moduleCache.set(dep, { exports }) | ||
return exports | ||
|
||
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 "${dep}"`) | ||
} | ||
|
||
return val | ||
}, | ||
} | ||
|
||
return new Proxy(exports, exportHandler) | ||
} | ||
|
||
private getMockPath(dep: string) { | ||
|
@@ -336,6 +354,16 @@ export class VitestMocker { | |
} | ||
|
||
public queueMock(id: string, importer: string, factory?: () => unknown) { | ||
const factoryHandler = { | ||
apply(target: () => unknown) { | ||
const value = target() | ||
if (typeof value !== '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 commentThe 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: Factory can also return a promise that resolves to primitive 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. 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Ah gotcha. I misunderstood your statement. |
||
|
||
factory = factory === undefined ? factory : new Proxy(factory, factoryHandler) | ||
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.
Should also check
null
, i think?