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

mock callback function jest

const bodyToAssertAgainst = {};
globals.request.post = jest.fn().mockImplementation((obj, cb) => {
    cb(null, bodyToAssertAgainst);
});
Comment

mock function jest

/*
The Mock Function
The goal for mocking is to replace something we don’t control with something 
we do, so it’s important that what we replace it with has all the features 
we need.

The Mock Function provides features to:

1. Capture calls
2. Set return values
3. Change the implementation

The simplest way to create a Mock Function instance is with jest.fn()
*/

test("returns undefined by default", () => {
  const mock = jest.fn();

  let result = mock("foo");

  expect(result).toBeUndefined();
  expect(mock).toHaveBeenCalled();
  expect(mock).toHaveBeenCalledTimes(1);
  expect(mock).toHaveBeenCalledWith("foo");
});

Comment

jest mock call

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

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

jest mock implementation once

const myMockFn = jest
  .fn()
  .mockImplementationOnce(cb => cb(null, true))
  .mockImplementationOnce(cb => cb(null, false));

myMockFn((err, val) => console.log(val));
// > true

myMockFn((err, val) => console.log(val));
// > false
Comment

jest mock instance

test("mock.instances", () => {
  const mockFn = jest.fn();

  const a = new mockFn();
  const b = new mockFn();

  mockFn.mock.instances[0] === a;
  mockFn.mock.instances[1] === b;
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: find element in array underscore js 
Javascript :: How to get maximum value in Javascript 
Javascript :: reactjs npm take photo 
Javascript :: fs readfile encoding 
Javascript :: render text in for loop react in function 
Javascript :: javascript && operator 
Javascript :: react native uid 
Javascript :: picture in picture remove from videojs 
Javascript :: Getting Search Parameters From A URL As An Array 
Javascript :: arduino vscode hex 
Javascript :: player.filter 
Javascript :: angular size of array 
Javascript :: how to change css using javascript 
Javascript :: array.find 
Javascript :: change the origin of html canvas 
Javascript :: confirm closing tab 
Javascript :: nodemon 
Javascript :: JSON.stringify() function converts buffers into objects. The raw data is encoded as an array of bytes that you can pass in to Buffer.from(). 
Javascript :: yup.array not working 
Javascript :: what is useref in react 
Javascript :: const { something} javascript 
Javascript :: final-form reset form 
Javascript :: javascript read file 
Javascript :: how to take yes or no in js 
Javascript :: type conversions in javascript 
Javascript :: javascript find area of triangle 
Javascript :: javascript check string empty 
Javascript :: get response from window.open 
Javascript :: iteration through json with key value pairs 
Javascript :: remove the last character from a string in JavaScript, 
ADD CONTENT
Topic
Content
Source link
Name
4+5 =