Franzf0
Franzf0

Reputation: 33

Mocked function returning "[Getter/Setter]" in jest

I want to test a component that uses this mixin method:

methods: {
    getActiveTab() {
      return new Promise((resolve) => chrome.tabs.query({ active: true }, tabs => resolve(tabs[0])));
    },
...

I'm mocking that method like this (I'm using jest-chrome):

chrome.tabs.query.mockImplementation((opts, cb) => cb([{ 'title': 'Hello World' }]));

But in my component, when I do this:

async mounted() {
   this.activeTab = await this.getCurrentTab();
},

this.activeTab will contain { title: [Getter/Setter] }. How do I force it to return the actual value?

If I do this:

jest.spyOn(chrome.tabs.query, 'get').mockImplementation((opts, cb) => cb([{ 'title': 'Hello World' }]));

I get this error:

Cannot spy the get property because it is not a function; undefined given instead

Upvotes: 0

Views: 2234

Answers (1)

Eduardo
Eduardo

Reputation: 1321

Try to mock getActiveTab that way:

  const wrapper = shallowMount(YourComponent, {
    mocks: {
      getActiveTab: jest.fn(() => { someData: 'someValue' })
    }
  })

OR simply mock activeTab property, I assume it is the target property you want to assert further:

  const wrapper = shallowMount(YourComponent, {
    mocks: {
      activeTab: {
        // desired data
      }
    }
  })

Upvotes: 1

Related Questions