项目作者: jeandesravines

项目描述 :
:angel: A Sinon's sandbox like for Jest
高级语言: JavaScript
项目地址: git://github.com/jeandesravines/jest-sandbox.git
创建时间: 2017-06-15T08:53:26Z
项目社区:https://github.com/jeandesravines/jest-sandbox

开源协议:MIT License

下载


Jest Sandbox

Build Status
codecov

A Sinon’s sandbox like for Jest Edit

Table of contents

Setup

  1. yarn add -D @jdes/jest-sandbox

API

spyOn(target: Object, property: string, create: ?boolean): Function

This method works like jest.spyOn with the only difference that
if the property to mocks does not exist and the parameters create is true, it will be created.

  1. const object = {};
  2. const spy = sandbox.spyOn(object, "add", true)
  3. .mockImplmentation((a, b) => a + b);
  4. expect(object.add(20, 22)).toBe(42);

restoreAllMocks(): void

Calls .mockRestore() on all spies in the sandbox.

  1. afterEach(() => {
  2. sandbox.restoreAllMocks();
  3. });

Examples

  1. import Sandbox from "@jdes/jest-sandbox"
  2. describe("Readme's examples", () => {
  3. const sandbox = new Sandbox();
  4. afterEach(() => {
  5. sandbox.restoreAllMocks();
  6. });
  7. test("should calls API", () => {
  8. const service = {
  9. callApi: () => Promise.reject()
  10. };
  11. const spyLog = sandbox.spyOn(service, "log", true)
  12. .mockImplementation((log) => log);
  13. const spyCallApi = sandbox.spyOn(service, "callApi")
  14. .mockImplementation((path) => {
  15. service.log(`GET ${path}`);
  16. return Promise.resolve('Hello');
  17. });
  18. return service.callApi('/')
  19. .then((response) => {
  20. expect(spyCallApi).toHaveBeenCalled();
  21. expect(spyLog).toHaveBeenCalledWith('GET /');
  22. expect(response).toBe("Hello");
  23. });
  24. });
  25. });