Skip to content
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

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

Closed
stefaneidelloth opened this issue Mar 17, 2020 · 9 comments
Closed

Comments

@stefaneidelloth
Copy link

stefaneidelloth commented Mar 17, 2020

🐛 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

#8137
#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; } });

@imhotep
Copy link

imhotep commented Apr 2, 2020

I have this issue as well. The workaround works but it would nice if spyOn also worked for instance properties.

@omgoshjosh
Copy link

dang! it's at least nice when the last comment is less than 2 weeks ago. thanks for the workaround. seems a bit janky though!

@adnan-razzaq
Copy link

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

@lennerd
Copy link

lennerd commented 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;

@staplespeter
Copy link
Contributor

staplespeter commented Aug 6, 2022

This is still broken in Jest@28.1.3, though my issue is with automocked classes not having any getter/setter mocks at all.

opened a new issue #13140

@pahuta
Copy link

pahuta commented Oct 6, 2022

Should be fixed in 29.1.0.
PR #13145

@github-actions
Copy link

github-actions bot commented Oct 6, 2023

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days.

@github-actions github-actions bot added the Stale label Oct 6, 2023
Copy link

github-actions bot commented Nov 5, 2023

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 5, 2023
Copy link

github-actions bot commented Dec 6, 2023

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 6, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

8 participants