admin管理员组文章数量:1332377
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
ment 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 mand:
babel-node --experimental node_modules/mocha/bin/_mocha --watch --recursive -R spec --check-leaks
Update for Babel 6:
Using babel-node 6.4.0, the following worked:
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
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
ment 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 mand:
babel-node --experimental node_modules/mocha/bin/_mocha --watch --recursive -R spec --check-leaks
Update for Babel 6:
Using babel-node 6.4.0, the following worked:
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
Share
Improve this question
edited Jan 19, 2016 at 14:22
arcseldon
37.2k17 gold badges125 silver badges126 bronze badges
asked Mar 29, 2015 at 21:00
kernelkernel
3,7533 gold badges28 silver badges34 bronze badges
1
- Take a look at my code at stackoverflow./questions/33369389/… maybe it can help you a bit? – egeland Commented Oct 28, 2015 at 2:00
1 Answer
Reset to default 8We solved this issue by replacing eventually.throw()
with .be.rejected
provided by chai-as-promised, since an async function
always returns a Promise. That wasn't clear to me in the first place. Have a look at the discussion on github if you like.
本文标签: javascriptHow to test an ES7 async function using mochachaichaiaspromisedStack Overflow
版权声明:本文标题:javascript - How to test an ES7 async function using mochachaichai-as-promised - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742294311a2448414.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论