jest-mock
Advanced tools
Comparing version 16.1.0-alpha.691b0e22 to 17.0.0
@@ -38,2 +38,4 @@ /** | ||
const MOCK_CONSTRUCTOR_NAME = 'mockConstructor'; | ||
@@ -174,51 +176,14 @@ | ||
function 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)); | ||
} | ||
class ModuleMocker { | ||
// 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, '$'); | ||
} | ||
/* eslint-disable no-new-func */ | ||
return new Function( | ||
MOCK_CONSTRUCTOR_NAME, | ||
'return function ' + name + '() {' + | ||
'return ' + MOCK_CONSTRUCTOR_NAME + '.apply(this,arguments);' + | ||
'}' + bindCall)( | ||
mockConstructor); | ||
/* eslint-enable no-new-func */ | ||
} | ||
class ModuleMocker { | ||
constructor() { | ||
/** | ||
* @see README.md | ||
* @param global Global object of the test environment, used to create | ||
* mocks | ||
*/ | ||
constructor(global) { | ||
this._environmentGlobal = global; | ||
this._mockRegistry = new WeakMap(); | ||
@@ -252,7 +217,7 @@ } | ||
if (metadata.type === 'object') { | ||
return {}; | ||
return new this._environmentGlobal.Object(); | ||
} else if (metadata.type === 'array') { | ||
return []; | ||
return new this._environmentGlobal.Array(); | ||
} else if (metadata.type === 'regexp') { | ||
return new RegExp(''); | ||
return new this._environmentGlobal.RegExp(''); | ||
} else if ( | ||
@@ -334,3 +299,3 @@ metadata.type === 'constant' || | ||
f = createMockFunction(metadata, mockConstructor); | ||
f = this._createMockFunction(metadata, mockConstructor); | ||
f._isMockFunction = true; | ||
@@ -408,2 +373,48 @@ f.getMockImplementation = () => this._ensureMock(f).private.mockImpl; | ||
_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( | ||
@@ -410,0 +421,0 @@ metadata, |
{ | ||
"name": "jest-mock", | ||
"version": "16.1.0-alpha.691b0e22", | ||
"version": "17.0.0", | ||
"repository": { | ||
@@ -5,0 +5,0 @@ "type": "git", |
@@ -5,2 +5,7 @@ # jest-mock | ||
### `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)` | ||
@@ -7,0 +12,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
18199
464
1
123