jest.spyOn(module, 'method', 'get') failed as method not declared configurable
See original GitHub issueThis seems to be an edge case around mocking or spyOn a module built with Babel, while it may be related to babel module export settings, but would like to get some insights here first to see if there’s anything I’ve missed or anything we can do on Jest side.
deps versions:
"babel-cli": "^6.26.0",
"babel-core": "^6.26.3",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-preset-env": "^1.7.0",
"jest": "^23.5.0",
Had a depedency dep-package
built with babel with the following .babelrc:
{
"presets": [
[
"env", { "targets": { "node": "6.1.0" } }
]
],
"plugins": [
"transform-object-rest-spread"
]
}
in test.js
import * as depPackage from 'dep-package';
jest.mock('dep-package');
module mocks would work
import * as depPackage from 'dep-package';
depPackage.compose = jest.fn();
import * as depPackage from 'dep-package';
jest.spyOn(depPackage, 'compose');
this would throw error TypeError: Cannot set property compose of [object Object] which has only a getter
import * as depPackage from 'dep-package';
jest.spyOn(depPackage, 'compose', 'get');
this would throw error compose is not declared configurable
Not sure what is causing the difference between module mock with jest.mock() and manual mock =jest.fn() here?
Issue Analytics
- State:
- Created 5 years ago
- Reactions:24
- Comments:20
Top Results From Across the Web
Error: <spyOnProperty> : function is not declared configurable
I would like to create spy on function of the FocusService. This service has only one method called myFunction. Only thing I want...
Read more >jest cannot spyon primitive value - You.com | The AI Search ...
The error means, the function sampleMethod you defined inside the functional component SampleComponent is not defined in SampleComponent.prototype .
Read more >The Jest Object
The methods in the jest object help create mocks and let you control Jest's overall ... Disables automatic mocking in the module loader....
Read more >How To Spy On An Exported Function In Jest - Chak Shun Yu
This article shows you how to spy on an exported function, imported function, named export, or named import in Jest.
Read more >How to use Jest spyOn with React.js and Fetch - Meticulous
Follow this step-by-step guide on Jest's SpyOn method, with a practical example using React and Fetch. This post also includes a comparison ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@zhenyulin give a try with this,
jest.mock('dep-package', () => ({ ...jest.requireActual('dep-package'), })); const cwpDetectionAgency = require('dep-package');
instead of doing
import * as depPackage from 'dep-package';
This has helped me, any thing which is out side of the current code base we have to do this.
Finally I got this. It’s an unrobust, but working solution, that monkey-patches
Object.defineProperty
in the setup file.In
setupTests.js
:In tests helpers:
in tests:
May be it will help someone. Also this
unfreezeImport
method can be united with the patcheddefineProperty
insetupTests.js
file.