Updating NPM dependencies
This commit is contained in:
194
node_modules/jest-mock/build/index.js
generated
vendored
194
node_modules/jest-mock/build/index.js
generated
vendored
@@ -21,6 +21,9 @@ function _defineProperty(obj, key, value) {
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
/* eslint-disable local/ban-types-eventually, local/prefer-rest-params-eventually */
|
||||
// | Window – add once TS improves typings;
|
||||
|
||||
/**
|
||||
* Possible types of a MockFunctionResult.
|
||||
* 'return': The call completed by returning normally.
|
||||
@@ -96,89 +99,70 @@ function matchArity(fn, length) {
|
||||
|
||||
switch (length) {
|
||||
case 1:
|
||||
mockConstructor = function mockConstructor(_a) {
|
||||
mockConstructor = function (_a) {
|
||||
return fn.apply(this, arguments);
|
||||
};
|
||||
|
||||
break;
|
||||
|
||||
case 2:
|
||||
mockConstructor = function mockConstructor(_a, _b) {
|
||||
mockConstructor = function (_a, _b) {
|
||||
return fn.apply(this, arguments);
|
||||
};
|
||||
|
||||
break;
|
||||
|
||||
case 3:
|
||||
mockConstructor = function mockConstructor(_a, _b, _c) {
|
||||
mockConstructor = function (_a, _b, _c) {
|
||||
return fn.apply(this, arguments);
|
||||
};
|
||||
|
||||
break;
|
||||
|
||||
case 4:
|
||||
mockConstructor = function mockConstructor(_a, _b, _c, _d) {
|
||||
mockConstructor = function (_a, _b, _c, _d) {
|
||||
return fn.apply(this, arguments);
|
||||
};
|
||||
|
||||
break;
|
||||
|
||||
case 5:
|
||||
mockConstructor = function mockConstructor(_a, _b, _c, _d, _e) {
|
||||
mockConstructor = function (_a, _b, _c, _d, _e) {
|
||||
return fn.apply(this, arguments);
|
||||
};
|
||||
|
||||
break;
|
||||
|
||||
case 6:
|
||||
mockConstructor = function mockConstructor(_a, _b, _c, _d, _e, _f) {
|
||||
mockConstructor = function (_a, _b, _c, _d, _e, _f) {
|
||||
return fn.apply(this, arguments);
|
||||
};
|
||||
|
||||
break;
|
||||
|
||||
case 7:
|
||||
mockConstructor = function mockConstructor(_a, _b, _c, _d, _e, _f, _g) {
|
||||
mockConstructor = function (_a, _b, _c, _d, _e, _f, _g) {
|
||||
return fn.apply(this, arguments);
|
||||
};
|
||||
|
||||
break;
|
||||
|
||||
case 8:
|
||||
mockConstructor = function mockConstructor(
|
||||
_a,
|
||||
_b,
|
||||
_c,
|
||||
_d,
|
||||
_e,
|
||||
_f,
|
||||
_g,
|
||||
_h
|
||||
) {
|
||||
mockConstructor = function (_a, _b, _c, _d, _e, _f, _g, _h) {
|
||||
return fn.apply(this, arguments);
|
||||
};
|
||||
|
||||
break;
|
||||
|
||||
case 9:
|
||||
mockConstructor = function mockConstructor(
|
||||
_a,
|
||||
_b,
|
||||
_c,
|
||||
_d,
|
||||
_e,
|
||||
_f,
|
||||
_g,
|
||||
_h,
|
||||
_i
|
||||
) {
|
||||
mockConstructor = function (_a, _b, _c, _d, _e, _f, _g, _h, _i) {
|
||||
return fn.apply(this, arguments);
|
||||
};
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
mockConstructor = function mockConstructor() {
|
||||
mockConstructor = function () {
|
||||
return fn.apply(this, arguments);
|
||||
};
|
||||
|
||||
@@ -315,7 +299,7 @@ class ModuleMockerClass {
|
||||
const prop = ownNames[i];
|
||||
|
||||
if (!isReadonlyProp(object, prop)) {
|
||||
const propDesc = Object.getOwnPropertyDescriptor(object, prop); // @ts-ignore Object.__esModule
|
||||
const propDesc = Object.getOwnPropertyDescriptor(object, prop);
|
||||
|
||||
if ((propDesc !== undefined && !propDesc.get) || object.__esModule) {
|
||||
slots.add(prop);
|
||||
@@ -355,8 +339,6 @@ class ModuleMockerClass {
|
||||
|
||||
_defaultMockConfig() {
|
||||
return {
|
||||
defaultReturnValue: undefined,
|
||||
isReturnValueLastSet: false,
|
||||
mockImpl: undefined,
|
||||
mockName: 'jest.fn()',
|
||||
specificMockImpls: [],
|
||||
@@ -397,7 +379,7 @@ class ModuleMockerClass {
|
||||
const prototypeSlots = this._getSlots(prototype);
|
||||
|
||||
const mocker = this;
|
||||
const mockConstructor = matchArity(function(...args) {
|
||||
const mockConstructor = matchArity(function (...args) {
|
||||
const mockState = mocker._ensureMockState(f);
|
||||
|
||||
const mockConfig = mocker._ensureMockConfig(f);
|
||||
@@ -436,10 +418,10 @@ class ModuleMockerClass {
|
||||
// it easier to interact with mock instance call and
|
||||
// return values
|
||||
if (prototype[slot].type === 'function') {
|
||||
// @ts-ignore no index signature
|
||||
const protoImpl = this[slot]; // @ts-ignore no index signature
|
||||
// @ts-expect-error no index signature
|
||||
const protoImpl = this[slot]; // @ts-expect-error no index signature
|
||||
|
||||
this[slot] = mocker.generateFromMetadata(prototype[slot]); // @ts-ignore no index signature
|
||||
this[slot] = mocker.generateFromMetadata(prototype[slot]); // @ts-expect-error no index signature
|
||||
|
||||
this[slot]._protoImpl = protoImpl;
|
||||
}
|
||||
@@ -449,43 +431,24 @@ class ModuleMockerClass {
|
||||
? mockConfig.specificMockImpls.shift()
|
||||
: mockConfig.mockImpl;
|
||||
return mockImpl && mockImpl.apply(this, arguments);
|
||||
}
|
||||
|
||||
const returnValue = mockConfig.defaultReturnValue; // If return value is last set, either specific or default, i.e.
|
||||
// mockReturnValueOnce()/mockReturnValue() is called and no
|
||||
// mockImplementationOnce()/mockImplementation() is called after
|
||||
// that.
|
||||
// use the set return value.
|
||||
|
||||
if (mockConfig.specificReturnValues.length) {
|
||||
return mockConfig.specificReturnValues.shift();
|
||||
}
|
||||
|
||||
if (mockConfig.isReturnValueLastSet) {
|
||||
return mockConfig.defaultReturnValue;
|
||||
} // If mockImplementationOnce()/mockImplementation() is last set,
|
||||
// or specific return values are used up, use the mock
|
||||
// implementation.
|
||||
// implementation use the mock
|
||||
|
||||
let specificMockImpl;
|
||||
let specificMockImpl = mockConfig.specificMockImpls.shift();
|
||||
|
||||
if (returnValue === undefined) {
|
||||
specificMockImpl = mockConfig.specificMockImpls.shift();
|
||||
if (specificMockImpl === undefined) {
|
||||
specificMockImpl = mockConfig.mockImpl;
|
||||
}
|
||||
|
||||
if (specificMockImpl === undefined) {
|
||||
specificMockImpl = mockConfig.mockImpl;
|
||||
}
|
||||
|
||||
if (specificMockImpl) {
|
||||
return specificMockImpl.apply(this, arguments);
|
||||
}
|
||||
if (specificMockImpl) {
|
||||
return specificMockImpl.apply(this, arguments);
|
||||
} // Otherwise use prototype implementation
|
||||
|
||||
if (returnValue === undefined && f._protoImpl) {
|
||||
if (f._protoImpl) {
|
||||
return f._protoImpl.apply(this, arguments);
|
||||
}
|
||||
|
||||
return returnValue;
|
||||
return undefined;
|
||||
})();
|
||||
} catch (error) {
|
||||
// Store the thrown error so we can record it, then re-throw it.
|
||||
@@ -544,13 +507,9 @@ class ModuleMockerClass {
|
||||
return restore ? restore() : undefined;
|
||||
};
|
||||
|
||||
f.mockReturnValueOnce = value => {
|
||||
// next function call will return this value or default return value
|
||||
const mockConfig = this._ensureMockConfig(f);
|
||||
|
||||
mockConfig.specificReturnValues.push(value);
|
||||
return f;
|
||||
};
|
||||
f.mockReturnValueOnce = (
|
||||
value // next function call will return this value or default return value
|
||||
) => f.mockImplementationOnce(() => value);
|
||||
|
||||
f.mockResolvedValueOnce = value =>
|
||||
f.mockImplementationOnce(() => Promise.resolve(value));
|
||||
@@ -558,14 +517,9 @@ class ModuleMockerClass {
|
||||
f.mockRejectedValueOnce = value =>
|
||||
f.mockImplementationOnce(() => Promise.reject(value));
|
||||
|
||||
f.mockReturnValue = value => {
|
||||
// next function call will return specified return value or this one
|
||||
const mockConfig = this._ensureMockConfig(f);
|
||||
|
||||
mockConfig.isReturnValueLastSet = true;
|
||||
mockConfig.defaultReturnValue = value;
|
||||
return f;
|
||||
};
|
||||
f.mockReturnValue = (
|
||||
value // next function call will return specified return value or this one
|
||||
) => f.mockImplementation(() => value);
|
||||
|
||||
f.mockResolvedValue = value =>
|
||||
f.mockImplementation(() => Promise.resolve(value));
|
||||
@@ -578,7 +532,6 @@ class ModuleMockerClass {
|
||||
// or default mock implementation return value
|
||||
const mockConfig = this._ensureMockConfig(f);
|
||||
|
||||
mockConfig.isReturnValueLastSet = false;
|
||||
mockConfig.specificMockImpls.push(fn);
|
||||
return f;
|
||||
};
|
||||
@@ -587,14 +540,12 @@ class ModuleMockerClass {
|
||||
// next function call will use mock implementation return value
|
||||
const mockConfig = this._ensureMockConfig(f);
|
||||
|
||||
mockConfig.isReturnValueLastSet = false;
|
||||
mockConfig.defaultReturnValue = undefined;
|
||||
mockConfig.mockImpl = fn;
|
||||
return f;
|
||||
};
|
||||
|
||||
f.mockReturnThis = () =>
|
||||
f.mockImplementation(function() {
|
||||
f.mockImplementation(function () {
|
||||
return this;
|
||||
});
|
||||
|
||||
@@ -681,7 +632,7 @@ class ModuleMockerClass {
|
||||
_generateMock(metadata, callbacks, refs) {
|
||||
// metadata not compatible but it's the same type, maybe problem with
|
||||
// overloading of _makeComponent and not _generateMock?
|
||||
// @ts-ignore
|
||||
// @ts-expect-error
|
||||
const mock = this._makeComponent(metadata);
|
||||
|
||||
if (metadata.refID != null) {
|
||||
@@ -693,7 +644,7 @@ class ModuleMockerClass {
|
||||
|
||||
if (slotMetadata.ref != null) {
|
||||
callbacks.push(
|
||||
(function(ref) {
|
||||
(function (ref) {
|
||||
return () => (mock[slot] = refs[ref]);
|
||||
})(slotMetadata.ref)
|
||||
);
|
||||
@@ -762,11 +713,11 @@ class ModuleMockerClass {
|
||||
metadata.value = component;
|
||||
return metadata;
|
||||
} else if (type === 'function') {
|
||||
// @ts-ignore this is a function so it has a name
|
||||
metadata.name = component.name; // @ts-ignore may be a mock
|
||||
// @ts-expect-error this is a function so it has a name
|
||||
metadata.name = component.name; // @ts-expect-error may be a mock
|
||||
|
||||
if (component._isMockFunction === true) {
|
||||
// @ts-ignore may be a mock
|
||||
// @ts-expect-error may be a mock
|
||||
metadata.mockImpl = component.getMockImplementation();
|
||||
}
|
||||
}
|
||||
@@ -778,12 +729,12 @@ class ModuleMockerClass {
|
||||
if (type !== 'array') {
|
||||
this._getSlots(component).forEach(slot => {
|
||||
if (
|
||||
type === 'function' && // @ts-ignore may be a mock
|
||||
type === 'function' && // @ts-expect-error may be a mock
|
||||
component._isMockFunction === true &&
|
||||
slot.match(/^mock/)
|
||||
) {
|
||||
return;
|
||||
} // @ts-ignore no index signature
|
||||
} // @ts-expect-error no index signature
|
||||
|
||||
const slotMetadata = this.getMetadata(component[slot], refs);
|
||||
|
||||
@@ -847,22 +798,50 @@ class ModuleMockerClass {
|
||||
);
|
||||
}
|
||||
|
||||
const isMethodOwner = object.hasOwnProperty(methodName); // @ts-ignore overriding original method with a Mock
|
||||
const isMethodOwner = object.hasOwnProperty(methodName);
|
||||
let descriptor = Object.getOwnPropertyDescriptor(object, methodName);
|
||||
let proto = Object.getPrototypeOf(object);
|
||||
|
||||
object[methodName] = this._makeComponent(
|
||||
{
|
||||
type: 'function'
|
||||
},
|
||||
() => {
|
||||
if (isMethodOwner) {
|
||||
object[methodName] = original;
|
||||
} else {
|
||||
delete object[methodName];
|
||||
while (!descriptor && proto !== null) {
|
||||
descriptor = Object.getOwnPropertyDescriptor(proto, methodName);
|
||||
proto = Object.getPrototypeOf(proto);
|
||||
}
|
||||
|
||||
let mock;
|
||||
|
||||
if (descriptor && descriptor.get) {
|
||||
const originalGet = descriptor.get;
|
||||
mock = this._makeComponent(
|
||||
{
|
||||
type: 'function'
|
||||
},
|
||||
() => {
|
||||
descriptor.get = originalGet;
|
||||
Object.defineProperty(object, methodName, descriptor);
|
||||
}
|
||||
}
|
||||
); // @ts-ignore original method is now a Mock
|
||||
);
|
||||
|
||||
object[methodName].mockImplementation(function() {
|
||||
descriptor.get = () => mock;
|
||||
|
||||
Object.defineProperty(object, methodName, descriptor);
|
||||
} else {
|
||||
mock = this._makeComponent(
|
||||
{
|
||||
type: 'function'
|
||||
},
|
||||
() => {
|
||||
if (isMethodOwner) {
|
||||
object[methodName] = original;
|
||||
} else {
|
||||
delete object[methodName];
|
||||
}
|
||||
}
|
||||
); // @ts-expect-error overriding original method with a Mock
|
||||
|
||||
object[methodName] = mock;
|
||||
}
|
||||
|
||||
mock.mockImplementation(function () {
|
||||
return original.apply(this, arguments);
|
||||
});
|
||||
}
|
||||
@@ -920,20 +899,20 @@ class ModuleMockerClass {
|
||||
this._typeOf(original) +
|
||||
' given instead'
|
||||
);
|
||||
} // @ts-ignore: mock is assignable
|
||||
} // @ts-expect-error: mock is assignable
|
||||
|
||||
descriptor[accessType] = this._makeComponent(
|
||||
{
|
||||
type: 'function'
|
||||
},
|
||||
() => {
|
||||
// @ts-ignore: mock is assignable
|
||||
// @ts-expect-error: mock is assignable
|
||||
descriptor[accessType] = original;
|
||||
Object.defineProperty(obj, propertyName, descriptor);
|
||||
}
|
||||
);
|
||||
descriptor[accessType].mockImplementation(function() {
|
||||
// @ts-ignore
|
||||
descriptor[accessType].mockImplementation(function () {
|
||||
// @ts-expect-error
|
||||
return original.apply(this, arguments);
|
||||
});
|
||||
}
|
||||
@@ -961,7 +940,6 @@ class ModuleMockerClass {
|
||||
return value == null ? '' + value : typeof value;
|
||||
}
|
||||
}
|
||||
/* eslint-disable-next-line no-redeclare */
|
||||
|
||||
const JestMock = new ModuleMockerClass(global);
|
||||
module.exports = JestMock;
|
||||
|
||||
Reference in New Issue
Block a user