formacao
This commit is contained in:
+21
-7
@@ -152,21 +152,25 @@ class ManualMockedModule {
|
||||
this.url = url;
|
||||
this.factory = factory;
|
||||
}
|
||||
async resolve() {
|
||||
resolve() {
|
||||
if (this.cache) {
|
||||
return this.cache;
|
||||
}
|
||||
let exports$1;
|
||||
try {
|
||||
exports$1 = await this.factory();
|
||||
exports$1 = this.factory();
|
||||
} catch (err) {
|
||||
const vitestError = new Error("[vitest] There was an error when mocking a module. " + "If you are using \"vi.mock\" factory, make sure there are no top level variables inside, since this call is hoisted to top of the file. " + "Read more: https://vitest.dev/api/vi.html#vi-mock");
|
||||
vitestError.cause = err;
|
||||
throw vitestError;
|
||||
throw createHelpfulError(err);
|
||||
}
|
||||
if (exports$1 === null || typeof exports$1 !== "object" || Array.isArray(exports$1)) {
|
||||
throw new TypeError(`[vitest] vi.mock("${this.raw}", factory?: () => unknown) is not returning an object. Did you mean to return an object with a "default" key?`);
|
||||
if (typeof exports$1 === "object" && typeof exports$1?.then === "function") {
|
||||
return exports$1.then((result) => {
|
||||
assertValidExports(this.raw, result);
|
||||
return this.cache = result;
|
||||
}, (error) => {
|
||||
throw createHelpfulError(error);
|
||||
});
|
||||
}
|
||||
assertValidExports(this.raw, exports$1);
|
||||
return this.cache = exports$1;
|
||||
}
|
||||
static fromJSON(data, factory) {
|
||||
@@ -181,5 +185,15 @@ class ManualMockedModule {
|
||||
};
|
||||
}
|
||||
}
|
||||
function createHelpfulError(cause) {
|
||||
const error = new Error("[vitest] There was an error when mocking a module. " + "If you are using \"vi.mock\" factory, make sure there are no top level variables inside, since this call is hoisted to top of the file. " + "Read more: https://vitest.dev/api/vi.html#vi-mock");
|
||||
error.cause = cause;
|
||||
return error;
|
||||
}
|
||||
function assertValidExports(raw, exports$1) {
|
||||
if (exports$1 === null || typeof exports$1 !== "object" || Array.isArray(exports$1)) {
|
||||
throw new TypeError(`[vitest] vi.mock("${raw}", factory?: () => unknown) is not returning an object. Did you mean to return an object with a "default" key?`);
|
||||
}
|
||||
}
|
||||
|
||||
export { AutomockedModule as A, MockerRegistry as M, RedirectedModule as R, ManualMockedModule as a, AutospiedModule as b };
|
||||
|
||||
Reference in New Issue
Block a user