This commit is contained in:
2026-08-14 16:03:47 +01:00
parent fd53e3f4fb
commit dfdc3b2840
2463 changed files with 186456 additions and 111315 deletions
+72 -22
View File
@@ -8,7 +8,6 @@ const MOCK_RESTORE = new Set();
const REGISTERED_MOCKS = new Set();
const MOCK_CONFIGS = new WeakMap();
function createMockInstance(options = {}) {
var _ref;
const { originalImplementation, restore, mockImplementation, resetToMockImplementation, resetToMockName } = options;
if (restore) {
MOCK_RESTORE.add(restore);
@@ -20,7 +19,7 @@ function createMockInstance(options = {}) {
state,
...options
});
const mockLength = ((_ref = mockImplementation || originalImplementation) === null || _ref === void 0 ? void 0 : _ref.length) ?? 0;
const mockLength = (mockImplementation || originalImplementation)?.length ?? 0;
Object.defineProperty(mock, "length", {
writable: true,
enumerable: false,
@@ -65,7 +64,7 @@ function createMockInstance(options = {}) {
config.mockImplementation = implementation;
config.onceMockImplementations = [];
const returnValue = callback();
if (typeof returnValue === "object" && typeof (returnValue === null || returnValue === void 0 ? void 0 : returnValue.then) === "function") {
if (typeof returnValue === "object" && typeof returnValue?.then === "function") {
return returnValue.then(() => {
reset();
return mock;
@@ -81,22 +80,64 @@ function createMockInstance(options = {}) {
});
};
mock.mockReturnValue = function mockReturnValue(value) {
return mock.mockImplementation(() => value);
return mock.mockImplementation(function() {
if (new.target) {
throwConstructorError("mockReturnValue");
}
return value;
});
};
mock.mockReturnValueOnce = function mockReturnValueOnce(value) {
return mock.mockImplementationOnce(() => value);
return mock.mockImplementationOnce(function() {
if (new.target) {
throwConstructorError("mockReturnValueOnce");
}
return value;
});
};
mock.mockThrow = function mockThrow(value) {
// eslint-disable-next-line prefer-arrow-callback
return mock.mockImplementation(function() {
throw value;
});
};
mock.mockThrowOnce = function mockThrowOnce(value) {
// eslint-disable-next-line prefer-arrow-callback
return mock.mockImplementationOnce(function() {
throw value;
});
};
mock.mockResolvedValue = function mockResolvedValue(value) {
return mock.mockImplementation(() => Promise.resolve(value));
return mock.mockImplementation(function() {
if (new.target) {
throwConstructorError("mockResolvedValue");
}
return Promise.resolve(value);
});
};
mock.mockResolvedValueOnce = function mockResolvedValueOnce(value) {
return mock.mockImplementationOnce(() => Promise.resolve(value));
return mock.mockImplementationOnce(function() {
if (new.target) {
throwConstructorError("mockResolvedValueOnce");
}
return Promise.resolve(value);
});
};
mock.mockRejectedValue = function mockRejectedValue(value) {
return mock.mockImplementation(() => Promise.reject(value));
return mock.mockImplementation(function() {
if (new.target) {
throwConstructorError("mockRejectedValue");
}
return Promise.reject(value);
});
};
mock.mockRejectedValueOnce = function mockRejectedValueOnce(value) {
return mock.mockImplementationOnce(() => Promise.reject(value));
return mock.mockImplementationOnce(function() {
if (new.target) {
throwConstructorError("mockRejectedValueOnce");
}
return Promise.reject(value);
});
};
mock.mockClear = function mockClear() {
state.calls = [];
@@ -116,7 +157,7 @@ function createMockInstance(options = {}) {
};
mock.mockRestore = function mockRestore() {
mock.mockReset();
return restore === null || restore === void 0 ? void 0 : restore();
return restore?.();
};
mock.mockName = function mockName(name) {
if (typeof name === "string") {
@@ -137,7 +178,7 @@ function createMockInstance(options = {}) {
}
function fn(originalImplementation) {
// if the function is already a mock, just return the same function,
// simillarly to how vi.spyOn() works
// similarly to how vi.spyOn() works
if (originalImplementation != null && isMockFunction(originalImplementation)) {
return originalImplementation;
}
@@ -161,6 +202,12 @@ function spyOn(object, key, accessor) {
let original;
if (originalDescriptor) {
original = originalDescriptor[accessType];
// weird Proxy edge case where descriptor's value is undefined,
// but there's still a value on the object when called
// https://github.com/vitest-dev/vitest/issues/9439
if (original == null && accessType === "value") {
original = object[key];
}
} else if (accessType !== "value") {
original = () => object[key];
} else {
@@ -236,7 +283,7 @@ let invocationCallCounter = 1;
function createMock({ state, config, name: mockName, prototypeState, prototypeConfig, keepMembersImplementation, mockImplementation, prototypeMembers = [] }) {
const original = config.mockOriginal;
const pseudoOriginal = mockImplementation;
const name = mockName || (original === null || original === void 0 ? void 0 : original.name) || "Mock";
const name = mockName || original?.name || "Mock";
const namedObject = { [name]: (function(...args) {
registerCalls(args, state, prototypeState);
registerInvocationOrder(invocationCallCounter++, state, prototypeState);
@@ -253,7 +300,7 @@ function createMock({ state, config, name: mockName, prototypeState, prototypeCo
const context = new.target ? undefined : this;
const [instanceIndex, instancePrototypeIndex] = registerInstance(context, state, prototypeState);
const [contextIndex, contextPrototypeIndex] = registerContext(context, state, prototypeState);
const implementation = config.onceMockImplementations.shift() || config.mockImplementation || (prototypeConfig === null || prototypeConfig === void 0 ? void 0 : prototypeConfig.onceMockImplementations.shift()) || (prototypeConfig === null || prototypeConfig === void 0 ? void 0 : prototypeConfig.mockImplementation) || original || function() {};
const implementation = config.onceMockImplementations.shift() || config.mockImplementation || prototypeConfig?.onceMockImplementations.shift() || prototypeConfig?.mockImplementation || original || function() {};
let returnValue;
let thrownValue;
let didThrow = false;
@@ -262,11 +309,11 @@ function createMock({ state, config, name: mockName, prototypeState, prototypeCo
returnValue = Reflect.construct(implementation, args, new.target);
// jest calls this before the implementation, but we have to resolve this _after_
// because we cannot do it before the `Reflect.construct` called the custom implementation.
// fortunetly, the constructor is always an empty functon because `prototypeMethods`
// fortunately, the constructor is always an empty function because `prototypeMethods`
// are only used by the automocker, so this doesn't matter
for (const prop of prototypeMembers) {
const prototypeMock = returnValue[prop];
// the method was overidden because of inheritence, ignore it
// the method was overridden because of inheritance, ignore it
// eslint-disable-next-line ts/no-use-before-define
if (prototypeMock !== mock.prototype[prop]) {
continue;
@@ -275,7 +322,7 @@ function createMock({ state, config, name: mockName, prototypeState, prototypeCo
const prototypeState = isMock ? prototypeMock.mock : undefined;
const prototypeConfig = isMock ? MOCK_CONFIGS.get(prototypeMock) : undefined;
returnValue[prop] = createMockInstance({
originalImplementation: keepMembersImplementation ? prototypeConfig === null || prototypeConfig === void 0 ? void 0 : prototypeConfig.mockOriginal : undefined,
originalImplementation: keepMembersImplementation ? prototypeConfig?.mockOriginal : undefined,
prototypeState,
prototypeConfig,
keepMembersImplementation
@@ -335,28 +382,28 @@ function createMock({ state, config, name: mockName, prototypeState, prototypeCo
}
function registerCalls(args, state, prototypeState) {
state.calls.push(args);
prototypeState === null || prototypeState === void 0 ? void 0 : prototypeState.calls.push(args);
prototypeState?.calls.push(args);
}
function registerInvocationOrder(order, state, prototypeState) {
state.invocationCallOrder.push(order);
prototypeState === null || prototypeState === void 0 ? void 0 : prototypeState.invocationCallOrder.push(order);
prototypeState?.invocationCallOrder.push(order);
}
function registerResult(result, state, prototypeState) {
state.results.push(result);
prototypeState === null || prototypeState === void 0 ? void 0 : prototypeState.results.push(result);
prototypeState?.results.push(result);
}
function registerSettledResult(result, state, prototypeState) {
state.settledResults.push(result);
prototypeState === null || prototypeState === void 0 ? void 0 : prototypeState.settledResults.push(result);
prototypeState?.settledResults.push(result);
}
function registerInstance(instance, state, prototypeState) {
const instanceIndex = state.instances.push(instance);
const instancePrototypeIndex = prototypeState === null || prototypeState === void 0 ? void 0 : prototypeState.instances.push(instance);
const instancePrototypeIndex = prototypeState?.instances.push(instance);
return [instanceIndex, instancePrototypeIndex];
}
function registerContext(context, state, prototypeState) {
const contextIndex = state.contexts.push(context);
const contextPrototypeIndex = prototypeState === null || prototypeState === void 0 ? void 0 : prototypeState.contexts.push(context);
const contextPrototypeIndex = prototypeState?.contexts.push(context);
return [contextIndex, contextPrototypeIndex];
}
function copyOriginalStaticProperties(mock, original) {
@@ -429,5 +476,8 @@ function clearAllMocks() {
function resetAllMocks() {
REGISTERED_MOCKS.forEach((mock) => mock.mockReset());
}
function throwConstructorError(shorthand) {
throw new TypeError(`Cannot use \`${shorthand}\` when called with \`new\`. Use \`mockImplementation\` with a \`class\` keyword instead. See https://vitest.dev/api/mock#class-support for more information.`);
}
export { clearAllMocks, createMockInstance, fn, isMockFunction, resetAllMocks, restoreAllMocks, spyOn };