stalniy/bdd-lazy-var

Using in-line syntax results in error: context.expect is not a function

AJMiller opened this issue · 7 comments

Trying to use the new inline syntax like this:

describe('myFunction', () => {
  def('param', true);
  
  subject('fireFunction', () => myFunction($param));
  
  context('when the param is false', () => {
    def('param', false);

    it(() => is.expected.to.eql(false));
  });
});

results in the following error when running the test:

TypeError: context.expect is not a function
      at Object.get expected [as expected] (node_modules/bdd-lazy-var/global.js:420:24)
      at Context.<anonymous> (output/webpack:/myFunction.test.js:198:19)

Running the test using the older syntax works fine:

it('returns false', () => {
  expect($fireFunction).to.eql(false);
});

Using mocha + bdd-lazy-var. Any thoughts?

Hm... Strange. I'll check it

I put this:

describe('myFunction', () => {
  def('param', true);

  subject('fireFunction', () => $param);

  context('when the param is false', () => {
    def('param', false);

    it(() => is.expected.to.eql(false));
  });
});

in my tests and it works.

Could you please clarify what version of mocha, chai and bdd-lazy-var you use?

Do you have global.expect = chai.expect somewhere in configuration?

This shorthand functions expect that expect function is available in global namespace

Thanks for the quick reply @stalniy. I'll take a look into it and report back. I didn't see that line in our codebase on a quick search, and I didn't see it mentioned in the installation steps for bdd-lazy-var.

Versions:
Mocha: 5.2.0
Chai: 4.1.2
Bdd-lazy-var: 2.4.0

One difference I did notice is that your subject returns the value directly, where my example has the subject executing a function. Would that make a difference?

Having expect On global level is a common practice. Jest & jasmine sets own expect implementation into global scope. The only exception is chai but usually people export chai.expect to global scope as well and use it everywhere in tests

Update bdd-lazy-var to the latest version it’s 2.4.4

thanks @stalniy. Adding the following to our test startup script solved the issue:

import { expect } from 'chai';
global.expect = expect;