@reaktivo/declare

0.1.1 • Public • Published

declare

GitHub license npm version Coverage Status CircleCI Status Greenkeeper badge PRs Welcome

Installation

npm i @reaktivo/declare

Usage

Imagine you want build a library that fetches json files,

// fetchJson.js
const declare = require("@reaktivo/declare");

module.exports = declare(({ fetch = require("node-fetch") }) => {
  return (...args) => fetch(...args).then(res => res.json());
});

When we need to use our tiny library, we simply require it the following way:

const fetchJson = require("./fetchJson");
fetchJson("https://httpbin.org/ip").then(json => console.log(json.ip));

Dependency injection

Since we don't actually want to call APIs when running our tests, we inject mocked dependencies:

// fetchJson.test.js
const inject = require("@reaktivo/declare/inject");
const mockFetch = url =>
  Promise.resolve({ json: () => Promise.resolve({ ip: "123.123.123.123" }) });
const fetchJson = inject(require("./fetchJson"))({
  fetch: mockFetch
});

// Now on our test we can make sure no actual http request will go out
// and that the fake API response will resolve

Development

Testing is done with Jest and is intended to be simple and straight to the point.

npm test

License

declare is open source software licensed as MIT.

Package Sidebar

Install

npm i @reaktivo/declare

Weekly Downloads

3

Version

0.1.1

License

MIT

Unpacked Size

6.11 kB

Total Files

13

Last publish

Collaborators

  • reaktivo