hayaan
v1.4.2
Published
Ruby's RSpec-inspired scoped lazy variables for Jest, with native TypeScript support.
Downloads
6
Readme
hayaan
Ruby's RSpec-inspired scoped lazy variables for Jest, with native TypeScript support.
Installation
npm install --save-dev hayaan
/ yarn add --dev hayaan
Usage
import dyn from "hayaan";
const sum = (a, b) => a + b;
describe("implementation", () => {
const a = dyn(5);
const b = dyn(3);
const total = dyn(() => sum(a.value, b.value));
it("sums up", () => expect(total.value).toEqual(8));
describe("with b changed", () => {
b.value = 10;
it("sums up", () => expect(total.value).toEqual(15));
})
describe("with both changed", () => {
a.value = 1;
b.value = -2;
it("still sums up", () => expect(total.value).toEqual(-1));
});
describe("with even the function changed", () => {
total.deferredValue = () => sum(-a.value, b.value);
it("uses the new function", () => expect(total.value).toEqual(-2));
describe("and a changed", () => {
a.value = 1;
it("uses the right values", () => expect(total.value).toEqual(2));
})
});
})
Debugging
- Make sure
<variable>.value = ...
calls are outside theit
blocks- hayaan uses beforeEach and afterEach under the hood, which only work on higher level than individual tests
Credits
Inspired by https://github.com/tatyshev/given2 and https://gist.github.com/nerdcave/4418ca2c787f28c6cb30b7c96d7af1fe