Skip to content
/ hayaan Public

Scoped lazy variables for Jest with native TypeScript support

Notifications You must be signed in to change notification settings

wyozi/hayaan

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

hayaan

Ruby's RSpec-inspired scoped lazy variables for Jest, with native TypeScript support.

Comparison

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 the it 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

About

Scoped lazy variables for Jest with native TypeScript support

Resources

Stars

Watchers

Forks

Packages