Skip to content

Add spyOnAll function#1581

Merged
slackersoft merged 1 commit intojasmine:masterfrom
aeisenberg:spy-all
Jul 25, 2018
Merged

Add spyOnAll function#1581
slackersoft merged 1 commit intojasmine:masterfrom
aeisenberg:spy-all

Conversation

@aeisenberg
Copy link
Copy Markdown
Contributor

@aeisenberg aeisenberg commented Jul 19, 2018

Description

This function will spy on all writable and configurable properties of
an object that is passed in. It can be used like this:

spyOnAll(obj);

Motivation and Context

This commit addresses #1421

How Has This Been Tested?

I have included specs for spyOnAll. I have run the tests in the browser through bundle exec rake jasmine. The browsers I tested are chrome, firefox, safari, and vivaldi (latest versions of each).

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

I am not sure how to update the documentation. After I submit this PR, I will figure this out.

@aeisenberg
Copy link
Copy Markdown
Contributor Author

I have added proper jsdoc. And it looks like that is all I need to do to update the documentation. For that reason, I am going to check that box in the PR checklist above.

@adriangodong
Copy link
Copy Markdown

How do I access the spy on the spied property?

@aeisenberg
Copy link
Copy Markdown
Contributor Author

It would be something like this:

let obj = { a: () => true };
spyOnAll(obj);
obj.a();
expect(obj.a).toHaveBeenCalled();

Or maybe I'm misunderstanding your question.

@adriangodong
Copy link
Copy Markdown

That’s for a function. How about for a property? If ‘a’ is a property, how do we differentiate between getting the property value vs. the spy?

@aeisenberg
Copy link
Copy Markdown
Contributor Author

That's a good question. I have never used spies on non-functions before. Perhaps I should restrict spyOnAll to functions only. (and perhaps rename to spyOnAllFunctions)

And then there could be an equivalent spyOnAllProperties to be implemented at a later date.

@slackersoft
Copy link
Copy Markdown
Member

I haven’t had a chance to review this in any more detail, but let’s either update the name or add the property spying (or both so there is spyOnAll, spyOnAllFunctions, and spyOnAllProperties or something)

@aeisenberg
Copy link
Copy Markdown
Contributor Author

aeisenberg commented Jul 20, 2018 via email

This function will spy on all writable and configurable functionss of
an object that is passed in. It can be used like this:

    spyOnAllFunctions(obj);

This commit addresses jasmine#1421
@aeisenberg
Copy link
Copy Markdown
Contributor Author

Updated the commit so that it provides spyOnAllFunctions. Do you want me to update the PR text as well? If and when this goes through, I'll add spyOnAllProperties.

@slackersoft slackersoft merged commit f62eb3b into jasmine:master Jul 25, 2018
slackersoft pushed a commit that referenced this pull request Jul 25, 2018
@steenburgh
Copy link
Copy Markdown

steenburgh commented Aug 14, 2018

I don't know a lot about developing in this repo, but I noticed that I cannot use spyOnAllFunctions without first constructing a registry. Was that intended behavior? Currently,

it("does a thing", function() {
    spyOnAllFunctions(someThingToSpyOn);
});

throws env.spyOnAllFunctions is not a function.

Adding the following to Env.js seems to fix my problem.

this.spyOnAllFunctions = {
  return spyRegistry.spyOnAllFunctions.apply(spyRegistry, arguments);
}

slackersoft pushed a commit that referenced this pull request Aug 15, 2018
@slackersoft
Copy link
Copy Markdown
Member

You're right, it looks like a bit of the wiring got missed for this. I'm releasing a 3.2.1 which should work properly out of the box.

@dougbacelar
Copy link
Copy Markdown

+1 !

Took me a long time to find this PR, excited for this.

Unfortunately, I am not currently on the latest version, but very glad with the addition.

@FrancescoBorzi
Copy link
Copy Markdown

is there a way to use this with classes instead of objects?

@aeisenberg
Copy link
Copy Markdown
Contributor Author

aeisenberg commented Nov 15, 2018 via email

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 this pull request may close these issues.

6 participants