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

Using expect for notify? #203

Closed
tamj0rd2 opened this issue Jun 10, 2017 · 6 comments
Closed

Using expect for notify? #203

tamj0rd2 opened this issue Jun 10, 2017 · 6 comments

Comments

@tamj0rd2
Copy link

tamj0rd2 commented Jun 10, 2017

Is it possible to use the below code snippet with the expect syntax?

it("should change the state", function (done) {
    otherState.should.equal("before");
    promise.should.be.fulfilled.then(function () {
        otherState.should.equal("after");
    }).should.notify(done);
});

All of my tests have been using expect so far, so I'd prefer not to change it if possible. I get the error TypeError: (0 , _chai.expect)(...).to.be.fulfilled.then is not a function if I try to use the expect syntax. My code:

it('does a thing', done => {
  let promise = wrapper.instance().handleSubmit(new Event('submit')) // just returns a promise
  expect(promise).to.be.fulfilled.then(() => {
    // my expectations
  }).and.notify(done)
})

So I tried to use the should syntax instead but I get the error TypeError: promise.should.be.fulfilled.then is not a function

it('does a thing', done => {
  let promise = wrapper.instance().handleSubmit(new Event('submit')) // just a function that returns a promise
  promise.should.be.fulfilled.then(() => {
    // my expectations...
  }).should.notify(done)
})
@tamj0rd2 tamj0rd2 changed the title Should notify Using expect for notify? Jun 10, 2017
@meeber
Copy link
Contributor

meeber commented Jun 10, 2017

Few things:

  1. You should not be getting that error message regardless. Something else is going on in your code or test suite. I'm not sure what.
  2. Even if everything else was working correctly, only .should.notify would be able to follow up a .fulfilled.then in that way.
  3. I'd recommend just returning the promise instead, like the below example (which I've tested).
const chai = require("chai");
const chaiAsPromised = require("chai-as-promised");

chai.use(chaiAsPromised);

const expect = chai.expect;

it("meow", () => {
  let promise = Promise.resolve();
  return expect(promise).to.be.fulfilled.then(() => {
    expect(42).to.equal(42);
  });
});

@tamj0rd2
Copy link
Author

tamj0rd2 commented Jun 10, 2017

@meeber I'm really not sure what's going on. I've got this at the top of my code, but I'm still getting errors saying to.be.fulfilled is not a function

import chai, { expect } from 'chai'
import chaiAsPromised from 'chai-as-promised'
chai.use(chaiAsPromised)```

@tamj0rd2
Copy link
Author

tamj0rd2 commented Jun 10, 2017

Thanks for the return tip. That seems to work, but not with to.be.fulfilled. I've had to do this instead:

it("meow", (done) => {
  let promise = Promise.resolve(42);
  return promise.then(result => {
    expect(result).to.equal(42);
    done()
  });
});

It'd be great if I could get to.be.fulfilled working though...

@tamj0rd2
Copy link
Author

tamj0rd2 commented Jun 10, 2017

I've just noticed the following when I run npm ls --depth=0

UNMET PEER DEPENDENCY chai@4.0.2
npm ERR! peer dep missing: chai@>= 2.1.2 < 4, required by chai-as-promised@6.0.0

I have chai 4.0.2 installed but chai-as-promised thinks it's missing?

@meeber
Copy link
Contributor

meeber commented Jun 10, 2017

chai-as-promised doesn't support Chai 4 yet (see #157). You can use chai@3.5 instead.

@tamj0rd2
Copy link
Author

@meeber Ahh, thank you!

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

2 participants