Sunday, 8 July 2018

What's the reason this gives me an error ?

I'm newbie to testing. So, I may be have done a stupid mistake. Anyway my test isn't working. When I run my test it gives me this error. I'm using Jest as the testing library and supertest for testing API requests (http)my linksController is in components/links path. This is the particular function.const getLastWeek = async (req, res) => { const client = new Client(conString); await client.connect(); if (req.query.userid !== undefined) { const { userid } = req.query; const getQuery = "SELECT * FROM links WHERE userid=($1) LIMIT 50"; const values = [userid]; try { const data = await client.query(getQuery, values); console.log(data.rows); res.send(data.rows); } catch (err) { console.log(err); res.send("Error occured"); } } }; I have imported all controllers to single route file and export it to use it as a middleware in server.jsconst { addLink, updateLink, deleteLink, getLastWeek } = require("./linksController"); module.exports = router => { router.post("/addlink", addLink); router.get("/update", updateLink); router.get("/delete", deleteLink); router.get("/getlinks", getLastWeek); }; This is how I use this route file (which is components/links/index.js) in my server.jsconst usersComponent = require("./components/users"); app.use("/api/user", usersComponent); This is my test case which is links.spec.jsconst { getLastWeek } = require("../components/links/linksController"); const request = require('supertest'); describe("Test the root path", () => { test("It should response the GET method", async() =>{ const response = await request(getLastWeek).get("/api/links/getlinks").query({ userid: 'Test1' }) expect(response.statusCode).toBe(200) } ); }); But when I run above test, it gives me this error.FAIL tests/links.spec.js Test the root path ✕ It should response the GET method (1159ms) ● Test the root path › It should response the GET method TypeError: Cannot read property 'userid' of undefined 96 | const client = new Client(conString); 97 | await client.connect(); > 98 | if (req.query.userid !== undefined) { | ^ 99 | const { userid } = req.query; 100 | const getQuery = "SELECT * FROM links WHERE userid=($1) LIMIT 50"; 101 | const values = [userid]; at Server.getLastWeek (components/links/linksController.js:98:17) Test Suites: 1 failed, 1 total Tests: 1 failed, 1 total Snapshots: 0 total Time: 2.496s, estimated 7s Ran all test suites. Jest did not exit one second after the test run has completed. This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue It says, TypeError: Cannot read property 'userid' of undefined . But I have already attached query parameters into get request with supertest by following this . What may be the reason ? Removing asynchronous doesn't fix this (gives the same error) How can I fix this ? Any help would be appreciated. Thanks.

Submitted July 08, 2018 at 07:52PM by sp3co92

No comments:

Post a Comment