Skip to content

Commit e9147a6

Browse files
committed
test: fix failing tests for spyOnProperty
1 parent 9c751da commit e9147a6

File tree

7 files changed

+37
-26
lines changed

7 files changed

+37
-26
lines changed

flow-typed/npm/jest_v21.x.x.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ declare var expect: {
555555
// TODO handle return type
556556
// http://jasmine.github.io/2.4/introduction.html#section-Spies
557557
declare function spyOn(value: mixed, method: string): Object;
558-
declare function spyOnProperty(value: mixed, propertyName: string, accessType: 'get' | 'set'): Object;
558+
declare function spyOnProperty(value: mixed, propertyName: string, accessType: string): Object;
559559

560560
/** Holds all functions related to manipulating test runner */
561561
declare var jest: JestObjectType;

packages/jest-jasmine2/src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ const addSnapshotData = (results, snapshotState) => {
161161
});
162162

163163
const uncheckedCount = snapshotState.getUncheckedCount();
164-
let uncheckedKeys
164+
let uncheckedKeys;
165165

166166
if (uncheckedCount) {
167167
uncheckedKeys = snapshotState.getUncheckedKeys();

packages/jest-jasmine2/src/jasmine/jasmine_light.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ exports.interface = function(jasmine: Jasmine, env: any) {
120120
return env.spyOn(obj, methodName);
121121
},
122122

123-
spyOnProperty: function (obj: Object, methodName: string, accessType = 'get') {
123+
spyOnProperty(obj: Object, methodName: string, accessType = 'get') {
124124
return env.spyOnProperty(obj, methodName, accessType);
125125
},
126126

packages/jest-jasmine2/src/jasmine/spy_registry.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,9 @@ export default function SpyRegistry(options: Object) {
131131

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

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

156158
if (!descriptor[accessType]) {
157-
throw new Error('Property ' + propertyName + ' does not have access type ' + accessType);
159+
throw new Error(
160+
'Property ' + propertyName + ' does not have access type ' + accessType,
161+
);
158162
}
159163

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

174178
if (Object.prototype.hasOwnProperty.call(obj, propertyName)) {
175-
restoreStrategy = function () {
179+
restoreStrategy = function() {
176180
Object.defineProperty(obj, propertyName, originalDescriptor);
177181
};
178182
} else {
179-
restoreStrategy = function () {
183+
restoreStrategy = function() {
180184
delete obj[propertyName];
181185
};
182186
}
183187

184188
currentSpies().push({
185-
restoreObjectToOriginalState: restoreStrategy
189+
restoreObjectToOriginalState: restoreStrategy,
186190
});
187191

188-
const spiedDescriptor = Object.assign({}, descriptor, { [accessType]: spiedProperty })
192+
const spiedDescriptor = Object.assign({}, descriptor, {
193+
[accessType]: spiedProperty,
194+
});
189195

190196
Object.defineProperty(obj, propertyName, spiedDescriptor);
191197

packages/jest-mock/src/index.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -691,27 +691,24 @@ class ModuleMockerClass {
691691
return object[methodName];
692692
}
693693

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

701701
if (!obj) {
702-
throw new Error('spyOn could not find an object to spy upon for ' + propertyName + '');
702+
throw new Error(
703+
'spyOn could not find an object to spy upon for ' + propertyName + '',
704+
);
703705
}
704706

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

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

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

724721
if (!descriptor[accessType]) {
725-
throw new Error('Property ' + propertyName + ' does not have access type ' + accessType);
722+
throw new Error(
723+
'Property ' + propertyName + ' does not have access type ' + accessType,
724+
);
726725
}
727726

728-
const original = descriptor[accessType]
727+
const original = descriptor[accessType];
729728

730729
if (!this.isMockFunction(original)) {
731730
if (typeof original !== 'function') {
732731
throw new Error(
733732
'Cannot spy the ' +
734-
methodName +
733+
propertyName +
735734
' property because it is not a function; ' +
736735
this._typeOf(original) +
737736
' given instead',
738737
);
739738
}
740739

741-
descriptor[accessType] = this._makeComponent({ type: 'function' }, () => {
740+
descriptor[accessType] = this._makeComponent({type: 'function'}, () => {
742741
descriptor[accessType] = original;
743742
});
744743

745-
descriptor[accessType].mockImplementation(function () {
744+
descriptor[accessType].mockImplementation(function() {
746745
return original.apply(this, arguments);
747746
});
748747
}

packages/jest-runtime/src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,9 @@ class Runtime {
763763
};
764764
const fn = this._moduleMocker.fn.bind(this._moduleMocker);
765765
const spyOn = this._moduleMocker.spyOn.bind(this._moduleMocker);
766-
const spyOnProperty = this._moduleMocker.spyOnProperty.bind(this._moduleMocker);
766+
const spyOnProperty = this._moduleMocker.spyOnProperty.bind(
767+
this._moduleMocker,
768+
);
767769

768770
const setTimeout = (timeout: number) => {
769771
this._environment.global.jasmine

types/Jest.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ export type Jest = {|
4444
setMock(moduleName: string, moduleExports: any): Jest,
4545
setTimeout(timeout: number): Jest,
4646
spyOn(object: Object, methodName: string): JestMockFn,
47-
spyOnProperty(object: Object, methodName: string, accessType: string): JestMockFn,
47+
spyOnProperty(
48+
object: Object,
49+
methodName: string,
50+
accessType: string,
51+
): JestMockFn,
4852
unmock(moduleName: string): Jest,
4953
useFakeTimers(): Jest,
5054
useRealTimers(): Jest,

0 commit comments

Comments
 (0)