John Winston
John Winston

Reputation: 1471

How to test internal store update in Jest?

Is it possible to test for object update in Jest, where that object is encapsulated inside a closure? I have a simple function like this:

function example(){
  const store = {}

  return {
    updateStore(key, value){
      store[key] = value
    }
  }
}

If I need to test updateStore, I need to check if the store has been updated, but I have no access to the store in test. Is mocking a solution for this situation?

Upvotes: 0

Views: 1150

Answers (1)

Tom
Tom

Reputation: 9137

To make it testable, you'll need to export either the store or some store-accessing method, so that the test can make assertions.

If you're writing your own store, as you are here, then this is a good idea.

If you're using a third-party store implementation, like Redux, you shouldn't test the store -- you should write tests that mock the store API and assert that your app code sends the right messages to the store.

Upvotes: 1

Related Questions