This package has been deprecated

Author message:

No longer maintained

@jdes/jest-sandbox

2.0.0 • Public • Published

Jest Sandbox

Build Status Coverage Status

A Sinon's sandbox like for Jest Edit

Table of contents

Setup

npm install --save-dev @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.

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

restoreAllMocks(): void

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

  afterEach(() => {
    sandbox.restoreAllMocks();
  });

Examples

import Sandbox from "@jdes/jest-sandbox"

describe("Readme's examples", () => {
  const sandbox = new Sandbox();

  afterEach(() => {
    sandbox.restoreAllMocks();
  });

  test("should calls API", () => {
    const service = {
      callApi: () => Promise.reject()
    };

    const spyLog = sandbox.spyOn(service, "log", true)
      .mockImplementation((log) => log);

    const spyCallApi = sandbox.spyOn(service, "callApi")
      .mockImplementation((path) => {
        service.log(`GET ${path}`);
        return Promise.resolve('Hello');
      });

    return service.callApi('/')
      .then((response) => {
        expect(spyCallApi).toHaveBeenCalled();
        expect(spyLog).toHaveBeenCalledWith('GET /');
        expect(response).toBe("Hello");
      });
  });
});

Readme

Keywords

none

Package Sidebar

Install

npm i @jdes/jest-sandbox

Weekly Downloads

2

Version

2.0.0

License

MIT

Last publish

Collaborators

  • jdes