Typescript 获取无效的Chai属性:';用'调用;
我正在为我的express应用程序设置单元测试。当我运行测试时,它会失败,并出现此错误``Typescript 获取无效的Chai属性:';用'调用;,typescript,express,sinon,chai,sinon-chai,Typescript,Express,Sinon,Chai,Sinon Chai,我正在为我的express应用程序设置单元测试。当我运行测试时,它会失败,并出现此错误`` import * as timestamp from './timestamp' import chai, { expect } from 'chai' import sinonChai from 'sinon-chai' import { mockReq, mockRes } from 'sinon-express-mock' //chai.use(sinonChai); <-- `I remov
import * as timestamp from './timestamp'
import chai, { expect } from 'chai'
import sinonChai from 'sinon-chai'
import { mockReq, mockRes } from 'sinon-express-mock'
//chai.use(sinonChai); <-- `I removed this because it was creating this error:: TypeError: Cannot read property 'use' of undefined`
describe('hello world', () => {
it('should behave...', () => {
const request = {
body: {
foo: 'bar',
},
}
const req = mockReq(request)
const res = mockRes()
timestamp.timestamp(req, res)
expect(res.json).to.have.been.calledWith({})
});
});
import*作为“./timestamp”中的时间戳
从“chai”导入chai,{expect}
从“信农斋”进口信农斋
从“sinon express mock”导入{mockReq,mockRes}
//柴.用(新竹);{
它('应该表现…',()=>{
常量请求={
正文:{
福:“酒吧”,
},
}
const req=mockReq(请求)
常量res=mockRes()
时间戳。时间戳(req,res)
expect(res.json).to.have.been.calledWith({})
});
});
我认为是您的模块系统或捆绑包(webpack?)搞砸了。在我的示例中,这很好:
前几天我在WebKit中看到了这一点,我通过将导入拆分为两行来修复它。试着替换
import chai, { expect } from 'chai';
与
chai中不存在使用调用的
,因此需要使用chai.use(sinonChai)代码>以添加它。您的问题不是无效属性错误,而是您在该行中遇到的TypeError。您可能需要相应地重新表述这个问题。至于为什么这条线不起作用,这将需要对周围环境有更多的了解。我看到了TypeScript标记,所以我假设您使用TypeScript来支持ES6模块语法。大多数情况下,当模块引用未定义时,通常是循环依赖的结果。当然,如果不查看./timestamp模块中的内容及其自身的依赖关系,我就无法确定。此外,在运行之前查看TS实际传输到的内容可能会有所帮助。
import chai, from 'chai';
import { expect } from 'chai';