I have this testing code that it’s not returning what i want, and it’s sending me 401 Authorization error, am i doing something wrong? I’m new at testing. I already tried passing the raw authentication token, but still the same error.
This is the error:
GET /products › should return a list of products
expect(received).toBe(expected) // Object.is equality
Expected: 200
Received: 401
22 | .get("/product")
23 | .set("Authorization", `Bearer ${token}`);
> 24 | expect(res.status).toBe(200);
| ^
25 | expect(Array.isArray(res.body)).toBe(true);
26 | });
27 | });
at __tests__/controllers/product.test.ts:24:24
at fulfilled (__tests__/controllers/product.test.ts:5:58)
import request from "supertest";
import { Request, Response } from "express";
import { app } from "../../src/app";
import { connect, disconnect } from "../../src/database/db";
import Product from "../../src/models/Product";
import Category from "../../src/models/Category";
import jwt from "jsonwebtoken";
import { login } from "../../src/controllers/auth"
beforeAll(async () => {
await connect();
});
afterAll(async () => {
await disconnect();
});
describe("GET /products", () => {
it("should return a list of products", async () => {
const token = jwt.sign({ role: "admin" }, "CARDAPIOJWTPASS");
const res = await request(app)
.get("/product")
.set("Authorization", `Bearer ${token}`);
expect(res.status).toBe(200);
expect(Array.isArray(res.body)).toBe(true);
});
});