{"action":"closed","issue":{"url":"https://api.github.com/repos/facebook/jest/issues/331","labels_url":"https://api.github.com/repos/facebook/jest/issues/331/labels{/name}","comments_url":"https://api.github.com/repos/facebook/jest/issues/331/comments","events_url":"https://api.github.com/repos/facebook/jest/issues/331/events","html_url":"https://github.com/facebook/jest/issues/331","id":68349763,"number":331,"title":"Accessing non-exported mock object from test","user":{"login":"matthewgertner","id":870558,"avatar_url":"https://avatars.githubusercontent.com/u/870558?v=3","gravatar_id":"","url":"https://api.github.com/users/matthewgertner","html_url":"https://github.com/matthewgertner","followers_url":"https://api.github.com/users/matthewgertner/followers","following_url":"https://api.github.com/users/matthewgertner/following{/other_user}","gists_url":"https://api.github.com/users/matthewgertner/gists{/gist_id}","starred_url":"https://api.github.com/users/matthewgertner/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/matthewgertner/subscriptions","organizations_url":"https://api.github.com/users/matthewgertner/orgs","repos_url":"https://api.github.com/users/matthewgertner/repos","events_url":"https://api.github.com/users/matthewgertner/events{/privacy}","received_events_url":"https://api.github.com/users/matthewgertner/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-04-14T11:56:27Z","updated_at":"2015-04-19T18:09:38Z","closed_at":"2015-04-19T18:09:38Z","body":"The module I am testing looks something like this:\r\n\r\n```\r\nvar Foo = require('foo');\r\nvar foo = new Foo();\r\n\r\nvar MyClass = function() {\r\n  // ... implementation does stuff with `foo`\r\n};\r\n\r\nmodule.exports = MyClass;\r\n```\r\n\r\nSince the methods of `MyClass` are doing stuff with `foo`, I want to be able to test that the `foo` methods are being called as expected. I have created a manual mock of `Foo` in the `__mocks__` directory (although I think the problematic would be essentially the same with an automatic mock). After reading the documentation about [CommonJS testing](http://facebook.github.io/jest/docs/common-js-testing.html), I expected that I would be able to add a property to the `Foo` constructor that tracks when an instance is created, something like this:\r\n\r\n```\r\nvar Foo = function() {\r\n  // ... constructor stuff\r\n  if (Foo.__instances__) {\r\n    Foo.__instances__.push(this);\r\n  }\r\n  else {\r\n    Foo.__instances = [ this ];\r\n  }\r\n}\r\n```\r\n\r\nHowever, what I am observing (having put some logging into the `foo.js` implementation file) is that the mock is being instantiated every time it is `require`d. My reading of the documentation mentioned above was that the same mock implementation would be reused (since otherwise how can I `require` the mock and check whether its methods were called by the module I am testing?).\r\n\r\nSo two questions:\r\n1) What am I misunderstanding and/or doing wrong?\r\n2) Assuming that I am able to get my hands on the same instance of the mock as the module being tested, does the `__instances__` trick make sense, or is there a better way to get my hands on the instances of that particular mock that have been created in the context of a given test?"}}