#Why is the object that i'm passing considered undefined??

15 messages · Page 1 of 1 (latest)

keen cosmos
regal bridge
#

@keen cosmos Kinda impossible to answer from a screenshot of a unit test: you'd have to look at what code is calling that function and why it's calling it with undefined instead of the expected object.

keen cosmos
#

how do I post ts code here?

#

what do I add to the `

regal bridge
#

```ts
// code here

keen cosmos
#
describe("GET /users/:username", () => {
        test("It returns a user with a specific username", async () => {

            const inputAdmin = { username: "admin", name: "admin", surname: "admin", password: "admin", role: "admin" }


            jest.spyOn(UserController.prototype, "getUserByUsername").mockResolvedValue(testAdmin)
            jest.spyOn(Authenticator.prototype, "isLoggedIn").mockImplementation((req, res, next) => {
                return next();
            })
            jest.spyOn(Authenticator.prototype, "isAdmin").mockImplementation((req, res, next) => {
                return next();
            })


            const response = await request(app).get(baseURL + "/users/admin")
            expect(response.status).toBe(200)
            expect(UserController.prototype.getUserByUsername).toHaveBeenCalled()
            expect(UserController.prototype.getUserByUsername).toHaveBeenCalledWith( inputAdmin, "admin")
            expect(response.body).toEqual([testAdmin])
        })
    })
#

I'm trying to test this

#

oh i think i understand what is going

#

the input admin is the thing that is expected

#

not what I am actually passing

regal bridge
#

Yeah, it looks like req.user is undefined there.

#

When the test is expecting that it won't be.

#

Could be an actual bug, could be an issue in the spy logic setup in the test.

keen cosmos
#

yes