Skip to content

Commit

Permalink
Reduce singleton (jestjs#3164)
Browse files Browse the repository at this point in the history
* [jest-jasmine2] Split `jasmine-light` over multiple files.

* Don't wrap things that don't need to be wrapped.

* Don't wrap things that don't need to be wrapped (again).

* Lint.

* [jest-jasmine2] Trim `jasmine` singleton.

* Adding NHL to companies that use Jest (jestjs#3163)

* Support custom platforms on jest-haste-map (jestjs#3162)

* Support custom platforms on jest-haste-map

* Run prettier

* Rebase.
  • Loading branch information
wtgtybhertgeghgtwtg authored and skovhus committed Apr 29, 2017
1 parent 84b3457 commit 6badc04
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 188 deletions.
17 changes: 0 additions & 17 deletions packages/jest-jasmine2/src/jasmine/Env.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,6 @@ module.exports = function(j$) {
return 'suite' + nextSuiteId++;
};

const expectationFactory = function(actual, spec) {
return j$.Expectation.Factory({
actual,
addExpectationResult,
});

function addExpectationResult(passed, result) {
return spec.addExpectationResult(passed, result);
}
};

const defaultResourcesForRunnable = function(id, parentRunnableId) {
const resources = {spies: []};

Expand Down Expand Up @@ -209,10 +198,7 @@ module.exports = function(j$) {
};

const topSuite = new j$.Suite({
env: this,
id: getNextSuiteId(),
description: 'test',
expectationFactory,
expectationResultFactory,
});
defaultResourcesForRunnable(topSuite.id);
Expand Down Expand Up @@ -305,11 +291,9 @@ module.exports = function(j$) {

const suiteFactory = function(description) {
const suite = new j$.Suite({
env: self,
id: getNextSuiteId(),
description,
parentSuite: currentDeclarationSuite,
expectationFactory,
expectationResultFactory,
throwOnExpectationFailure,
});
Expand Down Expand Up @@ -398,7 +382,6 @@ module.exports = function(j$) {
const spec = new j$.Spec({
id: getNextSpecId(),
beforeAndAfterFns: beforeAndAfterFns(suite),
expectationFactory,
resultCallback: specResultCallback,
getSpecName(spec) {
return getSpecName(spec, suite);
Expand Down
5 changes: 0 additions & 5 deletions packages/jest-jasmine2/src/jasmine/Spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
const ExpectationFailed = require('./ExpectationFailed');

function Spec(attrs) {
this.expectationFactory = attrs.expectationFactory;
this.resultCallback = attrs.resultCallback || function() {};
this.id = attrs.id;
this.description = attrs.description || '';
Expand All @@ -57,10 +56,6 @@ function Spec(attrs) {
this.expectationResultFactory = attrs.expectationResultFactory ||
function() {};
this.queueRunnerFactory = attrs.queueRunnerFactory || function() {};
this.catchingExceptions = attrs.catchingExceptions ||
function() {
return true;
};
this.throwOnExpectationFailure = !!attrs.throwOnExpectationFailure;

if (!this.queueableFn.fn) {
Expand Down
176 changes: 93 additions & 83 deletions packages/jest-jasmine2/src/jasmine/SpyRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
/* eslint-disable sort-keys */
'use strict';

const CallTracker = require('./CallTracker');
const createSpy = require('./createSpy');
const SpyStrategy = require('./SpyStrategy');

const formatErrorMsg = function() {
function generateErrorMsg(domain, usage) {
const usageDefinition = usage ? '\nUsage: ' + usage : '';
Expand All @@ -45,98 +49,104 @@ const formatErrorMsg = function() {
return generateErrorMsg;
};

module.exports = function(j$) {
const getErrorMsg = formatErrorMsg(
'<spyOn>',
'spyOn(<object>, <methodName>)',
);

function SpyRegistry(options) {
options = options || {};
const currentSpies = options.currentSpies ||
function() {
return [];
};

this.allowRespy = function(allow) {
this.respy = allow;
};

this.spyOn = function(obj, methodName) {
if (obj === void 0) {
throw new Error(
getErrorMsg(
'could not find an object to spy upon for ' + methodName + '()',
),
);
}

if (methodName === void 0) {
throw new Error(getErrorMsg('No method name supplied'));
}

if (obj[methodName] === void 0) {
throw new Error(getErrorMsg(methodName + '() method does not exist'));
}
function isSpy(putativeSpy) {
if (!putativeSpy) {
return false;
}
return putativeSpy.and instanceof SpyStrategy &&
putativeSpy.calls instanceof CallTracker;
};

if (obj[methodName] && j$.isSpy(obj[methodName])) {
if (this.respy) {
return obj[methodName];
} else {
throw new Error(
getErrorMsg(methodName + ' has already been spied upon'),
);
}
}
const getErrorMsg = formatErrorMsg(
'<spyOn>',
'spyOn(<object>, <methodName>)',
);

let descriptor;
try {
descriptor = Object.getOwnPropertyDescriptor(obj, methodName);
} catch (e) {
// IE 8 doesn't support `definePropery` on non-DOM nodes
}
function SpyRegistry(options) {
options = options || {};
const currentSpies = options.currentSpies ||
function() {
return [];
};

if (descriptor && !(descriptor.writable || descriptor.set)) {
this.allowRespy = function(allow) {
this.respy = allow;
};

this.spyOn = function(obj, methodName) {
if (obj === void 0) {
throw new Error(
getErrorMsg(
'could not find an object to spy upon for ' + methodName + '()',
),
);
}

if (methodName === void 0) {
throw new Error(getErrorMsg('No method name supplied'));
}

if (obj[methodName] === void 0) {
throw new Error(getErrorMsg(methodName + '() method does not exist'));
}

if (obj[methodName] && isSpy(obj[methodName])) {
if (this.respy) {
return obj[methodName];
} else {
throw new Error(
getErrorMsg(
methodName + ' is not declared writable or has no setter',
),
getErrorMsg(methodName + ' has already been spied upon'),
);
}

const originalMethod = obj[methodName];
const spiedMethod = j$.createSpy(methodName, originalMethod);
let restoreStrategy;

if (Object.prototype.hasOwnProperty.call(obj, methodName)) {
restoreStrategy = function() {
}

let descriptor;
try {
descriptor = Object.getOwnPropertyDescriptor(obj, methodName);
} catch (e) {
// IE 8 doesn't support `definePropery` on non-DOM nodes
}

if (descriptor && !(descriptor.writable || descriptor.set)) {
throw new Error(
getErrorMsg(
methodName + ' is not declared writable or has no setter',
),
);
}

const originalMethod = obj[methodName];
const spiedMethod = createSpy(methodName, originalMethod);
let restoreStrategy;

if (Object.prototype.hasOwnProperty.call(obj, methodName)) {
restoreStrategy = function() {
obj[methodName] = originalMethod;
};
} else {
restoreStrategy = function() {
if (!delete obj[methodName]) {
obj[methodName] = originalMethod;
};
} else {
restoreStrategy = function() {
if (!delete obj[methodName]) {
obj[methodName] = originalMethod;
}
};
}
}
};
}

currentSpies().push({
restoreObjectToOriginalState: restoreStrategy,
});
currentSpies().push({
restoreObjectToOriginalState: restoreStrategy,
});

obj[methodName] = spiedMethod;
obj[methodName] = spiedMethod;

return spiedMethod;
};
return spiedMethod;
};

this.clearSpies = function() {
const spies = currentSpies();
for (let i = spies.length - 1; i >= 0; i--) {
const spyEntry = spies[i];
spyEntry.restoreObjectToOriginalState();
}
};
}
this.clearSpies = function() {
const spies = currentSpies();
for (let i = spies.length - 1; i >= 0; i--) {
const spyEntry = spies[i];
spyEntry.restoreObjectToOriginalState();
}
};
}

return SpyRegistry;
};
module.exports = SpyRegistry;
2 changes: 0 additions & 2 deletions packages/jest-jasmine2/src/jasmine/Suite.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,9 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
const ExpectationFailed = require('./ExpectationFailed');

function Suite(attrs) {
this.env = attrs.env;
this.id = attrs.id;
this.parentSuite = attrs.parentSuite;
this.description = attrs.description;
this.expectationFactory = attrs.expectationFactory;
this.expectationResultFactory = attrs.expectationResultFactory;
this.throwOnExpectationFailure = !!attrs.throwOnExpectationFailure;

Expand Down
78 changes: 78 additions & 0 deletions packages/jest-jasmine2/src/jasmine/createSpy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* 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.
*
* @flow
*/
// This file is a heavily modified fork of Jasmine. The original license of the code:
/*
Copyright (c) 2008-2016 Pivotal Labs
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/* eslint-disable sort-keys */
'use strict';

const CallTracker = require('./CallTracker');
const SpyStrategy = require('./SpyStrategy');

function createSpy(name, originalFn) {
const spyStrategy = new SpyStrategy({
name,
fn: originalFn,
getSpy() {
return spy;
},
});
const callTracker = new CallTracker();
const spy = function() {
const callData = {
object: this,
args: Array.prototype.slice.apply(arguments),
};

callTracker.track(callData);
const returnValue = spyStrategy.exec.apply(this, arguments);
callData.returnValue = returnValue;

return returnValue;
};

for (const prop in originalFn) {
if (prop === 'and' || prop === 'calls') {
throw new Error(
"Jasmine spies would overwrite the 'and' and 'calls' properties " +
'on the object being spied upon',
);
}

spy[prop] = originalFn[prop];
}

spy.and = spyStrategy;
spy.calls = callTracker;

return spy;
};

module.exports = createSpy;
Loading

0 comments on commit 6badc04

Please sign in to comment.