I am trying to develop a MERN application. But MERN POST method unit testing fails with following error
ERROR:
1 failing
- POST /product/save/ OK, creating a new product works: AssertionError: expected { error: { errors: { …(5) }, …(3) } } to have property '_id' at Context. (test/api/product/post.js:33:29) at processTicksAndRejections (node:internal/process/task_queues:96:5
I have attached the code below.
MERNProject/products.js
const mongoose = require("mongoose");
const postSchema = new mongoose.Schema({
productName: {
type: String,
required: true,
},
description: {
type: String,
required: true,
},
price: {
type: String,
required: true,
},
productCategory: {
type: String,
required: true,
},
productUrl: {
type: String,
required: true,
},
});
module.exports = mongoose.model("Products", postSchema);
MERNProject/routes/products.js
const express = require("express");
const Products = require("../models/products");
const router = express();
//save Products
router.post("/product/save", (req, res) => {
let newProduct = new Products(req.body);
newProduct.save((err) => {
if (err) {
return res.status(400).json({
error: err,
});
}
return res.status(200).json({
success: "Product saved successfuly",
});
});
});
module.exports = router;
MERNProject/test/api/post.js
const expect = require("chai").expect;
const request = require("supertest");
const app = require("../../../routes/products.js");
const conn = require("../../../server.js");
describe("POST /product/save/", () => {
before((done) => {
conn
.connect()
.then(() => done())
.catch((err) => done(err));
});
after((done) => {
conn
.close()
.then(() => done())
.catch((err) => done(err));
});
it("OK, creating a new product works", async () => {
const res = await request(app).post("/product/save/").send({
productName: "Mango",
description: "description Mango",
price: "rs 50",
productCategory: "fruits",
productUrl:
"https://upload.wikimedia.org/wikipedia/commons/9/90/Hapus_Mango.jpg",
});
const body = res.body;
expect(body).to.contain.property("_id");
expect(body).to.contain.property("productName");
expect(body).to.contain.property("description");
expect(body).to.contain.property("price");
expect(body).to.contain.property("productCategory");
expect(body).to.contain.property("productUrl");
});
});
MERNProject/server.js
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const cors = require("cors");
const app = express();
//import routes
const postRoutes = require("./routes/products");
//app middleware
app.use(bodyParser.json());
app.use(cors());
app.use(postRoutes);
const PORT = 8000;
const DB_URL =
"mongodb srv://twg:[email protected]/myFirstDatabase?retryWrites=true&w=majority";
function connect() {
return new Promise((resolve, reject) => {
mongoose
.connect(DB_URL)
.then((res, err) => {
if (err) return reject(err);
//console.log(`DB connected`);
resolve();
})
.catch((err) => console.log(`DB connection error`, err));
});
}
function close() {
return mongoose.disconnect();
}
connect();
app.listen(PORT, () => {
console.log(`App is running on ${PORT}`);
});
module.exports = { connect, close };
CodePudding user response:
You probably have an error returned by your post request and the body
contains an error instead of the object you expect. From the traces, it seems to be something like:
{ error: { errors: { …(5) }, …(3) } }
I would say that your test works fine, the assertion raised an error and the test failed :)
To investigate further, you should log the content of res