Declaratively mocking a custom LWC in a single unit test

The approaches suggested of using jestconfig module mapper or the mocks folder work well if you have a single mock per component you are testing, but not if you have to build how your parent component interacts with a child components in different scenario.

I would like to create a custom mock of my component in a specific jest test like so:

jest.mock('c/my-component', 
() => {
  return {
    default: jest.fn(),
    myExternalMethod: jest.fn( () => 'something')
  };
},
{ virtual: true })

however the following approach fails like so:

   Assert Violation: c() 2nd argument Ctor must be a function.

I imagine this is somehow related to the fact that c/my-component extends LightningComponent, but I cannot find any documentation about this. What is the right way to mock a specific custom LWC in a specific test?

Answer

Attribution
Source : Link , Question Author : Edmondo1984 , Answer Author : Community

Leave a Comment