-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(jest-mock): improve user input validation and error messages of spyOn
and replaceProperty
methods
#14087
Merged
+287
−84
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
fix(jest-mock): improve user input validation and error messages of `…
…spyOn` and `replaceProperty` methods
commit 922aff2eebecfbb5778d7e5cd3b70be50d4de876
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1304,20 +1304,164 @@ describe('moduleMocker', () => { | |
expect(spy).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should throw on invalid input', () => { | ||
expect(() => { | ||
moduleMocker.spyOn(null, 'method'); | ||
}).toThrow('spyOn could not find an object to spy on for method'); | ||
expect(() => { | ||
moduleMocker.spyOn({}, 'method'); | ||
}).toThrow( | ||
"Cannot spy on the method property because it is not a function; undefined given instead. If you are trying to mock a property, use `jest.replaceProperty(object, 'method', value)` instead.", | ||
); | ||
expect(() => { | ||
moduleMocker.spyOn({method: 10}, 'method'); | ||
}).toThrow( | ||
"Cannot spy on the method property because it is not a function; number given instead. If you are trying to mock a property, use `jest.replaceProperty(object, 'method', value)` instead.", | ||
describe('should throw', () => { | ||
it.each` | ||
value | type | ||
${'foo'} | ${'string'} | ||
${1} | ${'number'} | ||
${NaN} | ${'number'} | ||
${1n} | ${'bigint'} | ||
${Symbol()} | ${'symbol'} | ||
${true} | ${'boolean'} | ||
${false} | ${'boolean'} | ||
${undefined} | ${'undefined'} | ||
${null} | ${'null'} | ||
`( | ||
'when primitive value $value is provided instead of an object', | ||
({value, type}) => { | ||
expect(() => { | ||
moduleMocker.spyOn(value, 'method'); | ||
}).toThrow(`Cannot use spyOn on a primitive value; ${type} given`); | ||
}, | ||
); | ||
|
||
it('when property name is not provided', () => { | ||
expect(() => { | ||
moduleMocker.spyOn({}, null); | ||
}).toThrow('No property name supplied'); | ||
}); | ||
|
||
it('when property does not exist', () => { | ||
expect(() => { | ||
moduleMocker.spyOn({}, 'doesNotExist'); | ||
}).toThrow( | ||
'Property `doesNotExist` does not exist in the provided object', | ||
); | ||
}); | ||
|
||
it('when getter does not exist', () => { | ||
expect(() => { | ||
moduleMocker.spyOn({}, 'missingGet', 'get'); | ||
}).toThrow( | ||
'Property `missingGet` does not exist in the provided object', | ||
); | ||
}); | ||
|
||
it('when setter does not exist', () => { | ||
expect(() => { | ||
moduleMocker.spyOn({}, 'missingSet', 'set'); | ||
}).toThrow( | ||
'Property `missingSet` does not exist in the provided object', | ||
); | ||
}); | ||
|
||
it('when getter is not configurable', () => { | ||
expect(() => { | ||
const obj = {}; | ||
|
||
Object.defineProperty(obj, 'property', { | ||
configurable: false, | ||
get() { | ||
return 1; | ||
}, | ||
}); | ||
|
||
moduleMocker.spyOn(obj, 'property', 'get'); | ||
}).toThrow('Property `property` is not declared configurable'); | ||
}); | ||
|
||
it('when setter is not configurable', () => { | ||
expect(() => { | ||
const obj = {}; | ||
let value = 38; | ||
|
||
Object.defineProperty(obj, 'property', { | ||
configurable: false, | ||
get() { | ||
return value; | ||
}, | ||
set(newValue) { | ||
value = newValue; | ||
}, | ||
}); | ||
|
||
moduleMocker.spyOn(obj, 'property', 'set'); | ||
}).toThrow('Property `property` is not declared configurable'); | ||
}); | ||
|
||
it('when property does not have access type get', () => { | ||
expect(() => { | ||
const obj = {}; | ||
let value = 38; | ||
|
||
// eslint-disable-next-line accessor-pairs | ||
Object.defineProperty(obj, 'property', { | ||
configurable: true, | ||
set(newValue) { | ||
value = newValue; | ||
}, | ||
}); | ||
|
||
moduleMocker.spyOn(obj, 'property', 'get'); | ||
}).toThrow('Property `property` does not have access type get'); | ||
}); | ||
|
||
it('when property does not have access type set', () => { | ||
expect(() => { | ||
const obj = {}; | ||
|
||
Object.defineProperty(obj, 'property', { | ||
configurable: true, | ||
get() { | ||
return 1; | ||
}, | ||
}); | ||
|
||
moduleMocker.spyOn(obj, 'property', 'set'); | ||
}).toThrow('Property `property` does not have access type set'); | ||
}); | ||
|
||
it('when trying to spy on a non function property', () => { | ||
expect(() => { | ||
moduleMocker.spyOn({property: 123}, 'property'); | ||
}).toThrow( | ||
"Cannot spy on the `property` property because it is not a function; number given instead. If you are trying to mock a property, use `jest.replaceProperty(object, 'property', value)` instead.", | ||
); | ||
}); | ||
}); | ||
|
||
it('supports spying on a method named `0`', () => { | ||
let haveBeenCalled = false; | ||
const obj = { | ||
0: () => { | ||
haveBeenCalled = true; | ||
}, | ||
}; | ||
|
||
const spy = moduleMocker.spyOn(obj, 0); | ||
obj[0].call(null); | ||
|
||
expect(haveBeenCalled).toBe(true); | ||
expect(spy).toHaveBeenCalled(); | ||
}); | ||
|
||
it('supports spying on a method which is defined on a function', () => { | ||
let haveBeenCalled = false; | ||
const obj = () => true; | ||
|
||
Object.defineProperty(obj, 'method', { | ||
configurable: true, | ||
value: () => { | ||
haveBeenCalled = true; | ||
}, | ||
writable: true, | ||
}); | ||
|
||
const spy = moduleMocker.spyOn(obj, 'method'); | ||
obj['method'].call(null); | ||
|
||
expect(haveBeenCalled).toBe(true); | ||
expect(spy).toHaveBeenCalled(); | ||
}); | ||
|
||
it('supports clearing a spy', () => { | ||
|
@@ -1642,16 +1786,14 @@ describe('moduleMocker', () => { | |
it('should throw on invalid input', () => { | ||
expect(() => { | ||
moduleMocker.spyOn(null, 'method'); | ||
}).toThrow('spyOn could not find an object to spy on for method'); | ||
}).toThrow('Cannot use spyOn on a primitive value; null given'); | ||
expect(() => { | ||
moduleMocker.spyOn({}, 'method'); | ||
}).toThrow( | ||
"Cannot spy on the method property because it is not a function; undefined given instead. If you are trying to mock a property, use `jest.replaceProperty(object, 'method', value)` instead.", | ||
); | ||
}).toThrow('Property `method` does not exist in the provided object'); | ||
Comment on lines
1808
to
+1809
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm.. It does not exist simply. |
||
expect(() => { | ||
moduleMocker.spyOn({method: 10}, 'method'); | ||
}).toThrow( | ||
"Cannot spy on the method property because it is not a function; number given instead. If you are trying to mock a property, use `jest.replaceProperty(object, 'method', value)` instead.", | ||
"Cannot spy on the `method` property because it is not a function; number given instead. If you are trying to mock a property, use `jest.replaceProperty(object, 'method', value)` instead.", | ||
); | ||
}); | ||
|
||
|
@@ -2018,34 +2160,23 @@ describe('moduleMocker', () => { | |
|
||
describe('should throw', () => { | ||
it.each` | ||
value | ||
${null} | ||
${undefined} | ||
`('when $value is provided instead of an object', ({value}) => { | ||
expect(() => { | ||
moduleMocker.replaceProperty(value, 'property', 1); | ||
}).toThrow( | ||
'replaceProperty could not find an object on which to replace property', | ||
); | ||
}); | ||
|
||
it.each` | ||
value | type | ||
${'foo'} | ${'string'} | ||
${1} | ${'number'} | ||
${NaN} | ${'number'} | ||
${1n} | ${'bigint'} | ||
${Symbol()} | ${'symbol'} | ||
${true} | ${'boolean'} | ||
${false} | ${'boolean'} | ||
${() => {}} | ${'function'} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A function can have properties. Removed this case and moved |
||
value | type | ||
${'foo'} | ${'string'} | ||
${1} | ${'number'} | ||
${NaN} | ${'number'} | ||
${1n} | ${'bigint'} | ||
${Symbol()} | ${'symbol'} | ||
${true} | ${'boolean'} | ||
${false} | ${'boolean'} | ||
${undefined} | ${'undefined'} | ||
${null} | ${'null'} | ||
`( | ||
'when primitive value $value is provided instead of an object', | ||
({value, type}) => { | ||
expect(() => { | ||
moduleMocker.replaceProperty(value, 'property', 1); | ||
}).toThrow( | ||
`Cannot mock property on a non-object value; ${type} given`, | ||
`Cannot use replaceProperty on a primitive value; ${type} given`, | ||
); | ||
}, | ||
); | ||
|
@@ -2056,10 +2187,12 @@ describe('moduleMocker', () => { | |
}).toThrow('No property name supplied'); | ||
}); | ||
|
||
it('when property is not defined', () => { | ||
it('when property does not exist', () => { | ||
expect(() => { | ||
moduleMocker.replaceProperty({}, 'doesNotExist', 1); | ||
}).toThrow('doesNotExist property does not exist'); | ||
}).toThrow( | ||
'Property `doesNotExist` does not exist in the provided object', | ||
); | ||
}); | ||
|
||
it('when property is not configurable', () => { | ||
|
@@ -2073,18 +2206,18 @@ describe('moduleMocker', () => { | |
}); | ||
|
||
moduleMocker.replaceProperty(obj, 'property', 2); | ||
}).toThrow('property is not declared configurable'); | ||
}).toThrow('Property `property` is not declared configurable'); | ||
}); | ||
|
||
it('when trying to mock a method', () => { | ||
it('when trying to replace a method', () => { | ||
expect(() => { | ||
moduleMocker.replaceProperty({method: () => {}}, 'method', () => {}); | ||
}).toThrow( | ||
"Cannot mock the method property because it is a function. Use `jest.spyOn(object, 'method')` instead.", | ||
"Cannot replace the `method` property because it is a function. Use `jest.spyOn(object, 'method')` instead.", | ||
); | ||
}); | ||
|
||
it('when mocking a getter', () => { | ||
it('when trying to replace a getter', () => { | ||
const obj = { | ||
get getter() { | ||
return 1; | ||
|
@@ -2093,21 +2226,49 @@ describe('moduleMocker', () => { | |
|
||
expect(() => { | ||
moduleMocker.replaceProperty(obj, 'getter', 1); | ||
}).toThrow('Cannot mock the getter property because it has a getter'); | ||
}).toThrow( | ||
'Cannot replace the `getter` property because it has a getter', | ||
); | ||
}); | ||
|
||
it('when mocking a setter', () => { | ||
it('when trying to replace a setter', () => { | ||
const obj = { | ||
// eslint-disable-next-line accessor-pairs | ||
set setter(_value: number) {}, | ||
}; | ||
|
||
expect(() => { | ||
moduleMocker.replaceProperty(obj, 'setter', 1); | ||
}).toThrow('Cannot mock the setter property because it has a setter'); | ||
}).toThrow( | ||
'Cannot replace the `setter` property because it has a setter', | ||
); | ||
}); | ||
}); | ||
|
||
it('supports replacing a property named `0`', () => { | ||
const obj = { | ||
0: 'zero', | ||
}; | ||
|
||
moduleMocker.replaceProperty(obj, 0, 'null'); | ||
|
||
expect(obj[0]).toBe('null'); | ||
}); | ||
|
||
it('supports replacing a property which is defined on a function', () => { | ||
const obj = () => true; | ||
|
||
Object.defineProperty(obj, 'property', { | ||
configurable: true, | ||
value: 'abc', | ||
writable: true, | ||
}); | ||
|
||
moduleMocker.replaceProperty(obj, 'property', 'def'); | ||
|
||
expect(obj['property']).toBe('def'); | ||
}); | ||
|
||
it('should work for property from prototype chain', () => { | ||
const parent = {property: 'abcd'}; | ||
const child = Object.create(parent); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
null
should be treated as primitive. Or?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, I think that's fine