Mocha和Chai没有答应
Mocha and Chai fail insde a promise
我无法让Chai expect
在这个简单的例子中工作:Mocha的done
函数似乎从未被调用,断言也被忽略:
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();
});
});
});
请注意,我已经尝试了这两种方法,使用和不使用chai-as-promised
的eventually
。
这是相关的输出:
> 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.
正如你所看到的,承诺正在发挥作用,我得到了控制台输出。但是expect()
调用work和done()
调用都不执行任何操作。
我不确定它是否相关,但我使用的是isomorphic-fetch
和建议的es6-promise
。
我发现了问题。实际上并不需要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);
});
});
相关文章:
- 如何在Mocha/Chai中测试JS原型(非模块)
- Node.js-chai是一个item对象
- Chai深度包含了对嵌套对象的断言
- Mocha/Chai测试链接到函数返回断言错误
- 如何按照承诺使用mocha/chai/chai测试ES7异步函数
- 在http服务单元测试中使用request-js时,chai.request不是一个函数
- 用一个数组应用Chai可链接的getters
- 使用 Chai - 如何检查对象属性是否包含 DOM 元素
- Chai 期望 [函数] 抛出一个(错误)未通过测试(使用 Node)
- 在JavaScript中测试一个无效参数w/Chai
- Chai正如承诺:当承诺抛出错误时,处理错误
- 这是链接chai.js's期望在守夜
- 按照承诺,通过与chai的函数调用验证已解析承诺的内容
- 当“new constructor()”与mocha和chai一起使用时,我如何断言throw
- 不可变的Chai断言错误,而预期的结果等于
- 给Chai/Mocha一份应该包括在内的部分钥匙清单
- Mocha和Chai如约超时
- 按照承诺在Ember应用程序中添加chai
- Javascript assertion libraries - chai vs check-types
- Mocha和Chai没有答应