0.1.1 • Published 7 years ago
@reaktivo/declare v0.1.1
declare
Installation
npm i @reaktivo/declareUsage
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 resolveDevelopment
Testing is done with Jest and is intended to be simple and straight to the point.
npm testLicense
declare is open source software licensed as MIT.