dimanche 29 mars 2015

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:



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.


So what happens is that the test(s) do not wait for the async password() function to finish. As I found out ECMAScript7 async functions immediatelly return a thenable, so it should be fine since chai-as-promised use exactly this feature. 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().


For the eager folks: I transpile this code on-the-fly with babel using the following command:


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


Any thoughts?


Aucun commentaire:

Enregistrer un commentaire