question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

jest.spyOn(module, 'method', 'get') failed as method not declared configurable

See original GitHub issue

This 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:closed
  • Created 5 years ago
  • Reactions:24
  • Comments:20

github_iconTop GitHub Comments

18reactions
AnilGayakwadcommented, Apr 3, 2019

@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.

11reactions
faiwercommented, Jul 7, 2020

Finally I got this. It’s an unrobust, but working solution, that monkey-patches Object.defineProperty in the setup file.

In setupTests.js:

const { defineProperty } = Object;
Object.defineProperty = function(object, name, meta) {
  if (meta.get && !meta.configurable) {
    // it might be an ES6 exports object
    return defineProperty(object, name, {
      ...meta,
      configurable: true, // prevent freezing
    });
  }

  return defineProperty(object, name, meta);
};

In tests helpers:

export const unfreezeImport = <T,>(module: T, key: keyof T): void => {
  const meta = orDie(Object.getOwnPropertyDescriptor(module, key));
  const getter = orDie(meta.get);

  const originalValue = getter() as T[typeof key];
  let currentValue = originalValue;
  let isMocked = false;

  Object.defineProperty(module, key, {
    ...meta,
    get: () => (isMocked ? currentValue : getter()),
    set(newValue: T[typeof key]) {
      isMocked = newValue !== originalValue;
      currentValue = newValue;
    },
  });
};

in tests:

import * as someModule from 'someModule';

unfreezeImport(someModule, 'someProperty');

it('someTest', () => {
  jest.spyOn(someModule, 'someProperty').mockImplementation(...);
  // test code
});

May be it will help someone. Also this unfreezeImport method can be united with the patched defineProperty in setupTests.js file.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found