@reaktivo/declare
v0.1.1
Published
A tiny dependency injection library
Downloads
1
Readme
declare
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.