【发布时间】:2019-07-01 15:54:19
【问题描述】:
我正在为我的 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 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({})
});
});
【问题讨论】:
-
calledWith在 chai 中不存在,所以你需要chai.use(sinonChai);来添加它。您的问题不是 Invalid Chai 属性错误,而是您在该行中遇到的 TypeError 。您可能需要相应地改写这个问题。 -
至于那条线为什么不工作,需要对周围环境有更多的了解。我看到了 TypeScript 标签,所以我假设您正在使用 TypeScript 来支持 ES6 模块语法。大多数时候,当模块引用返回未定义时,通常是循环依赖的结果。当然,如果没有看到 ./timestamp 模块中的内容及其自身的依赖项,我不能肯定地说。
-
另外,在运行之前查看 TS 实际转译的内容可能会有所帮助。
标签: typescript express sinon chai sinon-chai