OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to create a mock to a class with all methods?

  • Thread starter Thread starter CrazySynthax
  • Start date Start date
C

CrazySynthax

Guest
In Sinon, we can create a stub to a class:

Code:
    const auditTrailClientStub = sinon.createStubInstance(AuditTrailClient);

auditTrailClientStub will have all methods the original class AuditTrailClient has. For example, if AuditTrailClient has "method1", "method2" and "method3", then we can use these methods in auditTrailClientStub.

Do you how to mimic this functionality in Jest?

The only thing I found in Jest is:

Code:
    jest.mock('<path-to-AuditTrailClient>', () => ({
      method1: jest.fn(),
      method2: jest.fn(),
      method3: jest.fn(),
    }));

The problem here is that I have to name all the methods in AuditTrailClient that I'll need to mock. What if there are 10 methods in AuditTrailClient that I want to mock?

Is there an equivalent way to create a stub in Jest?

<p>In Sinon, we can create a stub to a class:</p>
<pre><code> const auditTrailClientStub = sinon.createStubInstance(AuditTrailClient);
</code></pre>
<p>auditTrailClientStub will have all methods the original class AuditTrailClient has.
For example, if AuditTrailClient has "method1", "method2" and "method3", then we can use these methods in auditTrailClientStub.</p>
<p>Do you how to mimic this functionality in Jest?</p>
<p>The only thing I found in Jest is:</p>
<pre><code> jest.mock('<path-to-AuditTrailClient>', () => ({
method1: jest.fn(),
method2: jest.fn(),
method3: jest.fn(),
}));
</code></pre>
<p>The problem here is that I have to name all the methods in AuditTrailClient that I'll need to mock. What if there are 10 methods in AuditTrailClient that I want to mock?</p>
<p>Is there an equivalent way to create a stub in Jest?</p>
 

Latest posts

Top