Skip to content

Commit

Permalink
chore: add tests for superHeroAPI route
Browse files Browse the repository at this point in the history
  • Loading branch information
somramnani committed Nov 23, 2024
1 parent 261f1e9 commit 6ee2373
Show file tree
Hide file tree
Showing 11 changed files with 203 additions and 171 deletions.
1 change: 1 addition & 0 deletions api/tests/hello.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const app = require("../index");
describe("GET /hello", () => {
it('should return a JSON response with message "hello"', async () => {
const response = await request(app).get("/hello");

expect(response.status).toBe(200);
expect(response.body).toEqual({ message: "hello" });
});
Expand Down
36 changes: 36 additions & 0 deletions api/tests/superHeroAPI.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const request = require("supertest");
const axios = require("axios");
jest.mock("axios");
const app = require("../index");

describe("GET /super-hero-api/:data", () => {
it("should return superhero data when the external API responds successfully", async () => {
const mockResponse = {
data: {
results: [
{
id: "620",
name: "Spider-Man",
powerstats: {
intelligence: "90",
strength: "55",
speed: "67",
durability: "75",
power: "74",
combat: "85",
},
},
],
},
};

axios.mockResolvedValue(mockResponse);

const response = await request(app).get("/super-hero-api/Spider-Man");

// Assertions
console.log(response.body.results);
expect(response.status).toBe(200);
expect(response.body.results[0].name).toBe("Spider-Man");
});
});
69 changes: 34 additions & 35 deletions coverage/clover.xml
Original file line number Diff line number Diff line change
@@ -1,50 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>
<coverage generated="1732168823541" clover="3.2.0">
<project timestamp="1732168823541" name="All files">
<metrics statements="30" coveredstatements="25" conditionals="4" coveredconditionals="3" methods="6" coveredmethods="2" elements="40" coveredelements="30" complexity="0" loc="30" ncloc="30" packages="2" files="3" classes="3"/>
<coverage generated="1732330065023" clover="3.2.0">
<project timestamp="1732330065023" name="All files">
<metrics statements="29" coveredstatements="26" conditionals="4" coveredconditionals="3" methods="6" coveredmethods="4" elements="39" coveredelements="33" complexity="0" loc="29" ncloc="29" packages="2" files="3" classes="3"/>
<package name="api">
<metrics statements="18" coveredstatements="16" conditionals="4" coveredconditionals="3" methods="2" coveredmethods="1"/>
<metrics statements="17" coveredstatements="15" conditionals="4" coveredconditionals="3" methods="2" coveredmethods="1"/>
<file name="index.js" path="/Users/som_ramnani/Projects/api-proxy-server/api/index.js">
<metrics statements="18" coveredstatements="16" conditionals="4" coveredconditionals="3" methods="2" coveredmethods="1"/>
<line num="1" count="2" type="stmt"/>
<line num="3" count="2" type="stmt"/>
<line num="4" count="2" type="stmt"/>
<line num="5" count="2" type="stmt"/>
<line num="6" count="2" type="stmt"/>
<line num="7" count="2" type="stmt"/>
<line num="9" count="2" type="stmt"/>
<line num="11" count="2" type="stmt"/>
<line num="17" count="2" type="stmt"/>
<line num="19" count="2" type="stmt"/>
<line num="20" count="2" type="stmt"/>
<line num="22" count="2" type="stmt"/>
<line num="23" count="1" type="stmt"/>
<line num="30" count="2" type="cond" truecount="2" falsecount="0"/>
<line num="32" count="2" type="cond" truecount="1" falsecount="1"/>
<metrics statements="17" coveredstatements="15" conditionals="4" coveredconditionals="3" methods="2" coveredmethods="1"/>
<line num="1" count="3" type="stmt"/>
<line num="3" count="3" type="stmt"/>
<line num="4" count="3" type="stmt"/>
<line num="5" count="3" type="stmt"/>
<line num="6" count="3" type="stmt"/>
<line num="8" count="3" type="stmt"/>
<line num="10" count="3" type="stmt"/>
<line num="16" count="3" type="stmt"/>
<line num="18" count="3" type="stmt"/>
<line num="19" count="3" type="stmt"/>
<line num="21" count="3" type="stmt"/>
<line num="22" count="1" type="stmt"/>
<line num="29" count="3" type="cond" truecount="2" falsecount="0"/>
<line num="31" count="3" type="cond" truecount="1" falsecount="1"/>
<line num="32" count="0" type="stmt"/>
<line num="33" count="0" type="stmt"/>
<line num="34" count="0" type="stmt"/>
<line num="38" count="2" type="stmt"/>
<line num="37" count="3" type="stmt"/>
</file>
</package>
<package name="api.routes">
<metrics statements="12" coveredstatements="9" conditionals="0" coveredconditionals="0" methods="4" coveredmethods="1"/>
<metrics statements="12" coveredstatements="11" conditionals="0" coveredconditionals="0" methods="4" coveredmethods="3"/>
<file name="hello.js" path="/Users/som_ramnani/Projects/api-proxy-server/api/routes/hello.js">
<metrics statements="4" coveredstatements="4" conditionals="0" coveredconditionals="0" methods="1" coveredmethods="1"/>
<line num="1" count="2" type="stmt"/>
<line num="2" count="2" type="stmt"/>
<line num="4" count="2" type="stmt"/>
<line num="6" count="2" type="stmt"/>
<line num="1" count="3" type="stmt"/>
<line num="2" count="3" type="stmt"/>
<line num="4" count="3" type="stmt"/>
<line num="6" count="3" type="stmt"/>
</file>
<file name="superHeroAPI.js" path="/Users/som_ramnani/Projects/api-proxy-server/api/routes/superHeroAPI.js">
<metrics statements="8" coveredstatements="5" conditionals="0" coveredconditionals="0" methods="3" coveredmethods="0"/>
<line num="1" count="2" type="stmt"/>
<line num="2" count="2" type="stmt"/>
<line num="3" count="2" type="stmt"/>
<line num="5" count="2" type="stmt"/>
<line num="6" count="0" type="stmt"/>
<line num="11" count="0" type="stmt"/>
<metrics statements="8" coveredstatements="7" conditionals="0" coveredconditionals="0" methods="3" coveredmethods="2"/>
<line num="1" count="3" type="stmt"/>
<line num="2" count="3" type="stmt"/>
<line num="3" count="3" type="stmt"/>
<line num="5" count="3" type="stmt"/>
<line num="6" count="1" type="stmt"/>
<line num="11" count="1" type="stmt"/>
<line num="14" count="0" type="stmt"/>
<line num="18" count="2" type="stmt"/>
<line num="18" count="3" type="stmt"/>
</file>
</package>
</project>
Expand Down
6 changes: 3 additions & 3 deletions coverage/coverage-final.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{"/Users/som_ramnani/Projects/api-proxy-server/api/index.js": {"path":"/Users/som_ramnani/Projects/api-proxy-server/api/index.js","statementMap":{"0":{"start":{"line":1,"column":0},"end":{"line":1,"column":27}},"1":{"start":{"line":3,"column":16},"end":{"line":3,"column":34}},"2":{"start":{"line":4,"column":13},"end":{"line":4,"column":28}},"3":{"start":{"line":5,"column":14},"end":{"line":5,"column":30}},"4":{"start":{"line":6,"column":26},"end":{"line":6,"column":58}},"5":{"start":{"line":7,"column":24},"end":{"line":7,"column":49}},"6":{"start":{"line":9,"column":12},"end":{"line":9,"column":21}},"7":{"start":{"line":11,"column":20},"end":{"line":15,"column":1}},"8":{"start":{"line":17,"column":0},"end":{"line":17,"column":27}},"9":{"start":{"line":19,"column":0},"end":{"line":19,"column":46}},"10":{"start":{"line":20,"column":0},"end":{"line":20,"column":35}},"11":{"start":{"line":22,"column":0},"end":{"line":28,"column":3}},"12":{"start":{"line":23,"column":2},"end":{"line":27,"column":5}},"13":{"start":{"line":30,"column":13},"end":{"line":30,"column":37}},"14":{"start":{"line":32,"column":0},"end":{"line":36,"column":1}},"15":{"start":{"line":33,"column":2},"end":{"line":35,"column":5}},"16":{"start":{"line":34,"column":4},"end":{"line":34,"column":49}},"17":{"start":{"line":38,"column":0},"end":{"line":38,"column":21}}},"fnMap":{"0":{"name":"(anonymous_0)","decl":{"start":{"line":22,"column":13},"end":{"line":22,"column":14}},"loc":{"start":{"line":22,"column":27},"end":{"line":28,"column":1}},"line":22},"1":{"name":"(anonymous_1)","decl":{"start":{"line":33,"column":19},"end":{"line":33,"column":20}},"loc":{"start":{"line":33,"column":25},"end":{"line":35,"column":3}},"line":33}},"branchMap":{"0":{"loc":{"start":{"line":30,"column":13},"end":{"line":30,"column":37}},"type":"binary-expr","locations":[{"start":{"line":30,"column":13},"end":{"line":30,"column":29}},{"start":{"line":30,"column":33},"end":{"line":30,"column":37}}],"line":30},"1":{"loc":{"start":{"line":32,"column":0},"end":{"line":36,"column":1}},"type":"if","locations":[{"start":{"line":32,"column":0},"end":{"line":36,"column":1}},{"start":{},"end":{}}],"line":32}},"s":{"0":2,"1":2,"2":2,"3":2,"4":2,"5":2,"6":2,"7":2,"8":2,"9":2,"10":2,"11":2,"12":1,"13":2,"14":2,"15":0,"16":0,"17":2},"f":{"0":1,"1":0},"b":{"0":[2,2],"1":[0,2]},"_coverageSchema":"1a1c01bbd47fc00a2c39e90264f33305004495a9","hash":"4569801c76b1c186e1add62c63f3933d6289e677"}
,"/Users/som_ramnani/Projects/api-proxy-server/api/routes/hello.js": {"path":"/Users/som_ramnani/Projects/api-proxy-server/api/routes/hello.js","statementMap":{"0":{"start":{"line":1,"column":16},"end":{"line":1,"column":34}},"1":{"start":{"line":2,"column":15},"end":{"line":2,"column":31}},"2":{"start":{"line":4,"column":0},"end":{"line":4,"column":62}},"3":{"start":{"line":4,"column":30},"end":{"line":4,"column":60}},"4":{"start":{"line":6,"column":0},"end":{"line":6,"column":24}}},"fnMap":{"0":{"name":"(anonymous_0)","decl":{"start":{"line":4,"column":16},"end":{"line":4,"column":17}},"loc":{"start":{"line":4,"column":30},"end":{"line":4,"column":60}},"line":4}},"branchMap":{},"s":{"0":2,"1":2,"2":2,"3":1,"4":2},"f":{"0":1},"b":{},"_coverageSchema":"1a1c01bbd47fc00a2c39e90264f33305004495a9","hash":"eebe1ea5ad45c88429d33abb13b0c376403ca861"}
,"/Users/som_ramnani/Projects/api-proxy-server/api/routes/superHeroAPI.js": {"path":"/Users/som_ramnani/Projects/api-proxy-server/api/routes/superHeroAPI.js","statementMap":{"0":{"start":{"line":1,"column":16},"end":{"line":1,"column":34}},"1":{"start":{"line":2,"column":14},"end":{"line":2,"column":30}},"2":{"start":{"line":3,"column":15},"end":{"line":3,"column":31}},"3":{"start":{"line":5,"column":0},"end":{"line":16,"column":3}},"4":{"start":{"line":6,"column":2},"end":{"line":15,"column":7}},"5":{"start":{"line":11,"column":6},"end":{"line":11,"column":37}},"6":{"start":{"line":14,"column":6},"end":{"line":14,"column":32}},"7":{"start":{"line":18,"column":0},"end":{"line":18,"column":24}}},"fnMap":{"0":{"name":"(anonymous_0)","decl":{"start":{"line":5,"column":21},"end":{"line":5,"column":22}},"loc":{"start":{"line":5,"column":41},"end":{"line":16,"column":1}},"line":5},"1":{"name":"(anonymous_1)","decl":{"start":{"line":10,"column":10},"end":{"line":10,"column":11}},"loc":{"start":{"line":10,"column":30},"end":{"line":12,"column":5}},"line":10},"2":{"name":"(anonymous_2)","decl":{"start":{"line":13,"column":11},"end":{"line":13,"column":12}},"loc":{"start":{"line":13,"column":20},"end":{"line":15,"column":5}},"line":13}},"branchMap":{},"s":{"0":2,"1":2,"2":2,"3":2,"4":0,"5":0,"6":0,"7":2},"f":{"0":0,"1":0,"2":0},"b":{},"_coverageSchema":"1a1c01bbd47fc00a2c39e90264f33305004495a9","hash":"b8fb18229cabf2caa93bebb7d66e8033fd7bec99"}
{"/Users/som_ramnani/Projects/api-proxy-server/api/index.js": {"path":"/Users/som_ramnani/Projects/api-proxy-server/api/index.js","statementMap":{"0":{"start":{"line":1,"column":0},"end":{"line":1,"column":27}},"1":{"start":{"line":3,"column":16},"end":{"line":3,"column":34}},"2":{"start":{"line":4,"column":13},"end":{"line":4,"column":28}},"3":{"start":{"line":5,"column":26},"end":{"line":5,"column":58}},"4":{"start":{"line":6,"column":24},"end":{"line":6,"column":49}},"5":{"start":{"line":8,"column":12},"end":{"line":8,"column":21}},"6":{"start":{"line":10,"column":20},"end":{"line":14,"column":1}},"7":{"start":{"line":16,"column":0},"end":{"line":16,"column":27}},"8":{"start":{"line":18,"column":0},"end":{"line":18,"column":46}},"9":{"start":{"line":19,"column":0},"end":{"line":19,"column":35}},"10":{"start":{"line":21,"column":0},"end":{"line":27,"column":3}},"11":{"start":{"line":22,"column":2},"end":{"line":26,"column":5}},"12":{"start":{"line":29,"column":13},"end":{"line":29,"column":37}},"13":{"start":{"line":31,"column":0},"end":{"line":35,"column":1}},"14":{"start":{"line":32,"column":2},"end":{"line":34,"column":5}},"15":{"start":{"line":33,"column":4},"end":{"line":33,"column":49}},"16":{"start":{"line":37,"column":0},"end":{"line":37,"column":21}}},"fnMap":{"0":{"name":"(anonymous_0)","decl":{"start":{"line":21,"column":13},"end":{"line":21,"column":14}},"loc":{"start":{"line":21,"column":27},"end":{"line":27,"column":1}},"line":21},"1":{"name":"(anonymous_1)","decl":{"start":{"line":32,"column":19},"end":{"line":32,"column":20}},"loc":{"start":{"line":32,"column":25},"end":{"line":34,"column":3}},"line":32}},"branchMap":{"0":{"loc":{"start":{"line":29,"column":13},"end":{"line":29,"column":37}},"type":"binary-expr","locations":[{"start":{"line":29,"column":13},"end":{"line":29,"column":29}},{"start":{"line":29,"column":33},"end":{"line":29,"column":37}}],"line":29},"1":{"loc":{"start":{"line":31,"column":0},"end":{"line":35,"column":1}},"type":"if","locations":[{"start":{"line":31,"column":0},"end":{"line":35,"column":1}},{"start":{},"end":{}}],"line":31}},"s":{"0":3,"1":3,"2":3,"3":3,"4":3,"5":3,"6":3,"7":3,"8":3,"9":3,"10":3,"11":1,"12":3,"13":3,"14":0,"15":0,"16":3},"f":{"0":1,"1":0},"b":{"0":[3,3],"1":[0,3]},"_coverageSchema":"1a1c01bbd47fc00a2c39e90264f33305004495a9","hash":"7969d7d620e06186000dc7ae288c679f5f036e7d"}
,"/Users/som_ramnani/Projects/api-proxy-server/api/routes/hello.js": {"path":"/Users/som_ramnani/Projects/api-proxy-server/api/routes/hello.js","statementMap":{"0":{"start":{"line":1,"column":16},"end":{"line":1,"column":34}},"1":{"start":{"line":2,"column":15},"end":{"line":2,"column":31}},"2":{"start":{"line":4,"column":0},"end":{"line":4,"column":62}},"3":{"start":{"line":4,"column":30},"end":{"line":4,"column":60}},"4":{"start":{"line":6,"column":0},"end":{"line":6,"column":24}}},"fnMap":{"0":{"name":"(anonymous_0)","decl":{"start":{"line":4,"column":16},"end":{"line":4,"column":17}},"loc":{"start":{"line":4,"column":30},"end":{"line":4,"column":60}},"line":4}},"branchMap":{},"s":{"0":3,"1":3,"2":3,"3":1,"4":3},"f":{"0":1},"b":{},"_coverageSchema":"1a1c01bbd47fc00a2c39e90264f33305004495a9","hash":"eebe1ea5ad45c88429d33abb13b0c376403ca861"}
,"/Users/som_ramnani/Projects/api-proxy-server/api/routes/superHeroAPI.js": {"path":"/Users/som_ramnani/Projects/api-proxy-server/api/routes/superHeroAPI.js","statementMap":{"0":{"start":{"line":1,"column":16},"end":{"line":1,"column":34}},"1":{"start":{"line":2,"column":14},"end":{"line":2,"column":30}},"2":{"start":{"line":3,"column":15},"end":{"line":3,"column":31}},"3":{"start":{"line":5,"column":0},"end":{"line":16,"column":3}},"4":{"start":{"line":6,"column":2},"end":{"line":15,"column":7}},"5":{"start":{"line":11,"column":6},"end":{"line":11,"column":37}},"6":{"start":{"line":14,"column":6},"end":{"line":14,"column":32}},"7":{"start":{"line":18,"column":0},"end":{"line":18,"column":24}}},"fnMap":{"0":{"name":"(anonymous_0)","decl":{"start":{"line":5,"column":21},"end":{"line":5,"column":22}},"loc":{"start":{"line":5,"column":41},"end":{"line":16,"column":1}},"line":5},"1":{"name":"(anonymous_1)","decl":{"start":{"line":10,"column":10},"end":{"line":10,"column":11}},"loc":{"start":{"line":10,"column":30},"end":{"line":12,"column":5}},"line":10},"2":{"name":"(anonymous_2)","decl":{"start":{"line":13,"column":11},"end":{"line":13,"column":12}},"loc":{"start":{"line":13,"column":20},"end":{"line":15,"column":5}},"line":13}},"branchMap":{},"s":{"0":3,"1":3,"2":3,"3":3,"4":1,"5":1,"6":0,"7":3},"f":{"0":1,"1":1,"2":0},"b":{},"_coverageSchema":"1a1c01bbd47fc00a2c39e90264f33305004495a9","hash":"b8fb18229cabf2caa93bebb7d66e8033fd7bec99"}
}
20 changes: 10 additions & 10 deletions coverage/lcov-report/api/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ <h1><a href="../index.html">All files</a> api</h1>
<div class='clearfix'>

<div class='fl pad1y space-right2'>
<span class="strong">88.88% </span>
<span class="strong">88.23% </span>
<span class="quiet">Statements</span>
<span class='fraction'>16/18</span>
<span class='fraction'>15/17</span>
</div>


Expand All @@ -44,9 +44,9 @@ <h1><a href="../index.html">All files</a> api</h1>


<div class='fl pad1y space-right2'>
<span class="strong">88.88% </span>
<span class="strong">88.23% </span>
<span class="quiet">Lines</span>
<span class='fraction'>16/18</span>
<span class='fraction'>15/17</span>
</div>


Expand Down Expand Up @@ -80,17 +80,17 @@ <h1><a href="../index.html">All files</a> api</h1>
</thead>
<tbody><tr>
<td class="file high" data-value="index.js"><a href="index.js.html">index.js</a></td>
<td data-value="88.88" class="pic high">
<td data-value="88.23" class="pic high">
<div class="chart"><div class="cover-fill" style="width: 88%"></div><div class="cover-empty" style="width: 12%"></div></div>
</td>
<td data-value="88.88" class="pct high">88.88%</td>
<td data-value="18" class="abs high">16/18</td>
<td data-value="88.23" class="pct high">88.23%</td>
<td data-value="17" class="abs high">15/17</td>
<td data-value="75" class="pct medium">75%</td>
<td data-value="4" class="abs medium">3/4</td>
<td data-value="50" class="pct medium">50%</td>
<td data-value="2" class="abs medium">1/2</td>
<td data-value="88.88" class="pct high">88.88%</td>
<td data-value="18" class="abs high">16/18</td>
<td data-value="88.23" class="pct high">88.23%</td>
<td data-value="17" class="abs high">15/17</td>
</tr>

</tbody>
Expand All @@ -101,7 +101,7 @@ <h1><a href="../index.html">All files</a> api</h1>
<div class='footer quiet pad2 space-top1 center small'>
Code coverage generated by
<a href="https://istanbul.js.org/" target="_blank" rel="noopener noreferrer">istanbul</a>
at 2024-11-21T06:00:23.535Z
at 2024-11-23T02:47:45.017Z
</div>
<script src="../prettify.js"></script>
<script>
Expand Down
Loading

0 comments on commit 6ee2373

Please sign in to comment.