jestjs - TypeError : (0 , _mockingoose.default) 不是函数 _ mockingooose

标签 jestjs mockingoose

我正在尝试用这段代码模拟我的 Mongoose 模式:

// report.schema.js
import mongoose, { Schema } from 'mongoose';

const reportSchema = new Schema(
  {
    data:{
        type: string
    },
  
  },
  { timestamps: true }
);

export default mongoose.model('Report', reportSchema);

在我的测试中:

import mockingoose from "mockingoose";
import Report from "../mongodb/models/report";
mockingoose(Report).toReturn({ data: "some_foo" }, "findOne");

错误是:

  ● Test suite failed to run

    TypeError: (0 , _mockingoose.default) is not a function

      1 | import mockingoose from "mockingoose";
      2 | import Report from "../mongodb/models/report";
    > 3 | mockingoose(Report).toReturn({ data: "some_foo" }, "findOne");

最佳答案

我解决了这个问题:

var bookMock = sinon.mock(new Book({ title: 'Rayuela', author: 'Julio Cortazar' }));
var book = bookMock.object;

我使用了这个链接 https://github.com/underscopeio/sinon-mongoose/blob/master/examples/promises/test.js enter image description here

关于jestjs - TypeError : (0 , _mockingoose.default) 不是函数 _ mockingooose,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70156753/

相关文章:

jestjs - 有没有办法让 `toHaveBeenCalledWith` 匹配正则表达式?

reactjs - 如何在enzyme中调用多个setProps?

reactjs - 如何通过更改 useState 来测试 React Hooks 组件

typescript - 使用 Jest/Typescript 和虚拟函数测试 fs 库函数

reactjs - 在 enzyme 模拟函数中传递一个完整的 `event` 对象