“JEST FUNCIONAL DE MOGO” Respostas de código

JEST FUNCIONAL DE MOGO

/*
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");
});

yusuf_fazeri

Método de zombaria por nome pelo nome

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');
  });
});


Tame Tapir

Função de retorno de chamada simulada JEST

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

Implementação de zombar de brincadeira uma vez

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
Proud Pony

Respostas semelhantes a “JEST FUNCIONAL DE MOGO”

Perguntas semelhantes a “JEST FUNCIONAL DE MOGO”

Mais respostas relacionadas para “JEST FUNCIONAL DE MOGO” em JavaScript

Procure respostas de código populares por idioma

Procurar outros idiomas de código