Node.js 使用Jest在内存中查询mongodb

Node.js 使用Jest在内存中查询mongodb,node.js,mongodb,mongoose,jestjs,Node.js,Mongodb,Mongoose,Jestjs,我正在尝试对我的代码进行单元测试,我正在使用Node.js、Express、MongoDB、Mongoose并尝试使用Jest对其进行测试 我对内存中的mongo db with Jest有问题。我已经阅读了文档() 当尝试测试一个Mongoose模型的查询时,我认为我的查询没有指向正确的位置,我希望它对内存集合运行查询 我当前的测试设置: const { MongoClient } = require("mongodb"); const Preferences = requ

我正在尝试对我的代码进行单元测试,我正在使用Node.js、Express、MongoDB、Mongoose并尝试使用Jest对其进行测试

我对内存中的mongo db with Jest有问题。我已经阅读了文档()

当尝试测试一个Mongoose模型的查询时,我认为我的查询没有指向正确的位置,我希望它对内存集合运行查询

我当前的测试设置:

const { MongoClient } = require("mongodb");
const Preferences = require("./preferences");

const mongoose = require("mongoose");
const ObjectId = mongoose.Types.ObjectId;

describe("Preference helper function", () => {
  let connection;
  let db;
  let userPreferences;

  const testUser = new Preferences({
    userId: ObjectId("5eef15429e93464b3ccae235"),
    wordPreferences: [
      { wordId: 1, active: true},
      { wordId: 2, active: true},
      { wordId: 3, active: true},
      { wordId: 4, active: true},
      { wordId: 5, active: true}
    ],
  });

  beforeAll(async () => {
    connection = await MongoClient.connect(global.__MONGO_URI__, {
      useNewUrlParser: true,
    });
    db = await connection.db(global.__MONGO_DB_NAME__);
  });

  afterAll(async () => {
    await connection.close();
    await db.close();
  });

  beforeEach(async () => {
    userPreferences = await db.collection("userPreferences");
  });

  afterEach(async () => {
    await userPreferences.deleteMany();
  });

  it("get active words function gives correct result", async () => {
    await userPreferences.insertOne(testUser);
    const expectedResult = [1, 2, 3, 4, 5];
    let queryResult = await Preferences.getActiveWordIds(testUser.userId);
    expect(queryResult).toEqual(expectedResult);
  });
});
运行此代码会出现以下错误:
Timeout-在jest.setTimeout.Timeout指定的5000毫秒超时内未调用异步回调-在jest.setTimeout.Error指定的5000毫秒超时内未调用异步回调

更改此操作的超时并不能解决问题,因为查询似乎无法解决问题。以下是上文测试中使用的偏好模型的月亮鹅模型:

const mongoose = require("mongoose");
const PreferenceMappingSchema = require("../preference-mapping");

var PreferenceSchema = new mongoose.Schema({
  userId: {
    type: mongoose.Types.ObjectId,
    required: true,
  },
  wordPreferences: {
    type: [PreferenceMappingSchema],
    required: true,
  },
});

// Choose the correct db and set the model
const db = mongoose.connection.useDb("users");
const Preferences = (module.exports = db.model("Preference", PreferenceSchema));

module.exports.getActiveWordIds = async function(userId) {
  try {
    const user = await Preferences.aggregate([
        // Get the current users doc
      { $match: { userId: userId} },
      {
        $project: {
          wordPreferences: {
            // Get only active words
            $filter: {
              input: "$wordPreferences",
              as: "pref",
              cond: {
                $eq: ["$$pref.active", true],
              },
            },
          },
        },
      }
    ]).exec();
    // Map to list of valid numbers
    return user[0]['wordPreferences'].map(x => x.wordId);
  } catch (error) {
    console.warn(error);
    throw error;
  }
};

提前感谢您的帮助

Mongo和Mongoose连接是不相关的,Mongoose无法在代码中意识到Mongo连接。userPreferences.insertOne是等待连接的挂起承诺。在使用Mongoose时不要使用原始Mongo,就是这样。Mongo和Mongoose连接是不相关的,Mongoose无法在代码中意识到Mongo连接。userPreferences.insertOne是等待连接的挂起承诺。用猫鼬的时候不要用生的猫鼬,就是这样。