From e9147a6f3706c5e3ca3635c7a173ed47b5487382 Mon Sep 17 00:00:00 2001 From: phra Date: Mon, 18 Dec 2017 21:24:50 +0100 Subject: [PATCH] test: fix failing tests for spyOnProperty --- flow-typed/npm/jest_v21.x.x.js | 2 +- packages/jest-jasmine2/src/index.js | 2 +- .../src/jasmine/jasmine_light.js | 2 +- .../jest-jasmine2/src/jasmine/spy_registry.js | 18 ++++++++---- packages/jest-mock/src/index.js | 29 +++++++++---------- packages/jest-runtime/src/index.js | 4 ++- types/Jest.js | 6 +++- 7 files changed, 37 insertions(+), 26 deletions(-) diff --git a/flow-typed/npm/jest_v21.x.x.js b/flow-typed/npm/jest_v21.x.x.js index 8dc63523cd46..ceb0799324c1 100644 --- a/flow-typed/npm/jest_v21.x.x.js +++ b/flow-typed/npm/jest_v21.x.x.js @@ -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; diff --git a/packages/jest-jasmine2/src/index.js b/packages/jest-jasmine2/src/index.js index b8bb08448b96..630055ada8ed 100644 --- a/packages/jest-jasmine2/src/index.js +++ b/packages/jest-jasmine2/src/index.js @@ -161,7 +161,7 @@ const addSnapshotData = (results, snapshotState) => { }); const uncheckedCount = snapshotState.getUncheckedCount(); - let uncheckedKeys + let uncheckedKeys; if (uncheckedCount) { uncheckedKeys = snapshotState.getUncheckedKeys(); diff --git a/packages/jest-jasmine2/src/jasmine/jasmine_light.js b/packages/jest-jasmine2/src/jasmine/jasmine_light.js index b9ebb0c1b28f..81a397708865 100644 --- a/packages/jest-jasmine2/src/jasmine/jasmine_light.js +++ b/packages/jest-jasmine2/src/jasmine/jasmine_light.js @@ -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); }, diff --git a/packages/jest-jasmine2/src/jasmine/spy_registry.js b/packages/jest-jasmine2/src/jasmine/spy_registry.js index 74ee51265c13..b1a3ce600edb 100644 --- a/packages/jest-jasmine2/src/jasmine/spy_registry.js +++ b/packages/jest-jasmine2/src/jasmine/spy_registry.js @@ -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) { @@ -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])) { @@ -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); diff --git a/packages/jest-mock/src/index.js b/packages/jest-mock/src/index.js index 4e490bc9b216..cc0f3d5a854e 100644 --- a/packages/jest-mock/src/index.js +++ b/packages/jest-mock/src/index.js @@ -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'); @@ -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); }); } diff --git a/packages/jest-runtime/src/index.js b/packages/jest-runtime/src/index.js index 38cc5cd7f43a..8058d9fa0439 100644 --- a/packages/jest-runtime/src/index.js +++ b/packages/jest-runtime/src/index.js @@ -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 diff --git a/types/Jest.js b/types/Jest.js index 6007812c1dc3..f22bc79ec167 100644 --- a/types/Jest.js +++ b/types/Jest.js @@ -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,