jest-mock
Advanced tools
Comparing version 14.0.0 to 14.2.0-alpha.ca8bfb6e
@@ -25,49 +25,49 @@ /** | ||
const RESERVED_KEYWORDS = [ | ||
'do', | ||
'if', | ||
'in', | ||
'for', | ||
'let', | ||
'new', | ||
'try', | ||
'var', | ||
'case', | ||
'else', | ||
'enum', | ||
'eval', | ||
'null', | ||
'this', | ||
'true', | ||
'void', | ||
'with', | ||
'await', | ||
'break', | ||
'catch', | ||
'class', | ||
'const', | ||
'false', | ||
'super', | ||
'throw', | ||
'while', | ||
'yield', | ||
'delete', | ||
'export', | ||
'import', | ||
'public', | ||
'return', | ||
'static', | ||
'switch', | ||
'typeof', | ||
'default', | ||
'extends', | ||
'finally', | ||
'package', | ||
'private', | ||
'continue', | ||
'debugger', | ||
'function', | ||
'arguments', | ||
'interface', | ||
'protected', | ||
'implements', | ||
'do', | ||
'if', | ||
'in', | ||
'for', | ||
'let', | ||
'new', | ||
'try', | ||
'var', | ||
'case', | ||
'else', | ||
'enum', | ||
'eval', | ||
'null', | ||
'this', | ||
'true', | ||
'void', | ||
'with', | ||
'await', | ||
'break', | ||
'catch', | ||
'class', | ||
'const', | ||
'false', | ||
'super', | ||
'throw', | ||
'while', | ||
'yield', | ||
'delete', | ||
'export', | ||
'import', | ||
'public', | ||
'return', | ||
'static', | ||
'switch', | ||
'typeof', | ||
'default', | ||
'extends', | ||
'finally', | ||
'package', | ||
'private', | ||
'continue', | ||
'debugger', | ||
'function', | ||
'arguments', | ||
'interface', | ||
'protected', | ||
'implements', | ||
'instanceof']; | ||
@@ -77,27 +77,27 @@ | ||
function isA(typeName, value) { | ||
return Object.prototype.toString.apply(value) === '[object ' + typeName + ']';} | ||
return Object.prototype.toString.apply(value) === '[object ' + typeName + ']'; | ||
} | ||
function getType(ref) { | ||
if (isA('Function', ref)) { | ||
return 'function';} else | ||
if (Array.isArray(ref)) { | ||
return 'array';} else | ||
if (isA('Object', ref)) { | ||
return 'object';} else | ||
if (isA('Number', ref) || isA('String', ref) || isA('Boolean', ref)) { | ||
return 'constant';} else | ||
if (isA('Map', ref) || isA('WeakMap', ref) || isA('Set', ref)) { | ||
return 'collection';} else | ||
if (isA('RegExp', ref)) { | ||
return 'regexp';} else | ||
if (ref === undefined) { | ||
return 'undefined';} else | ||
if (ref === null) { | ||
return 'null';} else | ||
{ | ||
return null;}} | ||
return 'function'; | ||
} else if (Array.isArray(ref)) { | ||
return 'array'; | ||
} else if (isA('Object', ref)) { | ||
return 'object'; | ||
} else if (isA('Number', ref) || isA('String', ref) || isA('Boolean', ref)) { | ||
return 'constant'; | ||
} else if (isA('Map', ref) || isA('WeakMap', ref) || isA('Set', ref)) { | ||
return 'collection'; | ||
} else if (isA('RegExp', ref)) { | ||
return 'regexp'; | ||
} else if (ref === undefined) { | ||
return 'undefined'; | ||
} else if (ref === null) { | ||
return 'null'; | ||
} else { | ||
return null; | ||
} | ||
} | ||
function isReadonlyProp(object, prop) { | ||
@@ -107,33 +107,33 @@ return ( | ||
( | ||
prop === 'arguments' || | ||
prop === 'caller' || | ||
prop === 'callee' || | ||
prop === 'name' || | ||
prop === 'length') && | ||
prop === 'arguments' || | ||
prop === 'caller' || | ||
prop === 'callee' || | ||
prop === 'name' || | ||
prop === 'length') && | ||
isA('Function', object) || | ||
isA('Function', object) || | ||
( | ||
prop === 'source' || | ||
prop === 'global' || | ||
prop === 'ignoreCase' || | ||
prop === 'multiline') && | ||
prop === 'source' || | ||
prop === 'global' || | ||
prop === 'ignoreCase' || | ||
prop === 'multiline') && | ||
isA('RegExp', object));} | ||
isA('RegExp', object)); | ||
} | ||
function getSlots(object) { | ||
const slots = {}; | ||
if (!object) { | ||
return [];} | ||
return []; | ||
} | ||
let parent = Object.getPrototypeOf(object); | ||
do { | ||
if (object === Object.getPrototypeOf(Function)) { | ||
break;} | ||
break; | ||
} | ||
const ownNames = Object.getOwnPropertyNames(object); | ||
@@ -145,20 +145,20 @@ for (let i = 0; i < ownNames.length; i++) { | ||
if (!propDesc.get) { | ||
slots[prop] = true;}}} | ||
slots[prop] = true; | ||
} | ||
} | ||
} | ||
object = parent; | ||
} while (object && (parent = Object.getPrototypeOf(object)) !== null); | ||
return Object.keys(slots); | ||
} | ||
object = parent;} while ( | ||
object && (parent = Object.getPrototypeOf(object)) !== null); | ||
return Object.keys(slots);} | ||
function createMockFunction( | ||
metadata, | ||
mockConstructor) | ||
metadata, | ||
mockConstructor) | ||
{ | ||
let name = metadata.name; | ||
if (!name) { | ||
return mockConstructor;} | ||
return mockConstructor; | ||
} | ||
// Preserve `name` property of mocked function. | ||
@@ -172,37 +172,37 @@ const boundFunctionPrefix = 'bound '; | ||
// Call bind() just to alter the function name. | ||
bindCall = '.bind(null)';} while ( | ||
name && name.startsWith(boundFunctionPrefix));} | ||
bindCall = '.bind(null)'; | ||
} while (name && name.startsWith(boundFunctionPrefix)); | ||
} | ||
// It's a syntax error to define functions with a reserved keyword | ||
// as name. | ||
if (RESERVED_KEYWORDS.indexOf(name) !== -1) { | ||
name = '$' + name;} | ||
name = '$' + name; | ||
} | ||
/* eslint-disable no-new-func */ | ||
return new Function( | ||
'mockConstructor', | ||
'return function ' + name + '() {' + | ||
'return mockConstructor.apply(this,arguments);' + | ||
'mockConstructor', | ||
'return function ' + name + '() {' + | ||
'return mockConstructor.apply(this,arguments);' + | ||
'}' + bindCall)( | ||
mockConstructor); | ||
/* eslint-enable no-new-func */} | ||
/* eslint-enable no-new-func */ | ||
} | ||
function makeComponent(metadata) { | ||
if (metadata.type === 'object') { | ||
return {};} else | ||
if (metadata.type === 'array') { | ||
return [];} else | ||
if (metadata.type === 'regexp') { | ||
return new RegExp('');} else | ||
if ( | ||
metadata.type === 'constant' || | ||
metadata.type === 'collection' || | ||
metadata.type === 'null' || | ||
metadata.type === 'undefined') | ||
return {}; | ||
} else if (metadata.type === 'array') { | ||
return []; | ||
} else if (metadata.type === 'regexp') { | ||
return new RegExp(''); | ||
} else if ( | ||
metadata.type === 'constant' || | ||
metadata.type === 'collection' || | ||
metadata.type === 'null' || | ||
metadata.type === 'undefined') | ||
{ | ||
return metadata.value;} else | ||
if (metadata.type === 'function') { | ||
return metadata.value; | ||
} else if (metadata.type === 'function') { | ||
let isReturnValueLastSet = false; | ||
@@ -218,6 +218,6 @@ let defaultReturnValue; | ||
const instances = []; | ||
const prototype = | ||
metadata.members && | ||
metadata.members.prototype && | ||
metadata.members.prototype.members || | ||
const prototype = | ||
metadata.members && | ||
metadata.members.prototype && | ||
metadata.members.prototype.members || | ||
{}; | ||
@@ -237,10 +237,10 @@ const prototypeSlots = getSlots(prototype); | ||
this[slot] = generateFromMetadata(prototype[slot]); | ||
this[slot]._protoImpl = protoImpl;}}); | ||
this[slot]._protoImpl = protoImpl; | ||
} | ||
}); | ||
// Run the mock constructor implementation | ||
return mockImpl && mockImpl.apply(this, arguments);} | ||
return mockImpl && mockImpl.apply(this, arguments); | ||
} | ||
let returnValue; | ||
@@ -254,6 +254,6 @@ // If return value is last set, either specific or default, i.e. | ||
if (returnValue === undefined) { | ||
returnValue = defaultReturnValue;}} | ||
returnValue = defaultReturnValue; | ||
} | ||
} | ||
// If mockImplementationOnce()/mockImplementation() is last set, | ||
@@ -265,17 +265,17 @@ // or specific return values are used up, use the mock implementation. | ||
if (specificMockImpl === undefined) { | ||
specificMockImpl = mockImpl;} | ||
specificMockImpl = mockImpl; | ||
} | ||
if (specificMockImpl) { | ||
return specificMockImpl.apply(this, arguments);}} | ||
return specificMockImpl.apply(this, arguments); | ||
} | ||
} | ||
// Otherwise use prototype implementation | ||
if (returnValue === undefined && f._protoImpl) { | ||
return f._protoImpl.apply(this, arguments);} | ||
return f._protoImpl.apply(this, arguments); | ||
} | ||
return returnValue; | ||
}; | ||
return returnValue;}; | ||
f = createMockFunction(metadata, mockConstructor); | ||
@@ -288,5 +288,5 @@ f._isMockFunction = true; | ||
calls.length = 0; | ||
instances.length = 0;}; | ||
instances.length = 0; | ||
}; | ||
f.mockReturnValueOnce = value => { | ||
@@ -296,5 +296,5 @@ // next function call will return this value or default return value | ||
specificReturnValues.push(value); | ||
return f;}; | ||
return f; | ||
}; | ||
f.mockReturnValue = value => { | ||
@@ -304,5 +304,5 @@ // next function call will return specified return value or this one | ||
defaultReturnValue = value; | ||
return f;}; | ||
return f; | ||
}; | ||
f.mockImplementationOnce = fn => { | ||
@@ -313,5 +313,5 @@ // next function call will use this mock implementation return value | ||
specificMockImpls.push(fn); | ||
return f;}; | ||
return f; | ||
}; | ||
f.mockImplementation = f.mockImpl = fn => { | ||
@@ -321,51 +321,51 @@ // next function call will use mock implementation return value | ||
mockImpl = fn; | ||
return f;}; | ||
return f; | ||
}; | ||
f.mockReturnThis = () => | ||
f.mockReturnThis = () => | ||
f.mockImplementation(function () { | ||
return this;}); | ||
return this; | ||
}); | ||
if (metadata.mockImpl) { | ||
f.mockImplementation(metadata.mockImpl);} | ||
f.mockImplementation(metadata.mockImpl); | ||
} | ||
return f;} else | ||
{ | ||
return f; | ||
} else { | ||
const unknownType = metadata.type || 'undefined type'; | ||
throw new Error('Unrecognized type ' + unknownType);}} | ||
throw new Error('Unrecognized type ' + unknownType); | ||
} | ||
} | ||
function generateMock( | ||
metadata, | ||
callbacks, | ||
refs) | ||
metadata, | ||
callbacks, | ||
refs) | ||
{ | ||
const mock = makeComponent(metadata); | ||
if (metadata.refID != null) { | ||
refs[metadata.refID] = mock;} | ||
refs[metadata.refID] = mock; | ||
} | ||
getSlots(metadata.members).forEach(slot => { | ||
const slotMetadata = metadata.members && metadata.members[slot] || {}; | ||
if (slotMetadata.ref != null) { | ||
callbacks.push(() => mock[slot] = refs[slotMetadata.ref]);} else | ||
{ | ||
mock[slot] = generateMock(slotMetadata, callbacks, refs);}}); | ||
callbacks.push(() => mock[slot] = refs[slotMetadata.ref]); | ||
} else { | ||
mock[slot] = generateMock(slotMetadata, callbacks, refs); | ||
} | ||
}); | ||
if ( | ||
metadata.type !== 'undefined' && | ||
metadata.type !== 'null' && | ||
mock.prototype) | ||
metadata.type !== 'undefined' && | ||
metadata.type !== 'null' && | ||
mock.prototype) | ||
{ | ||
mock.prototype.constructor = mock;} | ||
mock.prototype.constructor = mock; | ||
} | ||
return mock; | ||
} | ||
return mock;} | ||
function generateFromMetadata(_metadata) { | ||
@@ -376,8 +376,8 @@ const callbacks = []; | ||
callbacks.forEach(setter => setter()); | ||
return mock;} | ||
return mock; | ||
} | ||
function getMetadata( | ||
component, | ||
_refs) | ||
component, | ||
_refs) | ||
{ | ||
@@ -387,26 +387,26 @@ const refs = _refs || new Map(); | ||
if (ref != null) { | ||
return { ref };} | ||
return { ref }; | ||
} | ||
const type = getType(component); | ||
if (!type) { | ||
return null;} | ||
return null; | ||
} | ||
const metadata = { type }; | ||
if ( | ||
type === 'constant' || | ||
type === 'collection' || | ||
type === 'undefined' || | ||
type === 'null') | ||
type === 'constant' || | ||
type === 'collection' || | ||
type === 'undefined' || | ||
type === 'null') | ||
{ | ||
metadata.value = component; | ||
return metadata;} else | ||
if (type === 'function') { | ||
return metadata; | ||
} else if (type === 'function') { | ||
metadata.name = component.name; | ||
if (component._isMockFunction) { | ||
metadata.mockImpl = component.getMockImplementation();}} | ||
metadata.mockImpl = component.getMockImplementation(); | ||
} | ||
} | ||
metadata.refID = refs.size; | ||
@@ -421,13 +421,13 @@ refs.set(component, metadata.refID); | ||
if ( | ||
type === 'function' && | ||
component._isMockFunction && | ||
slot.match(/^mock/)) | ||
type === 'function' && | ||
component._isMockFunction && | ||
slot.match(/^mock/)) | ||
{ | ||
return;} | ||
return; | ||
} | ||
if ( | ||
!component.hasOwnProperty && component[slot] !== undefined || | ||
component.hasOwnProperty && component.hasOwnProperty(slot) || | ||
type === 'object' && component[slot] != Object.prototype[slot]) | ||
!component.hasOwnProperty && component[slot] !== undefined || | ||
component.hasOwnProperty && component.hasOwnProperty(slot) || | ||
type === 'object' && component[slot] != Object.prototype[slot]) | ||
{ | ||
@@ -437,10 +437,10 @@ const slotMetadata = getMetadata(component[slot], refs); | ||
if (!members) { | ||
members = {};} | ||
members = {}; | ||
} | ||
members[slot] = slotMetadata; | ||
} | ||
} | ||
}); | ||
} | ||
members[slot] = slotMetadata;}}});} | ||
// If component is native code function, prototype might be undefined | ||
@@ -451,46 +451,46 @@ if (type === 'function' && component.prototype) { | ||
if (!members) { | ||
members = {};} | ||
members = {}; | ||
} | ||
members.prototype = prototype; | ||
} | ||
} | ||
} | ||
members.prototype = prototype;}}} | ||
if (members) { | ||
metadata.members = members;} | ||
metadata.members = members; | ||
} | ||
return metadata; | ||
} | ||
return metadata;} | ||
function isMockFunction(fn) { | ||
return !!fn._isMockFunction;} | ||
return !!fn._isMockFunction; | ||
} | ||
module.exports = { | ||
module.exports = { | ||
/** | ||
* @see README.md | ||
* @param metadata Metadata for the mock in the schema returned by the | ||
* getMetadata method of this module. | ||
*/ | ||
generateFromMetadata, | ||
* @see README.md | ||
* @param metadata Metadata for the mock in the schema returned by the | ||
* getMetadata method of this module. | ||
*/ | ||
generateFromMetadata, | ||
/** | ||
* @see README.md | ||
* @param component The component for which to retrieve metadata. | ||
*/ | ||
getMetadata, | ||
* @see README.md | ||
* @param component The component for which to retrieve metadata. | ||
*/ | ||
getMetadata, | ||
/** | ||
* @see README.md | ||
*/ | ||
* @see README.md | ||
*/ | ||
getMockFunction() { | ||
return makeComponent({ type: 'function' });}, | ||
return makeComponent({ type: 'function' }); | ||
}, | ||
// Just a short-hand alias | ||
getMockFn() { | ||
return this.getMockFunction();}, | ||
return this.getMockFunction(); | ||
}, | ||
isMockFunction }; |
{ | ||
"name": "jest-mock", | ||
"version": "14.0.0", | ||
"version": "14.2.0-alpha.ca8bfb6e", | ||
"repository": { | ||
@@ -10,7 +10,2 @@ "type": "git", | ||
"main": "build/index.js", | ||
"jest": { | ||
"rootDir": "./src", | ||
"scriptPreprocessor": "../../babel-jest", | ||
"testEnvironment": "node" | ||
}, | ||
"scripts": { | ||
@@ -17,0 +12,0 @@ "test": "../../packages/jest-cli/bin/jest.js" |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
15143
411
2