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.

spyOn getter only works for static getters and not for instance getters

See original GitHub issue

🐛 Bug Report

If I try to spy on a getter with

spyOn(instance,‘foo’,‘get’).and.returnValue(false);

I get the error

 <spyOn> : foo property does not exist
    Usage: spyOn(<object>, <methodName>)

      22 |     var instance = new MyClass();
      23 | 
    > 24 |     spyOn(instance,'foo','get').and.returnValue(false);
         |     ^
      25 |     expect(instance.foo).toBe(false);
      26 | 
      27 | });

      at SpyRegistry._spyOnProperty (node_modules/jest-jasmine2/build/jasmine/spyRegistry.js:191:15)
      at Object.<anonymous> (test/tools/wizard/demo.test.js:24:5)

To Reproduce

Here are two examples, one for a static property and one for an instance property. The first test works. The second fails.

it('static getter', ()=>{

    class MyClass {
        static get foo(){
            return true;
        }
    }

    spyOn(MyClass,'foo','get').and.returnValue(false);
    expect(MyClass.foo).toBe(false);

});

it('non-static getter', ()=>{

    class MyClass {
        get foo(){
            return true;
        }
    }

    var instance = new MyClass();

    spyOn(instance,'foo','get').and.returnValue(false);
    expect(instance.foo).toBe(false);

});

My babel-setting inside package.json are:

"babel": {
    "presets": [
      [
        "@babel/preset-env",
        {
          "targets": {
            "node": "current"
          }
        }
      ],
      "@babel/preset-react"
    ]
  },

Expected behavior

The instance property “foo” should be found and the second test should work, too.

envinfo


npx: installed 1 in 2.149s

  System:
    OS: Windows 10 10.0.17763
    CPU: (8) x64 Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz
  Binaries:
    Node: 12.13.1 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.0 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
    npm: 6.12.1 - C:\Program Files\nodejs\npm.CMD
  npmPackages:
    jest: 25.1.0 => 25.1.0

related

https://github.com/facebook/jest/issues/8137 https://github.com/facebook/jest/issues/5746 https://jestjs.io/docs/en/jest-object.html#jestspyonobject-methodname-accesstype

Workaround

Inststead of

spyOn(instance,'foo','get').and.returnValue(false);

I am able to use

Object.defineProperty(instance, 'foo', { get(){ return false; } });

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:20
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
lennerdcommented, Dec 5, 2020

Quite unpleasant bug when working with Mobx and TypeScript. I’m currently working around this by setting the property directly and ignoring the typescript error for now:

// @ts-ignore
instance.foo = false;
3reactions
adnan-razzaqcommented, Dec 4, 2020

any progress on it? This workaround seems not to work for me

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to spy on a property (getter or setter) with Jasmine
Here we have the ES6 class syntax, with a getter property. The get fullName() syntax binds an object property fullName to a function...
Read more >
Mock static getter with jest - node.js - Stack Overflow
I'm trying to mock a logging service that have static getters. static get ...
Read more >
Jest Automock Doesn't Mock Getters - Bambielli's Blog
TIL that Jest module automocking does not mock the getter / setter ... it is now possible to spy on getters and setters...
Read more >
Spying on properties - Jasmine Documentation
Changing the value of an existing spy is more difficult than with a function, because you cannot refer to a property without calling...
Read more >
How to test classes with Jest - DEV Community ‍ ‍
To test class implementation using spies with Jest we use the jest.spyOn() function and spy on all methods in the class that take...
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