Have you noticed that comprehension arrives more quickly when reading comparison pieces that juxtapose the syntaxes, rules, and shibboleths of two programming languages. Steve Hicks' article What JavaScript Tests Could Learn From RSpec is one such example.

My preference when writing tests is to keep re-assigning and re-using variables to a minimum. Localize them as close to the assertions as possible. Beware of cross-contamination. Pollination. Sexual interops.

Recreating a lazy-evaluated let-style block in JS, Hicks performs some clever scoping gymnastics which makes me nervous. The declarations of calculator, first, and second feel visually and temporally far-flung from usage.

describe("Calculator", () => {
  let calculator
  beforeEach(() => { calculator = new Calculator() })

  describe(".multiply", () => {
    let first, second
    function getResult() {
      return calculator.multiply(first, second)
    }

    describe("when the first value is negative", () => {
      beforeEach(() => { first = -1 })

      describe("when the second value is negative", () => {
        beforeEach(() => { second = -3 })

        it("returns a positive number", () => {
          expect(getResult()).toEqual(3)
        })
      })

      describe("when the second value is positive", () => {
        beforeEach(() => { second = 3 })

        it("returns a negative number", () => {
          expect(getResult()).toEqual(-3)
        })
      })
    })
  })
})

This doesn’t really bother me though:

let sharedVar;

before(() => {
  sharedVar = ...
})

describe(() => {
  it(() => {
    sharedVar = ...
  })
})

Is rspec let better than JS let?

Even rspec maintainers caution us, Hicks notes. Convenience always carries a caveat in programming. This is a substantial pillar of the “readability” discussion: durability of convenience.

Note: let can enhance readability when used sparingly (1,2, or maybe 3 declarations) in any given example group, but that can quickly degrade with overuse. YMMV.

Cool find: givens. Shoutout to the translators, the transcribers, the interpreters.