Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expect syntax support? #20

Open
ivitivan opened this issue Aug 28, 2015 · 14 comments
Open

Expect syntax support? #20

ivitivan opened this issue Aug 28, 2015 · 14 comments

Comments

@ivitivan
Copy link

Hi!

Does it work with expect syntax?

@RubenVerborgh
Copy link
Collaborator

Yes.

@weijie-tang
Copy link

weijie-tang commented Apr 27, 2017

I've been trying to wrap some chai-things in expect syntax as a custom assertion for the purposes of use with Serenity-JS but I get an error when I try to do it. Should works fine.

export const AllContainsOrgCode = expected => actual => actual.should.eventually.all.have.property('orgCode', expected);
export const AllContainsOrgCodeExpect = expected => actual => expect(actual).to.eventually.all.have.property('orgCode', expected);

The first line works fine but the latter line throws an error: TS2339:Property 'have' does not exist on type 'PromisedKeyFilter'.

@meeber
Copy link

meeber commented Apr 27, 2017

@weijie-tang I'm assuming by .eventually that you're using chai-as-promised as well. Which order are you loading chai-as-promised and chai-things? chai-things needs to be loaded first. The first example below works for me, but not the second:

// Works
chai.use(chaiThings);
chai.use(chaiAsPromised);

it("blah", function () {
  var orgs = Promise.resolve([
    {"orgCode": 42},
    {"orgCode": 42},
  ]);

  return expect(orgs).to.eventually.all.have.property("orgCode", 42);
});
// Doesn't work because of `.use` order
chai.use(chaiAsPromised);
chai.use(chaiThings);

it("blah", function () {
  var orgs = Promise.resolve([
    {"orgCode": 42},
    {"orgCode": 42},
  ]);

  return expect(orgs).to.eventually.all.have.property("orgCode", 42);
});

If that's not the issue, then my second guess is related to a conflict between .all in Chai core and chai-things; it has a different behavior between core and the plugin. This conflict isn't destructive enough to cause my above examples to stop working, but when you combine it with the chai-as-promised TypeScript type definitions (which seem to treat .all based on Chai core instead of chai-things), maybe it's causing a destructive conflict. I don't know enough about TypeScript to troubleshoot further.

@weijie-tang
Copy link

@meeber I have them imported in the same order as you do and I still get the error.

import chai = require('chai');
chai.use(require('chai-things'));
chai.use(require('chai-as-promised'));
const expect = chai.expect;

@meeber
Copy link

meeber commented Apr 28, 2017

@weijie-tang Then my best guess is a conflict between chai-as-promised TypeScript type definitions and chai-things. I base that on the error message you're receiving and the fact that my simple example above works with chai-as-promised and chai-things but no TypeScript.

@ADjenkov
Copy link

Any progress on this?
TS2339:Property 'have' does not exist on type 'PromisedKeyFilter' when using TypeScript

@dhilgarth
Copy link

I think this now is a general problem as chai now also has to.all

@jorgemachado89
Copy link

Hey,

When attempting to use expect alongside chai-things it breaks, stating that:

expected { Object (sql, values) } to have a property 'length'

Order of use is the following:

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

chai.use(require('chai-things'));
var expect = chai.expect;

Any hints ?

@RubenVerborgh
Copy link
Collaborator

@jorgemachado89 Please also add the code for your assertion. It seems logical that an object { sql, values } does not have a property length.

@jorgemachado89
Copy link

jorgemachado89 commented Jan 9, 2018

Thanks for the quick feedback. Here follows an example below:

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

chai.use(require('chai-things'));
var expect = chai.expect;

describe('something', function() {
 it('the test description', function() {
    expect({
      sql: 'sasasasa',
      values: [['sasa','sasa'],['sasa','sasasa']]
    })
    .to.be.an('object')
    .to.have.deep.all.keys('sql', 'values');
 }};
});

Returns the following:

AssertionError: expected { Object (sql, values) } to have a property 'length'

Including chai-things, breaks the behaviour of expect from chai

@Knaledge
Copy link

Knaledge commented Mar 22, 2019

@RubenVerborgh any progress on this? We've just encountered this as well:

Including chai-things, breaks the behaviour of expect from chai)

@RubenVerborgh
Copy link
Collaborator

@RubenVerborgh I have not been involved anymore with chai for several years, so I unfortunately cannot advise you on this one.

@phil294
Copy link

phil294 commented Nov 30, 2019

Could this be solved by replacing the .add with .each?

@Crusader4Christ
Copy link

Tried several ways, but chai-things do not work in typescript:

import { expect, use } from 'chai';
use(require('chai-things'));

expect(actual).all.have.property("Id", fixture.Id);
---
test.ts:162:24 - error TS2339: Property 'have' does not exist on type 'KeyFilter'.

162     expect(actual).all.have.property("Id", fixture.Id);
                           ~~~~

Same result

import * as chai from 'chai';
chai.use(require('chai-things'));
const expect = chai.expect;
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants