Need help with jasmine spy for node-postgres function

hi

i have a function listAllVisitors() that returns an array of objects:

async function listAllVisitors() {
	try {
		const result = await pool.query("SELECT * FROM Visitors");
		return result.rows;
	} catch (err) {
		return err.stack;
	}
}

i wrote a spy already that spies on the pool.query method:

describe("listAllVisitors function", () => {
	it("should select all visitors from visitors table.", () => {
		spyOn(pool, "query");
		listAllVisitors();
		expect(pool.query).toHaveBeenCalled();
		expect(pool.query).toHaveBeenCalledWith("SELECT * FROM Visitors");
		
	});
});

now i need help with spying on the array of objects that gets returned from the listAllVisitors() function, i need to test that listAllVisitors() actually returns an array. What i tried so far(some are in comments, some not):

describe("listAllVisitors function", () => {
	it("should select all visitors from visitors table.", () => {
		spyOn(pool, "query");
		//spyOn(result, "rows");
		listAllVisitors();
		expect(pool.query).toHaveBeenCalled();
		expect(pool.query).toHaveBeenCalledWith("SELECT * FROM Visitors");
		//allVisitors = jasmine.createSpyObj("allVisitors", ["then"]);
		// //.and.returnValue([]);
		//allVisitors = jasmine.createSpy("allVisitors").and.returnValue([]);
		// expect(allVisitors.then).toBeDefined();

		//expect(pool.query).toHaveBeenCalled();
		expect(visitorArr).toEqual(jasmine.any(Array)); 
		// expect(Array.isArray(allVisitors.then)).toEqual(true);
	});
});

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.