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

automocking ignores arrow-functions in ES6 classes #6065

Closed
vonovak opened this issue Apr 25, 2018 · 3 comments
Closed

automocking ignores arrow-functions in ES6 classes #6065

vonovak opened this issue Apr 25, 2018 · 3 comments

Comments

@vonovak
Copy link
Contributor

vonovak commented Apr 25, 2018

This is something between a question and feature request:

I was checking out the docs for es6 class mocks. The example uses the following code:

// sound-player-consumer.js
import SoundPlayer from './sound-player';

export default class SoundPlayerConsumer {
  constructor() {
    this.soundPlayer = new SoundPlayer();
  }

  playSomethingCool() {
    const coolSoundFileName = 'song.mp3';
    this.soundPlayer.playSoundFile(coolSoundFileName);
  }
}

If I create an automock of such module, the playSomethingCool function will be mocked. However, if I change it to an arrow function (which I would wanna do because of the behavior of this), the mock won't contain the playSomethingCool function. I am aware of the fact that the arrow function is not present on the object's prototype which is probably why the behavior is different.

I would like to ask if this is the intended behavior and if it would make sense to create mocks also for the arrow functions. I feel like this is something that should be covered in the docs, which I wanted to do a PR for but I figured I would first ask here.

Thanks

running jest 22.4

@mjesun
Copy link
Contributor

mjesun commented Apr 26, 2018

Yep, Jest can only mock the structure of objects that are present at require time. It does it by reflection (not by analysis), which means that properties that get added by the constructor cannot be mocked. It's important to understand though that a fat-arrow assignment in a class in JS is not a class method; it's a class property holding a reference to a function.

My suggestion to that would be that you change the fat arrow assignments to standard methods and bind them in the constructor. That way they will be Jest aware. Otherwise I'm afraid there's not much we can do here.

I'm happy to take a PR with some improvements to the documentation, though 🙂

@vonovak
Copy link
Contributor Author

vonovak commented Apr 26, 2018

thanks @mjesun that's what I was thinking. I'll ship the PR soon.

@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 12, 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