Added logging, changed some directory structure

This commit is contained in:
2018-01-13 21:33:40 -05:00
parent f079a5f067
commit 8e72ffb917
73656 changed files with 35284 additions and 53718 deletions

View File

@@ -0,0 +1,4 @@
**/__mocks__/**
**/__tests__/**
src
yarn.lock

View File

@@ -0,0 +1,122 @@
# jest-mock
## API
### `constructor(global)`
Creates a new module mocker that generates mocks as if they were created in an
environment with the given global object.
### `generateFromMetadata(metadata)`
Generates a mock based on the given metadata (Metadata for the mock in
the schema returned by the getMetadata method of this module). Mocks treat
functions specially, and all mock functions have additional members, described
in the documentation for `fn` in this module.
One important note: function prototypes are handled specially by this
mocking framework. For functions with prototypes, when called as a
constructor, the mock will install mocked function members on the instance.
This allows different instances of the same constructor to have different
values for its mocks member and its return values.
### `getMetadata(component)`
Inspects the argument and returns its schema in the following recursive format:
```js
{
type: ...
members : {}
}
```
Where type is one of `array`, `object`, `function`, or `ref`, and members
is an optional dictionary where the keys are member names and the values
are metadata objects. Function prototypes are defined simply by defining
metadata for the `member.prototype` of the function. The type of a function
prototype should always be `object`. For instance, a simple class might be
defined like this:
```js
{
type: 'function',
members: {
staticMethod: {type: 'function'},
prototype: {
type: 'object',
members: {
instanceMethod: {type: 'function'}
}
}
}
}
```
Metadata may also contain references to other objects defined within the
same metadata object. The metadata for the referent must be marked with
`refID` key and an arbitrary value. The referrer must be marked with a
`ref` key that has the same value as object with refID that it refers to.
For instance, this metadata blob:
```js
{
type: 'object',
refID: 1,
members: {
self: {ref: 1}
}
}
```
defines an object with a slot named `self` that refers back to the object.
### `fn`
Generates a stand-alone function with members that help drive unit tests or
confirm expectations. Specifically, functions returned by this method have
the following members:
##### `.mock`
An object with two members, `calls`, and `instances`, which are both
lists. The items in the `calls` list are the arguments with which the
function was called. The "instances" list stores the value of 'this' for
each call to the function. This is useful for retrieving instances from a
constructor.
##### `.mockReturnValueOnce(value)`
Pushes the given value onto a FIFO queue of return values for the
function.
##### `.mockReturnValue(value)`
Sets the default return value for the function.
##### `.mockImplementationOnce(function)`
Pushes the given mock implementation onto a FIFO queue of mock
implementations for the function.
##### `.mockImplementation(function)`
Sets the default mock implementation for the function.
##### `.mockReturnThis()`
Syntactic sugar for .mockImplementation(function() {return this;})
In case both `mockImplementationOnce()/mockImplementation()` and
`mockReturnValueOnce()/mockReturnValue()` are called. The priority of which to
use is based on what is the last call:
- if the last call is mockReturnValueOnce() or mockReturnValue(),
use the specific return value or default return value.
If specific return values are used up or no default return value is set,
fall back to try mockImplementation();
- if the last call is mockImplementationOnce() or mockImplementation(),
run the specific implementation and return the result or run default
implementation and return the result.

View File

@@ -0,0 +1,596 @@
'use strict';var _map = require('babel-runtime/core-js/map');var _map2 = _interopRequireDefault(_map);var _weakMap = require('babel-runtime/core-js/weak-map');var _weakMap2 = _interopRequireDefault(_weakMap);var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);var _createClass2 = require('babel-runtime/helpers/createClass');var _createClass3 = _interopRequireDefault(_createClass2);var _keys = require('babel-runtime/core-js/object/keys');var _keys2 = _interopRequireDefault(_keys);var _getOwnPropertyDescriptor = require('babel-runtime/core-js/object/get-own-property-descriptor');var _getOwnPropertyDescriptor2 = _interopRequireDefault(_getOwnPropertyDescriptor);var _getOwnPropertyNames = require('babel-runtime/core-js/object/get-own-property-names');var _getOwnPropertyNames2 = _interopRequireDefault(_getOwnPropertyNames);var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);var _create = require('babel-runtime/core-js/object/create');var _create2 = _interopRequireDefault(_create);var _assign = require('babel-runtime/core-js/object/assign');var _assign2 = _interopRequireDefault(_assign);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}
var MOCK_CONSTRUCTOR_NAME = 'mockConstructor';
// $FlowFixMe
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*
*/var RESERVED_KEYWORDS = (0, _assign2.default)((0, _create2.default)(null), { arguments: true, await: true, break: true, case: true, catch: true, class: true, const: true, continue: true,
debugger: true,
default: true,
delete: true,
do: true,
else: true,
enum: true,
eval: true,
export: true,
extends: true,
false: true,
finally: true,
for: true,
function: true,
if: true,
implements: true,
import: true,
in: true,
instanceof: true,
interface: true,
let: true,
new: true,
null: true,
package: true,
private: true,
protected: true,
public: true,
return: true,
static: true,
super: true,
switch: true,
this: true,
throw: true,
true: true,
try: true,
typeof: true,
var: true,
void: true,
while: true,
with: true,
yield: true });
function isA(typeName, value) {
return Object.prototype.toString.apply(value) === '[object ' + typeName + ']';
}
function getType(ref) {
if (isA('Function', ref) || isA('AsyncFunction', 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) ||
isA('Symbol', 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) {
return (
(prop === 'arguments' ||
prop === 'caller' ||
prop === 'callee' ||
prop === 'name' ||
prop === 'length') && (
isA('Function', object) || isA('AsyncFunction', object)) ||
(prop === 'source' ||
prop === 'global' ||
prop === 'ignoreCase' ||
prop === 'multiline') &&
isA('RegExp', object));
}
function getSlots(object) {
var slots = {};
if (!object) {
return [];
}
var parent = (0, _getPrototypeOf2.default)(object);
do {
if (object === (0, _getPrototypeOf2.default)(Function)) {
break;
}
var ownNames = (0, _getOwnPropertyNames2.default)(object);
for (var i = 0; i < ownNames.length; i++) {
var prop = ownNames[i];
if (!isReadonlyProp(object, prop)) {
var propDesc = (0, _getOwnPropertyDescriptor2.default)(object, prop);
if (!propDesc.get || object.__esModule) {
slots[prop] = true;
}
}
}
object = parent;
} while (object && (parent = (0, _getPrototypeOf2.default)(object)) !== null);
return (0, _keys2.default)(slots);
}var
ModuleMockerClass = function () {
/**
* @see README.md
* @param global Global object of the test environment, used to create
* mocks
*/
function ModuleMockerClass(global) {(0, _classCallCheck3.default)(this, ModuleMockerClass);
this._environmentGlobal = global;
this._mockState = new _weakMap2.default();
this._mockConfigRegistry = new _weakMap2.default();
this.ModuleMocker = ModuleMockerClass;
}(0, _createClass3.default)(ModuleMockerClass, [{ key: '_ensureMockConfig', value: function _ensureMockConfig(
f) {
var config = this._mockConfigRegistry.get(f);
if (!config) {
config = this._defaultMockConfig();
this._mockConfigRegistry.set(f, config);
}
return config;
} }, { key: '_ensureMockState', value: function _ensureMockState(
f) {
var state = this._mockState.get(f);
if (!state) {
state = this._defaultMockState();
this._mockState.set(f, state);
}
return state;
} }, { key: '_defaultMockConfig', value: function _defaultMockConfig()
{
return {
defaultReturnValue: undefined,
isReturnValueLastSet: false,
mockImpl: undefined,
specificMockImpls: [],
specificReturnValues: [] };
} }, { key: '_defaultMockState', value: function _defaultMockState()
{
return {
calls: [],
instances: [] };
} }, { key: '_makeComponent', value: function _makeComponent(
metadata, restore) {var _this2 = this;
if (metadata.type === 'object') {
return new this._environmentGlobal.Object();
} else if (metadata.type === 'array') {
return new this._environmentGlobal.Array();
} else if (metadata.type === 'regexp') {
return new this._environmentGlobal.RegExp('');
} else if (
metadata.type === 'constant' ||
metadata.type === 'collection' ||
metadata.type === 'null' ||
metadata.type === 'undefined')
{
return metadata.value;
} else if (metadata.type === 'function') {
/* eslint-disable prefer-const */
var f = void 0;
/* eslint-enable prefer-const */
var prototype = metadata.members &&
metadata.members.prototype &&
metadata.members.prototype.members || {};
var prototypeSlots = getSlots(prototype);
var mocker = this;
var mockConstructor = function mockConstructor() {var _this = this;
var mockState = mocker._ensureMockState(f);
var mockConfig = mocker._ensureMockConfig(f);
mockState.instances.push(this);
mockState.calls.push(Array.prototype.slice.call(arguments));
if (this instanceof f) {
// This is probably being called as a constructor
prototypeSlots.forEach(function (slot) {
// Copy prototype methods to the instance to make
// it easier to interact with mock instance call and
// return values
if (prototype[slot].type === 'function') {
var protoImpl = _this[slot];
_this[slot] = mocker.generateFromMetadata(prototype[slot]);
_this[slot]._protoImpl = protoImpl;
}
});
// Run the mock constructor implementation
return (
mockConfig.mockImpl && mockConfig.mockImpl.apply(this, arguments));
}
var returnValue = void 0;
// 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.isReturnValueLastSet) {
returnValue = mockConfig.specificReturnValues.shift();
if (returnValue === undefined) {
returnValue = mockConfig.defaultReturnValue;
}
return returnValue;
}
// If mockImplementationOnce()/mockImplementation() is last set,
// or specific return values are used up, use the mock implementation.
var specificMockImpl = void 0;
if (returnValue === undefined) {
specificMockImpl = mockConfig.specificMockImpls.shift();
if (specificMockImpl === undefined) {
specificMockImpl = mockConfig.mockImpl;
}
if (specificMockImpl) {
return specificMockImpl.apply(this, arguments);
}
}
// Otherwise use prototype implementation
if (returnValue === undefined && f._protoImpl) {
return f._protoImpl.apply(this, arguments);
}
return returnValue;
};
f = this._createMockFunction(metadata, mockConstructor);
f._isMockFunction = true;
f.getMockImplementation = function () {return _this2._ensureMockConfig(f).mockImpl;};
this._mockState.set(f, this._defaultMockState());
this._mockConfigRegistry.set(f, this._defaultMockConfig());
// $FlowFixMe - defineProperty getters not supported
Object.defineProperty(f, 'mock', {
configurable: false,
enumerable: true,
get: function get() {return _this2._ensureMockState(f);},
set: function set(val) {return _this2._mockState.set(f, val);} });
f.mockClear = function () {
_this2._mockState.delete(f);
};
f.mockReset = function () {
_this2._mockState.delete(f);
_this2._mockConfigRegistry.delete(f);
};
f.mockReturnValueOnce = function (value) {
// next function call will return this value or default return value
var mockConfig = _this2._ensureMockConfig(f);
mockConfig.isReturnValueLastSet = true;
mockConfig.specificReturnValues.push(value);
return f;
};
f.mockReturnValue = function (value) {
// next function call will return specified return value or this one
var mockConfig = _this2._ensureMockConfig(f);
mockConfig.isReturnValueLastSet = true;
mockConfig.defaultReturnValue = value;
return f;
};
f.mockImplementationOnce = function (fn) {
// next function call will use this mock implementation return value
// or default mock implementation return value
var mockConfig = _this2._ensureMockConfig(f);
mockConfig.isReturnValueLastSet = false;
mockConfig.specificMockImpls.push(fn);
return f;
};
f.mockImplementation = function (fn) {
// next function call will use mock implementation return value
var mockConfig = _this2._ensureMockConfig(f);
mockConfig.isReturnValueLastSet = false;
mockConfig.mockImpl = fn;
return f;
};
f.mockReturnThis = function () {return (
f.mockImplementation(function () {
return this;
}));};
if (metadata.mockImpl) {
f.mockImplementation(metadata.mockImpl);
}
f.mockRestore = restore ? restore : function () {};
return f;
} else {
var unknownType = metadata.type || 'undefined type';
throw new Error('Unrecognized type ' + unknownType);
}
} }, { key: '_createMockFunction', value: function _createMockFunction(
metadata,
mockConstructor)
{
var name = metadata.name;
// Special case functions named `mockConstructor` to guard for infinite
// loops.
if (!name || name === MOCK_CONSTRUCTOR_NAME) {
return mockConstructor;
}
// Preserve `name` property of mocked function.
var boundFunctionPrefix = 'bound ';
var bindCall = '';
// if-do-while for perf reasons. The common case is for the if to fail.
if (name && name.startsWith(boundFunctionPrefix)) {
do {
name = name.substring(boundFunctionPrefix.length);
// Call bind() just to alter the function name.
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[name]) {
name = '$' + name;
}
// It's also a syntax error to define a function with a reserved character
// as part of it's name.
if (/[\s-]/.test(name)) {
name = name.replace(/[\s-]/g, '$');
}
var body =
'return function ' +
name +
'() {' +
'return ' +
MOCK_CONSTRUCTOR_NAME +
'.apply(this,arguments);' +
'}' +
bindCall;
var createConstructor = new this._environmentGlobal.Function(
MOCK_CONSTRUCTOR_NAME,
body);
return createConstructor(mockConstructor);
} }, { key: '_generateMock', value: function _generateMock(
metadata,
callbacks,
refs)
{var _this3 = this;
var mock = this._makeComponent(metadata);
if (metadata.refID != null) {
refs[metadata.refID] = mock;
}
getSlots(metadata.members).forEach(function (slot) {
var slotMetadata = metadata.members && metadata.members[slot] || {};
if (slotMetadata.ref != null) {
callbacks.push(function () {return mock[slot] = refs[slotMetadata.ref];});
} else {
mock[slot] = _this3._generateMock(slotMetadata, callbacks, refs);
}
});
if (
metadata.type !== 'undefined' &&
metadata.type !== 'null' &&
mock.prototype)
{
mock.prototype.constructor = mock;
}
return mock;
}
/**
* @see README.md
* @param metadata Metadata for the mock in the schema returned by the
* getMetadata method of this module.
*/ }, { key: 'generateFromMetadata', value: function generateFromMetadata(
_metadata) {
var callbacks = [];
var refs = {};
var mock = this._generateMock(_metadata, callbacks, refs);
callbacks.forEach(function (setter) {return setter();});
return mock;
}
/**
* @see README.md
* @param component The component for which to retrieve metadata.
*/ }, { key: 'getMetadata', value: function getMetadata(
component, _refs) {var _this4 = this;
var refs = _refs || new _map2.default();
var ref = refs.get(component);
if (ref != null) {
return { ref: ref };
}
var type = getType(component);
if (!type) {
return null;
}
var metadata = { type: type };
if (
type === 'constant' ||
type === 'collection' ||
type === 'undefined' ||
type === 'null')
{
metadata.value = component;
return metadata;
} else if (type === 'function') {
metadata.name = component.name;
if (component._isMockFunction) {
metadata.mockImpl = component.getMockImplementation();
}
}
metadata.refID = refs.size;
refs.set(component, metadata.refID);
var members = null;
// Leave arrays alone
if (type !== 'array') {
if (type !== 'undefined') {
getSlots(component).forEach(function (slot) {
if (
type === 'function' &&
component._isMockFunction &&
slot.match(/^mock/))
{
return;
}
if (
!component.hasOwnProperty && component[slot] !== undefined ||
component.hasOwnProperty && component.hasOwnProperty(slot) ||
type === 'object' && component[slot] != Object.prototype[slot])
{
var slotMetadata = _this4.getMetadata(component[slot], refs);
if (slotMetadata) {
if (!members) {
members = {};
}
members[slot] = slotMetadata;
}
}
});
}
// If component is native code function, prototype might be undefined
if (type === 'function' && component.prototype) {
var prototype = this.getMetadata(component.prototype, refs);
if (prototype && prototype.members) {
if (!members) {
members = {};
}
members.prototype = prototype;
}
}
}
if (members) {
metadata.members = members;
}
return metadata;
} }, { key: 'isMockFunction', value: function isMockFunction(
fn) {
return !!fn._isMockFunction;
} }, { key: 'fn', value: function fn(
implementation) {
var fn = this._makeComponent({ type: 'function' });
if (implementation) {
fn.mockImplementation(implementation);
}
return fn;
} }, { key: 'spyOn', value: function spyOn(
object, methodName) {
var original = object[methodName];
if (!this.isMockFunction(original)) {
if (typeof original !== 'function') {
throw new Error(
'Cannot spyOn the ' + methodName + ' property; it is not a function');
}
object[methodName] = this._makeComponent({ type: 'function' }, function () {
object[methodName] = original;
});
object[methodName].mockImplementation(function () {
return original.apply(this, arguments);
});
}
return object[methodName];
} }, { key: 'clearAllMocks', value: function clearAllMocks()
{
this._mockState = new _weakMap2.default();
} }, { key: 'resetAllMocks', value: function resetAllMocks()
{
this._mockConfigRegistry = new _weakMap2.default();
this._mockState = new _weakMap2.default();
} }]);return ModuleMockerClass;}();
module.exports = new ModuleMockerClass(global);

View File

@@ -0,0 +1,596 @@
'use strict';
const MOCK_CONSTRUCTOR_NAME = 'mockConstructor';
// $FlowFixMe
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*
*/const RESERVED_KEYWORDS = Object.assign(Object.create(null), { arguments: true, await: true, break: true, case: true, catch: true, class: true, const: true, continue: true,
debugger: true,
default: true,
delete: true,
do: true,
else: true,
enum: true,
eval: true,
export: true,
extends: true,
false: true,
finally: true,
for: true,
function: true,
if: true,
implements: true,
import: true,
in: true,
instanceof: true,
interface: true,
let: true,
new: true,
null: true,
package: true,
private: true,
protected: true,
public: true,
return: true,
static: true,
super: true,
switch: true,
this: true,
throw: true,
true: true,
try: true,
typeof: true,
var: true,
void: true,
while: true,
with: true,
yield: true });
function isA(typeName, value) {
return Object.prototype.toString.apply(value) === '[object ' + typeName + ']';
}
function getType(ref) {
if (isA('Function', ref) || isA('AsyncFunction', 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) ||
isA('Symbol', 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) {
return (
(prop === 'arguments' ||
prop === 'caller' ||
prop === 'callee' ||
prop === 'name' ||
prop === 'length') && (
isA('Function', object) || isA('AsyncFunction', object)) ||
(prop === 'source' ||
prop === 'global' ||
prop === 'ignoreCase' ||
prop === 'multiline') &&
isA('RegExp', object));
}
function getSlots(object) {
const slots = {};
if (!object) {
return [];
}
let parent = Object.getPrototypeOf(object);
do {
if (object === Object.getPrototypeOf(Function)) {
break;
}
const ownNames = Object.getOwnPropertyNames(object);
for (let i = 0; i < ownNames.length; i++) {
const prop = ownNames[i];
if (!isReadonlyProp(object, prop)) {
const propDesc = Object.getOwnPropertyDescriptor(object, prop);
if (!propDesc.get || object.__esModule) {
slots[prop] = true;
}
}
}
object = parent;
} while (object && (parent = Object.getPrototypeOf(object)) !== null);
return Object.keys(slots);
}
class ModuleMockerClass {
/**
* @see README.md
* @param global Global object of the test environment, used to create
* mocks
*/
constructor(global) {
this._environmentGlobal = global;
this._mockState = new WeakMap();
this._mockConfigRegistry = new WeakMap();
this.ModuleMocker = ModuleMockerClass;
}
_ensureMockConfig(f) {
let config = this._mockConfigRegistry.get(f);
if (!config) {
config = this._defaultMockConfig();
this._mockConfigRegistry.set(f, config);
}
return config;
}
_ensureMockState(f) {
let state = this._mockState.get(f);
if (!state) {
state = this._defaultMockState();
this._mockState.set(f, state);
}
return state;
}
_defaultMockConfig() {
return {
defaultReturnValue: undefined,
isReturnValueLastSet: false,
mockImpl: undefined,
specificMockImpls: [],
specificReturnValues: [] };
}
_defaultMockState() {
return {
calls: [],
instances: [] };
}
_makeComponent(metadata, restore) {
if (metadata.type === 'object') {
return new this._environmentGlobal.Object();
} else if (metadata.type === 'array') {
return new this._environmentGlobal.Array();
} else if (metadata.type === 'regexp') {
return new this._environmentGlobal.RegExp('');
} else if (
metadata.type === 'constant' ||
metadata.type === 'collection' ||
metadata.type === 'null' ||
metadata.type === 'undefined')
{
return metadata.value;
} else if (metadata.type === 'function') {
/* eslint-disable prefer-const */
let f;
/* eslint-enable prefer-const */
const prototype = metadata.members &&
metadata.members.prototype &&
metadata.members.prototype.members || {};
const prototypeSlots = getSlots(prototype);
const mocker = this;
const mockConstructor = function () {
const mockState = mocker._ensureMockState(f);
const mockConfig = mocker._ensureMockConfig(f);
mockState.instances.push(this);
mockState.calls.push(Array.prototype.slice.call(arguments));
if (this instanceof f) {
// This is probably being called as a constructor
prototypeSlots.forEach(slot => {
// Copy prototype methods to the instance to make
// it easier to interact with mock instance call and
// return values
if (prototype[slot].type === 'function') {
const protoImpl = this[slot];
this[slot] = mocker.generateFromMetadata(prototype[slot]);
this[slot]._protoImpl = protoImpl;
}
});
// Run the mock constructor implementation
return (
mockConfig.mockImpl && mockConfig.mockImpl.apply(this, arguments));
}
let returnValue;
// 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.isReturnValueLastSet) {
returnValue = mockConfig.specificReturnValues.shift();
if (returnValue === undefined) {
returnValue = mockConfig.defaultReturnValue;
}
return returnValue;
}
// If mockImplementationOnce()/mockImplementation() is last set,
// or specific return values are used up, use the mock implementation.
let specificMockImpl;
if (returnValue === undefined) {
specificMockImpl = mockConfig.specificMockImpls.shift();
if (specificMockImpl === undefined) {
specificMockImpl = mockConfig.mockImpl;
}
if (specificMockImpl) {
return specificMockImpl.apply(this, arguments);
}
}
// Otherwise use prototype implementation
if (returnValue === undefined && f._protoImpl) {
return f._protoImpl.apply(this, arguments);
}
return returnValue;
};
f = this._createMockFunction(metadata, mockConstructor);
f._isMockFunction = true;
f.getMockImplementation = () => this._ensureMockConfig(f).mockImpl;
this._mockState.set(f, this._defaultMockState());
this._mockConfigRegistry.set(f, this._defaultMockConfig());
// $FlowFixMe - defineProperty getters not supported
Object.defineProperty(f, 'mock', {
configurable: false,
enumerable: true,
get: () => this._ensureMockState(f),
set: val => this._mockState.set(f, val) });
f.mockClear = () => {
this._mockState.delete(f);
};
f.mockReset = () => {
this._mockState.delete(f);
this._mockConfigRegistry.delete(f);
};
f.mockReturnValueOnce = value => {
// next function call will return this value or default return value
const mockConfig = this._ensureMockConfig(f);
mockConfig.isReturnValueLastSet = true;
mockConfig.specificReturnValues.push(value);
return f;
};
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.mockImplementationOnce = fn => {
// next function call will use this mock implementation return value
// or default mock implementation return value
const mockConfig = this._ensureMockConfig(f);
mockConfig.isReturnValueLastSet = false;
mockConfig.specificMockImpls.push(fn);
return f;
};
f.mockImplementation = fn => {
// next function call will use mock implementation return value
const mockConfig = this._ensureMockConfig(f);
mockConfig.isReturnValueLastSet = false;
mockConfig.mockImpl = fn;
return f;
};
f.mockReturnThis = () =>
f.mockImplementation(function () {
return this;
});
if (metadata.mockImpl) {
f.mockImplementation(metadata.mockImpl);
}
f.mockRestore = restore ? restore : () => {};
return f;
} else {
const unknownType = metadata.type || 'undefined type';
throw new Error('Unrecognized type ' + unknownType);
}
}
_createMockFunction(
metadata,
mockConstructor)
{
let name = metadata.name;
// Special case functions named `mockConstructor` to guard for infinite
// loops.
if (!name || name === MOCK_CONSTRUCTOR_NAME) {
return mockConstructor;
}
// Preserve `name` property of mocked function.
const boundFunctionPrefix = 'bound ';
let bindCall = '';
// if-do-while for perf reasons. The common case is for the if to fail.
if (name && name.startsWith(boundFunctionPrefix)) {
do {
name = name.substring(boundFunctionPrefix.length);
// Call bind() just to alter the function name.
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[name]) {
name = '$' + name;
}
// It's also a syntax error to define a function with a reserved character
// as part of it's name.
if (/[\s-]/.test(name)) {
name = name.replace(/[\s-]/g, '$');
}
const body =
'return function ' +
name +
'() {' +
'return ' +
MOCK_CONSTRUCTOR_NAME +
'.apply(this,arguments);' +
'}' +
bindCall;
const createConstructor = new this._environmentGlobal.Function(
MOCK_CONSTRUCTOR_NAME,
body);
return createConstructor(mockConstructor);
}
_generateMock(
metadata,
callbacks,
refs)
{
const mock = this._makeComponent(metadata);
if (metadata.refID != null) {
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] = this._generateMock(slotMetadata, callbacks, refs);
}
});
if (
metadata.type !== 'undefined' &&
metadata.type !== 'null' &&
mock.prototype)
{
mock.prototype.constructor = mock;
}
return mock;
}
/**
* @see README.md
* @param metadata Metadata for the mock in the schema returned by the
* getMetadata method of this module.
*/
generateFromMetadata(_metadata) {
const callbacks = [];
const refs = {};
const mock = this._generateMock(_metadata, callbacks, refs);
callbacks.forEach(setter => setter());
return mock;
}
/**
* @see README.md
* @param component The component for which to retrieve metadata.
*/
getMetadata(component, _refs) {
const refs = _refs || new Map();
const ref = refs.get(component);
if (ref != null) {
return { ref };
}
const type = getType(component);
if (!type) {
return null;
}
const metadata = { type };
if (
type === 'constant' ||
type === 'collection' ||
type === 'undefined' ||
type === 'null')
{
metadata.value = component;
return metadata;
} else if (type === 'function') {
metadata.name = component.name;
if (component._isMockFunction) {
metadata.mockImpl = component.getMockImplementation();
}
}
metadata.refID = refs.size;
refs.set(component, metadata.refID);
let members = null;
// Leave arrays alone
if (type !== 'array') {
if (type !== 'undefined') {
getSlots(component).forEach(slot => {
if (
type === 'function' &&
component._isMockFunction &&
slot.match(/^mock/))
{
return;
}
if (
!component.hasOwnProperty && component[slot] !== undefined ||
component.hasOwnProperty && component.hasOwnProperty(slot) ||
type === 'object' && component[slot] != Object.prototype[slot])
{
const slotMetadata = this.getMetadata(component[slot], refs);
if (slotMetadata) {
if (!members) {
members = {};
}
members[slot] = slotMetadata;
}
}
});
}
// If component is native code function, prototype might be undefined
if (type === 'function' && component.prototype) {
const prototype = this.getMetadata(component.prototype, refs);
if (prototype && prototype.members) {
if (!members) {
members = {};
}
members.prototype = prototype;
}
}
}
if (members) {
metadata.members = members;
}
return metadata;
}
isMockFunction(fn) {
return !!fn._isMockFunction;
}
fn(implementation) {
const fn = this._makeComponent({ type: 'function' });
if (implementation) {
fn.mockImplementation(implementation);
}
return fn;
}
spyOn(object, methodName) {
const original = object[methodName];
if (!this.isMockFunction(original)) {
if (typeof original !== 'function') {
throw new Error(
'Cannot spyOn the ' + methodName + ' property; it is not a function');
}
object[methodName] = this._makeComponent({ type: 'function' }, () => {
object[methodName] = original;
});
object[methodName].mockImplementation(function () {
return original.apply(this, arguments);
});
}
return object[methodName];
}
clearAllMocks() {
this._mockState = new WeakMap();
}
resetAllMocks() {
this._mockConfigRegistry = new WeakMap();
this._mockState = new WeakMap();
}}
module.exports = new ModuleMockerClass(global);

View File

@@ -0,0 +1,46 @@
{
"_args": [
[
"jest-mock@20.0.3",
"C:\\Users\\deranjer\\go\\src\\github.com\\deranjer\\goTorrent\\torrent-project"
]
],
"_from": "jest-mock@20.0.3",
"_id": "jest-mock@20.0.3",
"_inBundle": false,
"_integrity": "sha1-i8Bw6QQUqhVcEajWTIaaDVxx2lk=",
"_location": "/react-scripts/jest-mock",
"_phantomChildren": {},
"_requested": {
"type": "version",
"registry": true,
"raw": "jest-mock@20.0.3",
"name": "jest-mock",
"escapedName": "jest-mock",
"rawSpec": "20.0.3",
"saveSpec": null,
"fetchSpec": "20.0.3"
},
"_requiredBy": [
"/react-scripts/jest-environment-jsdom",
"/react-scripts/jest-environment-node",
"/react-scripts/jest-util"
],
"_resolved": "https://registry.npmjs.org/jest-mock/-/jest-mock-20.0.3.tgz",
"_spec": "20.0.3",
"_where": "C:\\Users\\deranjer\\go\\src\\github.com\\deranjer\\goTorrent\\torrent-project",
"browser": "build-es5/index.js",
"bugs": {
"url": "https://github.com/facebook/jest/issues"
},
"description": "## API",
"homepage": "https://github.com/facebook/jest#readme",
"license": "BSD-3-Clause",
"main": "build/index.js",
"name": "jest-mock",
"repository": {
"type": "git",
"url": "git+https://github.com/facebook/jest.git"
},
"version": "20.0.3"
}