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

Mocking static class methods #1245

Closed
samzhang111 opened this issue Jul 5, 2016 · 4 comments
Closed

Mocking static class methods #1245

samzhang111 opened this issue Jul 5, 2016 · 4 comments

Comments

@samzhang111
Copy link

I ran into an issue trying to mock a static class method in Jest. Is this behavior supported?

Cross-posted at http://stackoverflow.com/questions/38206478/mocking-methods-in-jest

I have a very simple unit test that looks like this:

import ApiWrapper from '../../services/api_wrapper';
jest.unmock('../helper')

describe('Helper', () => {
    let Helper;

    beforeEach(() => {
        Helper = require('../helper').default;
    });

    it('calls the Api Wrapper', () => {
        Helper.help()

        expect(ApiWrapper.help).toHaveBeenCalled();
    });

});

Where Helper looks like this:

import ApiWrapper from '../services/api_wrapper'
class Helper {
    help() {
        ApiWrapper.help()
    }
}

export default new Helper();

And ApiWrapper looks like this:

class ApiWrapper {
  static help () {
     console.log('help!')
  }
}
export default ApiWrapper;

ApiWrapper.help() gets mocked by Jest so 'help!' never gets printed, yet the expectation in the test fails. This still fails if we rewrite ApiWrapper to just be a plain Javascript object like such:

export default {
    help: () => { console.log('help!'); }
}

It works, however, if we change the imports in the spec (so ApiWrapper is imported in the beforeEach), and rewrite ApiWrapper to be a Singleton class, like so:

class ApiWrapper {
   help() {
      console.log('help!');
   }
}();

export default new ApiWrapper();

What is it about Jest's mocking behavior that makes this happen?

@cpojer
Copy link
Member

cpojer commented Jul 6, 2016

The correct assertion should be toBeCalled. Can you try that?

@samzhang111
Copy link
Author

Sorry, that's what I meant. I typed the example in Stackoverflow directly.

@cpojer
Copy link
Member

cpojer commented Jul 6, 2016

Jest resets the module registry on every call to it. You are requiring ApiWrapper on the top and Helper inside beforeEach, so Helper likely receives a different copy of ApiWrapper. To fix it you can:

  • Import both classes on the top.
  • Require both classes in beforeEach

Simplified explanation:

var A = require('A');
jest.resetModuleRegistry();
A !== require('A'); // true, A is not the exact same module as before we reset the registry.

Let me know if that doesn't fix your problem.

@cpojer cpojer closed this as completed Jul 6, 2016
@github-actions
Copy link

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 May 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants