stalniy/bdd-lazy-var

Fallback to parent's variable inside the same definition sometimes fails

dkreft opened this issue · 4 comments

It looks like a parent-referencing subject() fails to initialize when there is another lazy variable in the same scope. Here's a minimal test case to demonstrate the problem:

'use strict'

import { expect } from 'chai'

class UserProvidedValue {
  constructor(value) {
    this.value = value
  }
}

class HardCodedValue {
  constructor(value) {
    this.value = { x: 'hard-coded' }
  }
}

describe('self-referencing subject problem', () => {
  describe('UserProvidedValue', () => {
    def('model', () => {
      return new UserProvidedValue($value)
    })

    describe('.value', () => {
      subject(() => $model.value)

      context('the x value', () => {
        def('value', () => ({ x: 5 }))

        subject(() => $subject.x)

        it('returns 5', () => {
          expect($subject).to.equal(5)
        })
      })
    })
  })

  describe('HardCodedValue', () => {
    def('model', () => {
      return new HardCodedValue()
    })

    describe('.value', () => {
      subject(() => $model.value)

      context('the x value', () => {
        subject(() => $subject.x)

        it('returns "hard-coded"', () => {
          expect($subject).to.equal('hard-coded')
        })
      })
    })
  })
})

When I run it through my gulp task, I see the following:

$ gulp test --name lazy-var
[10:21:28] Using gulpfile /workplace/exm-admin-tool/gulpfile.js
[10:21:28] Starting 'test'...


  self-referencing subject problem
    UserProvidedValue
      .value
        the x value
          1) returns 5
    HardCodedValue
      .value
        the x value
          ✓ returns "hard-coded"


  1 passing (11ms)
  1 failing

  1) self-referencing subject problem UserProvidedValue .value the x value returns 5:
     TypeError: Cannot read property 'x' of undefined
      at Context.<anonymous> (lazy-var-test.js:29:23)
      at node_modules/bdd-lazy-var/lib/interface.js:115:25
      at Context.Object.defineProperty.get (node_modules/bdd-lazy-var/lib/lazy_var.js:40:71)
      at context.get (node_modules/bdd-lazy-var/lib/interface.js:63:38)
      at Object.defineProperty.get (node_modules/bdd-lazy-var/lib/define_var.js:31:22)
      at Context.<anonymous> (lazy-var-test.js:32:18)



[10:21:28] 'test' errored after 161 ms
[10:21:28] Error in plugin 'gulp-mocha'
Message:
    1 test failed.

Note that this is not an ES6/babel issue. Replacing the classes with functions and invoking with mocha directly gives the same results:

'use strict'

var expect = require('chai').expect;

function UserProvidedValue(value) {
  this.value = value
}

function HardCodedValue(value) {
  this.value = { x: 'hard-coded' }
}

// rest of the file is unchanged

Here's the output:

 $ ./node_modules/.bin/mocha -u bdd-lazy-var/global test/es5-lazy-var-test.js


  self-referencing subject problem
    UserProvidedValue
      .value
        the x value
          1) returns 5
    HardCodedValue
      .value
        the x value
           returns "hard-coded"


  1 passing (12ms)
  1 failing

  1) self-referencing subject problem UserProvidedValue .value the x value returns 5:
     TypeError: Cannot read property 'x' of undefined
      at Context.<anonymous> (test/es5-lazy-var-test.js:25:31)
      at node_modules/bdd-lazy-var/lib/interface.js:115:25
      at Context.Object.defineProperty.get (node_modules/bdd-lazy-var/lib/lazy_var.js:40:71)
      at context.get (node_modules/bdd-lazy-var/lib/interface.js:63:38)
      at Object.defineProperty.get (node_modules/bdd-lazy-var/lib/define_var.js:31:22)
      at Context.<anonymous> (test/es5-lazy-var-test.js:28:18)

thank you for posting the issue. I'll check it now

@dkreft fixed by 00bdb06 Could you please re-check in the latest master? If everything works fine, I'll close the issue

Yes, this seems to have fixed it. Please let me know when you make a release for this change.

@dkreft fixed in 1.1.8 , available in npm now.