简体   繁体   English

如何使用mocha / chai / chai-as-promised测试ES7异步函数

[英]How to test an ES7 async function using mocha/chai/chai-as-promised

I have the following function to test: 我有以下功能来测试:

// ...
const local = new WeakMap();

export default class User {

  // ...

  async password(password) {
    if (!password) return local.get(this).get('hash'); // remove this for security reasons!
    if (password.length < 6) throw new Error('New password must be at least 6 characters long');
    if (!password.match(passwordPattern)) throw new Error(`New password must match ${passwordPattern}`);
    local.get(this).set('hash', await Password.hash(password));
  }

  // ...

}

Now I want to test this function with mocha , chai and chai-as-promised doing this test-case: 现在我想用mochachaichai-as- promise来测试这个函数做这个测试用例:

import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';

import User from '../../../server/User';

chai.use(chaiAsPromised);
const expect = chai.expect;

describe('.password()', () => {

  const testuser = new User({username: 'Testuser', password: '123abc'});

  // FINDME
  it(`should throw when too short`, () => {
    return expect(testuser.password('1a')).to.eventually.throw();
  });

  // ...

});

Look for the // FINDME comment in the above code to get the context I'm referring to. 在上面的代码中查找// FINDME注释以获取我所指的上下文。

So what happens is that the test(s) do not wait for the async password() function to finish. 所以会发生的是,测试不会等待async password()函数完成。 As I found out ECMAScript7 async functions immediatelly return a thenable, so it should be fine since chai-as-promised use exactly this feature. 当我发现ECMAScript7异步函数立即返回一个thenable时,所以它应该没问题,因为chai-as-promised正好使用了这个特性。 My guess is, that async functions don't throw errors into this said promise rather than throwing it into the enclosing function itself, not being able to capture it through expect() . 我的猜测是,异步函数不会将错误抛入此声明,而不是将其抛入封闭函数本身,而不能通过expect()捕获它。

For the eager folks: I transpile this code on-the-fly with babel using the following command: 对于热切的人:我使用以下命令在babel上动态编写此代码:

babel-node --experimental node_modules/mocha/bin/_mocha --watch --recursive -R spec --check-leaks

Update for Babel 6: Babel 6更新:

Using babel-node 6.4.0, the following worked: 使用babel-node 6.4.0,以下工作:

npm install --save-dev babel-preset-stage-0

Then run: 然后运行:

./node_modules/.bin/babel-node --presets stage-0 node_modules/mocha/bin/_mocha --    
recursive -R spec --check-leaks

We solved this issue by replacing eventually.throw() with .be.rejected provided by chai-as-promised, since an async function always returns a Promise. 我们通过用chai-as- .be.rejected提供的.be.rejected替换.be.rejected eventually.throw()来解决这个问题,因为async function总是返回一个Promise。 That wasn't clear to me in the first place. 首先,我不清楚这一点。 Have a look at the discussion on github if you like. 如果你愿意,可以看一下关于github的讨论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Mocha和Chai-as-promise超时 - Mocha and Chai-as-promised time out 期望答应解决或拒绝的尝试无法正确地通过Mocha和Chai-as-Promise的测试失败 - expecting promised to resolve or reject doesn't properly fail a test with mocha and chai-as-promised 如何在 Typescript 中使用 chai-as-promised? - How to use chai-as-promised with Typescript? 使用chai-as-promise和chai-bignumber失败了 - Using chai-as-promised and chai-bignumber together fails 使用带有 chai-as-promised 的自定义 chai 方法断言 - Using a custom chai method assertion with chai-as-promised 为什么这个chai-as-promised AssertionError打印到Console而不是我的Mocha测试运行器? - Why is this chai-as-promised AssertionError printing to the Console but not my Mocha test runner? 使用Mocha和Chai-as-Promised测试被拒绝承诺的特定属性 - Testing for specific properties of rejected promises, with Mocha and Chai-as-Promised 测试chai-as-promise和mocha中嵌套属性的值 - Testing value of nested property in chai-as-promised and mocha 在测试 promise 拒绝时,在将 Mocha 与 chai-as-promised 一起使用时获得“不是一个 thenable” - When testing for promise rejections, getting “is not a thenable” when using Mocha with chai-as-promised Test Promise链以.catch结尾(按承诺使用Mocha / Chai) - Test Promise chain ends in .catch (using Mocha / Chai as promised)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM