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

Viewed 366

I am trying to mock my mongoose schemas.with this code :

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

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

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

and inside my test:

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

and the error is:

  ● 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");
3 Answers

I could not properly solve the issue myself. I will get back to you if I can. By now, as a workaround you can use

// @ts-expect-error TS7016
import * as mockingoose from 'mockingoose'

In my project "esModuleInterop": true, does not work for some reason. Also I will provide you my tsconfig.json just in case:

{
  "compilerOptions": {
    "strict": true,
    "target": "ES2021",
    "declaration": true,
    "sourceMap": true,
    "module": "commonjs",
    "moduleResolution": "node",
    "lib": ["ES2021"],
    "resolveJsonModule": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "importsNotUsedAsValues": "preserve"
  }
}

Error Message:

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

In my case the problem was solved by changing syntax ES6 import/export to CommonJS

Before:

import mockingoose from "mockingoose"

After:

const mockingoose = require("mockingoose");
Related