0

I can't make Chai expect work in this simple example: Mocha's donefunction never seems to get called and assertions are simply ignored:

import chai, {expect} from 'chai';
import chaiAsPromised from 'chai-as-promised';

chai.use(chaiAsPromised);

import 'isomorphic-fetch';

describe('Mocha and Chai', () => {
  it('tests chai expect inside a promise', (done) => {
    fetch('http://google.com').then(() => {
      const actual = 'test';
      const expected = 'expected';
      console.log(`'${actual}'' equals to '${expected}'?`, actual === expected);
      expect(actual).to.be.equals(expected);
      expect(actual).to.eventually.be.equals(expected);
      done();
    });
  });
});

Note that I have tried both, with and without chai-as-promised's eventually.

This is the relevant output:

> mocha tools/testSetup.js "src/**/*.spec.js" --reporter progress

'test'' equals to 'expected'? false

  1) Mocha and Chai tests chai expect inside a promise:
     Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.

As you can see, the promise is working, I get my console output. But neither the expect()calls work nor the done()call does anything.

I am not sure if it's relevant, but I'm using isomorphic-fetch and the suggested es6-promise.

4

1 回答 1

1

我发现了问题。done实际上不需要回调。如果您在块内返回承诺, Mocha 就会理解这种情况it()

it('tests chai expect inside a promise', () => {
   return fetch('http://google.com').then(() => {
     const actual = 'test';
     const expected = 'expected';
     console.log(`'${actual}'' equals to '${expected}'?`, actual === expected);
     expect(actual).to.be.equals(expected);
     expect(actual).to.eventually.be.equals(expected);        
   });
});
于 2016-07-25T20:58:52.100 回答