AjjjHsh
AjjjHsh

Reputation: 253

How to mock properties with jest.fn()

I have the following code:

this.$vs.loading()
....
this.$vs.loading.close()

I need to pass a mock to shallowMount like this:

const vs = {
  loading: jest.fn()
}

mocks: {
   $vs: vs
}

But how can I mock loading and close?

Using loading: jest.fn(), I just mock loading(). How do I mock close() inside the loading property?

Upvotes: 5

Views: 4991

Answers (1)

tony19
tony19

Reputation: 138656

You can attach the mocked property to the mock function itself:

const vs = {
  loading: jest.fn()
}
vs.loading.close = jest.fn()

Upvotes: 5

Related Questions