I have a mongoose model:
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;
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: 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 did not exit one second after the test run has completed.
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?