stalniy / bdd-lazy-var

Provides UI for testing frameworks such as mocha, jasmine and jest which allows to define lazy variables and subjects.
MIT License
162 stars 14 forks source link

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

Closed AJMiller closed 6 years ago

AJMiller commented 6 years ago

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?

stalniy commented 6 years ago

Hm... Strange. I'll check it

stalniy commented 6 years ago

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?

stalniy commented 6 years ago

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

AJMiller commented 6 years ago

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?

stalniy commented 6 years ago

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

stalniy commented 6 years ago

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

AJMiller commented 6 years ago

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

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