Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

jest mock method by name

import Foo from './Foo';
import Bar from './Bar';

jest.mock('./Bar');

describe('Foo', () => {
  it('should return correct foo', () => {
    // As Bar is already mocked,
    // we just need to cast it to jest.Mock (for TypeScript) and mock whatever you want
    (Bar.prototype.runBar as jest.Mock).mockReturnValue('Mocked bar');
    const foo = new Foo();
    expect(foo.runFoo()).toBe('real foo : Mocked bar');
  });
});


Comment

jest mock mockname

test("mockName", () => {
  const mockFn = jest.fn().mockName("mockedFunction");
  mockFn(); // comment me
  expect(mockFn).toHaveBeenCalled();
});
Comment

jest mock call

test("mock.calls", () => {
  const mockFn = jest.fn();
  mockFn(1, 2);

  expect(mockFn.mock.calls).toEqual([[1, 2]]);
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: js object from array of keys 
Javascript :: function for flatten an array 
Javascript :: express and node 
Javascript :: .shift javascript 
Javascript :: function to count words in string 
Javascript :: swr data fetching 
Javascript :: stripe angular 
Javascript :: jQuery - Add Elements 
Javascript :: includes in js 
Javascript :: react get route params 
Javascript :: a href javascript 
Javascript :: what triggers formik validate 
Javascript :: js array includes multiple items 
Javascript :: yarn globakl 
Javascript :: nested json schema mongoose 
Javascript :: How to return arguments in an array in javascript 
Javascript :: byte number to array js 
Javascript :: mouse position 
Javascript :: xpath in javascript 
Javascript :: react white screen 
Javascript :: get previous link javascript 
Javascript :: how to redirect to another page after clicking ok in alert 
Javascript :: global execution context javascript 
Javascript :: audio element javascript 
Javascript :: node.js parameters 
Javascript :: how to check for unused dependencies in my react project 
Javascript :: how to check if email already exists in database using javascript 
Javascript :: javascript string 
Javascript :: countdown js 
Javascript :: javascript add parameter to object 
ADD CONTENT
Topic
Content
Source link
Name
1+8 =