I’m used this code to test my API endpoint using Jest. But, I don’t why my code is not running.
const mongoose = require("mongoose");
const request = require("supertest");
const app = require("../index.js");
require("dotenv").config();
beforeEach(async () => {
await mongoose.connect(process.env.mongoUrl);
});
describe("POST /user/register", () => {
it("should return successfull and register an user", async () => {
const res = await request(app).post("/user/register").send({
name: "name",
email:"[email protected]",
password: "passcode"
});
expect(res.statusCode).toBe(200);
expect(res.body.name).toBe("hari");
});
});
/* Closing database connection after each test. */
afterEach(async () => {
await mongoose.connection.close();
});
This is the console error when I run the code
I run the script using cross-env NODE_ENV=test jest --testTimeout=5000
I need to run this test for my API endpoint