Skip to content

Commit

Permalink
test: fix failing tests for spyOnProperty
Browse files Browse the repository at this point in the history
  • Loading branch information
phra committed Dec 19, 2017
1 parent 4cfb737 commit 65d884d
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 26 deletions.
2 changes: 1 addition & 1 deletion flow-typed/npm/jest_v21.x.x.js
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ declare var expect: {
// TODO handle return type
// http://jasmine.github.io/2.4/introduction.html#section-Spies
declare function spyOn(value: mixed, method: string): Object;
declare function spyOnProperty(value: mixed, propertyName: string, accessType: 'get' | 'set'): Object;
declare function spyOnProperty(value: mixed, propertyName: string, accessType: string): Object;

/** Holds all functions related to manipulating test runner */
declare var jest: JestObjectType;
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-jasmine2/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ const addSnapshotData = (results, snapshotState) => {
});

const uncheckedCount = snapshotState.getUncheckedCount();
let uncheckedKeys
let uncheckedKeys;

if (uncheckedCount) {
uncheckedKeys = snapshotState.getUncheckedKeys();
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-jasmine2/src/jasmine/jasmine_light.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ exports.interface = function(jasmine: Jasmine, env: any) {
return env.spyOn(obj, methodName);
},

spyOnProperty: function (obj: Object, methodName: string, accessType = 'get') {
spyOnProperty(obj: Object, methodName: string, accessType = 'get') {
return env.spyOnProperty(obj, methodName, accessType);
},

Expand Down
18 changes: 12 additions & 6 deletions packages/jest-jasmine2/src/jasmine/spy_registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ export default function SpyRegistry(options: Object) {

this.spyOnProperty = function(obj, propertyName, accessType = 'get') {
if (!obj) {
throw new Error('spyOn could not find an object to spy upon for ' + propertyName + '');
throw new Error(
'spyOn could not find an object to spy upon for ' + propertyName + '',
);
}

if (!propertyName) {
Expand All @@ -154,7 +156,9 @@ export default function SpyRegistry(options: Object) {
}

if (!descriptor[accessType]) {
throw new Error('Property ' + propertyName + ' does not have access type ' + accessType);
throw new Error(
'Property ' + propertyName + ' does not have access type ' + accessType,
);
}

if (obj[propertyName] && isSpy(obj[propertyName])) {
Expand All @@ -172,20 +176,22 @@ export default function SpyRegistry(options: Object) {
let restoreStrategy;

if (Object.prototype.hasOwnProperty.call(obj, propertyName)) {
restoreStrategy = function () {
restoreStrategy = function() {
Object.defineProperty(obj, propertyName, originalDescriptor);
};
} else {
restoreStrategy = function () {
restoreStrategy = function() {
delete obj[propertyName];
};
}

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

const spiedDescriptor = Object.assign({}, descriptor, { [accessType]: spiedProperty })
const spiedDescriptor = Object.assign({}, descriptor, {
[accessType]: spiedProperty,
});

Object.defineProperty(obj, propertyName, spiedDescriptor);

Expand Down
29 changes: 14 additions & 15 deletions packages/jest-mock/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -691,27 +691,24 @@ class ModuleMockerClass {
return object[methodName];
}

spyOnProperty(object: any, propertyName: any, accessType = 'get'): any {
if (typeof object !== 'object' && typeof object !== 'function') {
spyOnProperty(obj: any, propertyName: any, accessType: string = 'get'): any {
if (typeof obj !== 'object' && typeof obj !== 'function') {
throw new Error(
'Cannot spyOn on a primitive value; ' + this._typeOf(object) + ' given',
'Cannot spyOn on a primitive value; ' + this._typeOf(obj) + ' given',
);
}

if (!obj) {
throw new Error('spyOn could not find an object to spy upon for ' + propertyName + '');
throw new Error(
'spyOn could not find an object to spy upon for ' + propertyName + '',
);
}

if (!propertyName) {
throw new Error('No property name supplied');
}

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

if (!descriptor) {
throw new Error(propertyName + ' property does not exist');
Expand All @@ -722,27 +719,29 @@ class ModuleMockerClass {
}

if (!descriptor[accessType]) {
throw new Error('Property ' + propertyName + ' does not have access type ' + accessType);
throw new Error(
'Property ' + propertyName + ' does not have access type ' + accessType,
);
}

const original = descriptor[accessType]
const original = descriptor[accessType];

if (!this.isMockFunction(original)) {
if (typeof original !== 'function') {
throw new Error(
'Cannot spy the ' +
methodName +
propertyName +
' property because it is not a function; ' +
this._typeOf(original) +
' given instead',
);
}

descriptor[accessType] = this._makeComponent({ type: 'function' }, () => {
descriptor[accessType] = this._makeComponent({type: 'function'}, () => {
descriptor[accessType] = original;
});

descriptor[accessType].mockImplementation(function () {
descriptor[accessType].mockImplementation(function() {
return original.apply(this, arguments);
});
}
Expand Down
4 changes: 3 additions & 1 deletion packages/jest-runtime/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,9 @@ class Runtime {
};
const fn = this._moduleMocker.fn.bind(this._moduleMocker);
const spyOn = this._moduleMocker.spyOn.bind(this._moduleMocker);
const spyOnProperty = this._moduleMocker.spyOnProperty.bind(this._moduleMocker);
const spyOnProperty = this._moduleMocker.spyOnProperty.bind(
this._moduleMocker,
);

const setTimeout = (timeout: number) => {
this._environment.global.jasmine
Expand Down
6 changes: 5 additions & 1 deletion types/Jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ export type Jest = {|
setMock(moduleName: string, moduleExports: any): Jest,
setTimeout(timeout: number): Jest,
spyOn(object: Object, methodName: string): JestMockFn,
spyOnProperty(object: Object, methodName: string, accessType: string): JestMockFn,
spyOnProperty(
object: Object,
methodName: string,
accessType: string,
): JestMockFn,
unmock(moduleName: string): Jest,
useFakeTimers(): Jest,
useRealTimers(): Jest,
Expand Down

0 comments on commit 65d884d

Please sign in to comment.