我有这个导出的函数:
module.exports.doThing = async (input) => {
if(input === '') { throw('no input present') }
// other stuff
return input
}以及它的一个测试文件,在这个文件中,我试图测试当输入无效时是否抛出错误。这是我尝试过的:
const testService = require('../services/testService.js')
const chai = require('chai')
const expect = chai.expect
const sinon = require('sinon')
chai.use(require('sinon-chai'))
describe('doThing', () => {
it('throws an exception if input is not present', async () => {
expect(testService.doThing('')).to.be.rejected
})
})我得到了错误Error: Invalid Chai property: rejected和UnhandledPromiseRejectionWarning
如何修复此测试?
发布于 2020-07-16 01:07:18
您可以安装插件chai-as-promised。这允许您执行以下操作:
const testService = require('../services/testService.js')
const chai = require('chai')
.use(require('chai-as-promised'))
const expect = chai.expect;
describe('doThing', () => {
it('throws an exception if input is not present', async () => {
await expect(testService.doThing('')).to.be.rejectedWith('no input present');
});
it('should not throw ...', async () => {
await expect(testService.doThing('some input')).to.be.fulfilled;
});
})https://stackoverflow.com/questions/62918555
复制相似问题