jest mockgoose - 在测试运行完成后一秒内开玩笑没有退出 [英] jest mockgoose - jest did not exit one second after the test run has completed

查看:12
本文介绍了jest mockgoose - 在测试运行完成后一秒内开玩笑没有退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个猫鼬模型:

var mongoose = require("mongoose");

var transactionSchema = mongoose.Schema({
  category: { type: String, required: [true, "Category is required."] },
  amount: Number,
  comment: String,
  tags: Array,
  currency: String
});

var Transaction = mongoose.model("Transaction", transactionSchema);

module.exports = Transaction;

使用 mockgoosejest 进行简单的单元测试:

And a simple unit test using mockgoose and jest:

var { Mockgoose } = require("mockgoose");
var mongoose = require("mongoose");
var Transaction = require("./transaction");

var mockgoose = new Mockgoose(mongoose);

describe("transaction", function() {
  afterEach(function() {
    mockgoose.helper.reset().then(() => {
      done();
    });
  });

  it("category is required", function() {
    mockgoose.prepareStorage().then(() => {
      mongoose.connect("mongodb://foobar/baz");
      mongoose.connection.on("connected", () => {
        var mockTransaction = new Transaction({
          category: "Transportation",
          amount: 25,
          comment: "Gas money, Petrol.",
          tags: ["Gas", "Car", "Transport"],
          currency: "EUR"
        });
        mockTransaction.save(function(err, savedTransaction) {
          if (err) return console.error(err);
          expect(savedTransaction).toEqual(mockTransaction);
        });
      });
    });
  });
});

现在,当我运行测试时,我收到以下两个警告:

Now when I run my tests, I get these two warnings:

(node:2199) UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝 ID:1):ReferenceError:done 未定义(节点:2199)[DEP0018] DeprecationWarning:未处理的承诺拒绝已弃用.以后不处理的promise拒绝将使用非零退出代码终止 Node.js 进程.

(node:2199) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): ReferenceError: done is not defined (node:2199) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

然后单元测试通过,然后我收到此错误消息:

Then the unit test passes, and then I get this error message:

测试运行完成后,Jest 没有退出一秒.

Jest did not exit one second after the test run has completed.

这通常意味着有异步操作在你的测试中停止.考虑运行 Jest--detectOpenHandles 来解决这个问题.

This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with --detectOpenHandles to troubleshoot this issue.

获得正确结果后如何终止测试?

How do I terminate the test once I get to the correct result?

推荐答案

错误的意思就是它所说的,done 没有定义,但它被使用了.如果使用承诺,则不需要它.Jest 支持 promises,promise 应该从块中返回以便正确处理:

The error means exactly what it says, done wasn't defined but it's used. And it isn't needed in case promises are used. Jest supports promises, a promise should be returned from a block in order to be properly handled:

afterEach(() => mockgoose.helper.reset());

如果这个问题中的开放句柄有问题,Mongoose可以通过以下方式明确断开连接:

If there's a problem with open handles as in this question, Mongoose can be explicitly disconnected with:

afterAll(() => mongoose.disconnect());

这篇关于jest mockgoose - 在测试运行完成后一秒内开玩笑没有退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆