【问题标题】:Getting Invalid Chai property: 'calledWith'获取无效的 Chai 属性:'callWith'
【发布时间】: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


【解决方案1】:

我认为是您的模块系统或捆绑程序(webpack?)搞砸了。这在我的示例中运行良好: https://runkit.com/fatso83/chai-spy-example

前几天我在 WebKit 中看到了这个问题,我通过将导入分成两行来修复它。换个试试

import chai, { expect } from 'chai';

import chai, from 'chai';
import { expect } from 'chai';

【讨论】:

    猜你喜欢
    • 2019-01-30
    • 2022-08-19
    • 2019-08-28
    • 2018-01-01
    • 2018-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多