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

Leaking state in program scope #446

Closed
MichalBryxi opened this issue Jul 3, 2019 · 2 comments · May be fixed by #447
Closed

Leaking state in program scope #446

MichalBryxi opened this issue Jul 3, 2019 · 2 comments · May be fixed by #447

Comments

@MichalBryxi
Copy link

Consider following service:

var cachedFoo = null;

export default Service.extend({
  fetchFoo(count) {
    if(!cachedFoo) {
      fetch(`./api/?results=${count}`).then(function(response) {
        cachedFoo = response;
      });
    }
  }

  getFoo() {
    return cachedFoo;
  }
});

This service will probably work as expected in the scope of running Ember code.

But if there are two tests calling this service with different value for count when calling fetchFoo, then calls to getFoo will always return the response of the first test.

@MichalBryxi
Copy link
Author

This just bit us in our test code, so I thought an eslint rule might be a good idea.

I know that just enforcing consts is not bullet-proof as the content of an object can be still mutated, but forcing user to use const instead of var or let might give them a hint that something might be wrong.

  • This is my first eslint rule, so not really sure what I might have missed.
  • Also not sure whether there might be files that actually can have global mutable variables without compromising the tests.

@rajasegar
Copy link
Contributor

@MichalBryxi FYI
https://eslint.org/docs/rules/prefer-const
https://eslint.org/docs/rules/no-var

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants