0

Trying to test UseHead in my component that's inside of a computed but it is throwing the following error Cannot read properties of undefined (reading 'name')

Vue Component has

useHead(computed(() => ({
  title: data?.title,
})));

Jest Test

import { useHead } from '@unhead/vue';

jest.mock('@unhead/vue');

const expectedArguments = { title: 'Test Title' };

describe('Page Meta Tests', () => {
  it('should display meta title', async () => {
     const wrapper = await mountComponent();
     expect(useHead).toHaveBeenCalledWith(expectedArguments);
  });
});
2
  • what line of code is the error on? there is no name in the code shown Commented May 18, 2024 at 0:08
  • Can you share the way you mocked @unhead/vue? Also the we don't know what mountComponent function looks like, you might have to attach the component to the virtual dom Commented May 22, 2024 at 7:54

1 Answer 1

0

You should not mock useHead just to test if it has been called. Just test the actual behavior of a testing component.

it('should display meta title', async () => {
     const wrapper = await mountComponent();
     expect(document.title).toBe(expectedTitle);
  });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.